2013-08-08 17:01:56 +08:00
|
|
|
|
class PraiseTreadController < ApplicationController
|
2013-08-12 14:50:54 +08:00
|
|
|
|
|
|
|
|
|
accept_api_auth :tread_plus,:praise_plus
|
2016-06-15 14:33:25 +08:00
|
|
|
|
# before_filter :require_login,:only => [:praise_plus,:tread_plus]
|
|
|
|
|
before_filter :user_unlogged_check,:only => [:praise_plus,:tread_plus,:praise_minus]
|
2013-08-12 14:50:54 +08:00
|
|
|
|
|
2013-08-08 17:01:56 +08:00
|
|
|
|
def praise_plus
|
|
|
|
|
@obj = nil
|
2016-01-13 19:16:51 +08:00
|
|
|
|
@activity = false
|
2016-01-14 10:07:46 +08:00
|
|
|
|
if request.get?
|
2016-01-14 21:07:05 +08:00
|
|
|
|
@obj_id = params[:obj_id]
|
|
|
|
|
@obj_type = params[:obj_type]
|
2016-01-14 10:07:46 +08:00
|
|
|
|
if !params[:user_activity_id].nil? && !params[:type].nil?
|
|
|
|
|
@user_activity_id = params[:user_activity_id]
|
|
|
|
|
@type = params[:type]
|
|
|
|
|
@activity = true
|
|
|
|
|
end
|
2016-01-14 21:07:05 +08:00
|
|
|
|
# @is_in_list = nil
|
2013-08-12 14:50:54 +08:00
|
|
|
|
@obj = find_object_by_type_and_id(@obj_type,@obj_id)
|
2016-01-14 21:07:05 +08:00
|
|
|
|
pts = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",@obj_id,@obj_type.to_s,User.current.id)
|
|
|
|
|
unless pts.empty?
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
return
|
|
|
|
|
end
|
2016-01-18 15:12:47 +08:00
|
|
|
|
@horizontal = params[:horizontal].downcase == "false" ? false:true if params[:horizontal]
|
2016-05-25 16:19:41 +08:00
|
|
|
|
# if @obj.respond_to?("author_id")
|
|
|
|
|
# author_id = @obj.author_id
|
|
|
|
|
# elsif @obj.respond_to?("user_id")
|
|
|
|
|
# author_id = @obj.user_id
|
|
|
|
|
# end
|
|
|
|
|
# unless author_id == User.current.id
|
|
|
|
|
praise_tread_plus(@obj_type,@obj_id,1)
|
|
|
|
|
# end
|
2016-01-14 10:07:46 +08:00
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
2016-01-13 19:16:51 +08:00
|
|
|
|
end
|
2016-01-14 10:07:46 +08:00
|
|
|
|
end
|
2013-08-08 17:01:56 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def praise_minus
|
|
|
|
|
@obj = nil
|
2016-01-13 19:16:51 +08:00
|
|
|
|
@activity = false
|
2013-08-08 17:01:56 +08:00
|
|
|
|
if request.get?
|
2016-01-13 16:32:05 +08:00
|
|
|
|
#@obj = params[:obj] # 传的是对象,最后变成id了
|
2013-08-08 17:01:56 +08:00
|
|
|
|
#首先更新praise_tread 表 删除关注记录
|
2016-01-13 16:32:05 +08:00
|
|
|
|
#@pt = PraiseTread.find_by_user_id_and_praise_tread_object_id_and_praise_tread_object_type(User.current.id,@obj,"user")
|
2016-01-14 21:07:05 +08:00
|
|
|
|
@obj_id = params[:obj_id]
|
|
|
|
|
@obj_type = params[:obj_type]
|
|
|
|
|
if !params[:user_activity_id].nil? && !params[:type].nil?
|
|
|
|
|
@user_activity_id = params[:user_activity_id]
|
|
|
|
|
@type = params[:type]
|
|
|
|
|
@activity = true
|
|
|
|
|
end
|
|
|
|
|
@obj = find_object_by_type_and_id(@obj_type,@obj_id)
|
2016-01-13 16:32:05 +08:00
|
|
|
|
@pt = PraiseTread.where("praise_tread_object_id=? and praise_tread_object_type=? and user_id=?",@obj_id,@obj_type.to_s,User.current.id).first
|
2016-01-14 21:07:05 +08:00
|
|
|
|
if @pt.nil?
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
return
|
|
|
|
|
end
|
2016-01-13 16:32:05 +08:00
|
|
|
|
@pt.delete if !@pt.nil?
|
2013-08-08 17:01:56 +08:00
|
|
|
|
#再更新praise_tread_cache表 使相应的记录减1 当为0时删除
|
2016-01-13 16:32:05 +08:00
|
|
|
|
@ptc = PraiseTreadCache.where("object_id=? and object_type=?",@obj_id,@obj_type.to_s).first
|
|
|
|
|
@ptc.minus(1) if !@ptc.nil?
|
2013-08-08 17:01:56 +08:00
|
|
|
|
if @ptc.praise_num == 0
|
2016-01-13 16:32:05 +08:00
|
|
|
|
@ptc.delete
|
2013-08-08 17:01:56 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
2016-01-13 16:32:05 +08:00
|
|
|
|
#@obj = User.find_by_id(@obj)
|
2013-08-08 17:01:56 +08:00
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def tread_plus
|
2013-08-11 23:44:12 +08:00
|
|
|
|
@obj = nil
|
2013-08-12 14:50:54 +08:00
|
|
|
|
# @is_in_list = nil
|
2013-08-11 23:44:12 +08:00
|
|
|
|
if request.get?
|
|
|
|
|
@obj_id = params[:obj_id]
|
|
|
|
|
@obj_type = params[:obj_type]
|
2016-01-13 16:32:05 +08:00
|
|
|
|
#@horizontal = params[:horizontal].downcase == "false" ? false:true
|
2013-08-12 14:50:54 +08:00
|
|
|
|
@obj = find_object_by_type_and_id(@obj_type,@obj_id)
|
2014-06-23 11:17:42 +08:00
|
|
|
|
unless @obj.author_id == User.current.id
|
|
|
|
|
praise_tread_plus(@obj_type,@obj_id,0)
|
|
|
|
|
end
|
|
|
|
|
|
2013-08-11 23:44:12 +08:00
|
|
|
|
end
|
2013-08-08 17:01:56 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def tread_minus
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-08-11 23:44:12 +08:00
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def find_object_by_type_and_id(type,id)
|
|
|
|
|
@obj = nil
|
|
|
|
|
case type
|
2016-01-18 15:52:38 +08:00
|
|
|
|
when 'Memo'
|
|
|
|
|
@obj = Memo.find_by_id(id)
|
|
|
|
|
when 'Message'
|
|
|
|
|
@obj = Message.find_by_id(id)
|
|
|
|
|
when 'HomeworkCommon'
|
|
|
|
|
@obj = HomeworkCommon.find_by_id(id)
|
|
|
|
|
when 'JournalsForMessage'
|
|
|
|
|
@obj = JournalsForMessage.find_by_id(id)
|
|
|
|
|
when 'News'
|
|
|
|
|
@obj = News.find_by_id(id)
|
|
|
|
|
when 'Comment'
|
|
|
|
|
@obj = Comment.find_by_id(id)
|
|
|
|
|
when 'Journal'
|
|
|
|
|
@obj = Journal.find_by_id(id)
|
|
|
|
|
when 'BlogComment'
|
|
|
|
|
@obj = BlogComment.find_by_id(id)
|
2016-01-20 10:53:05 +08:00
|
|
|
|
when 'OrgDocumentComment'
|
|
|
|
|
@obj = OrgDocumentComment.find_by_id(id)
|
2016-01-18 15:52:38 +08:00
|
|
|
|
when 'User'
|
|
|
|
|
@obj = User.find_by_id(id)
|
|
|
|
|
when 'Issue'
|
|
|
|
|
@obj = Issue.find_by_id(id)
|
|
|
|
|
when 'Project'
|
|
|
|
|
@obj = Project.find_by_id(id)
|
|
|
|
|
when 'Bid'
|
|
|
|
|
@obj = Bid.find_by_id(id)
|
|
|
|
|
when 'Contest'
|
|
|
|
|
@obj = Contest.find_by_id(id)
|
2016-06-30 10:44:11 +08:00
|
|
|
|
when 'Syllabus'
|
|
|
|
|
@obj = Syllabus.find_by_id(id)
|
2016-01-18 15:52:38 +08:00
|
|
|
|
else
|
|
|
|
|
@obj = nil
|
2013-08-11 23:44:12 +08:00
|
|
|
|
end
|
|
|
|
|
return @obj
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def praise_tread_plus(type,id,flag)
|
|
|
|
|
unless id.nil? and type.nil?
|
|
|
|
|
#首先创建或更新praise_tread 表
|
|
|
|
|
@pt = PraiseTread.new
|
|
|
|
|
@pt.user_id = User.current.id
|
|
|
|
|
@pt.praise_tread_object_id = id.to_i
|
|
|
|
|
@pt.praise_tread_object_type = type
|
|
|
|
|
@pt.praise_or_tread = flag
|
|
|
|
|
@pt.save
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
#再创建或更新praise_tread_cache表
|
2016-01-18 15:12:47 +08:00
|
|
|
|
#@ptc = PraiseTreadCache.find_by_object_id_and_object_type(id,type)
|
|
|
|
|
@ptc = PraiseTreadCache.where("object_id = ? and object_type = ?",id.to_i,type).first
|
2013-08-11 23:44:12 +08:00
|
|
|
|
@ptc = @ptc.nil? ? PraiseTreadCache.new : @ptc
|
2013-08-12 14:50:54 +08:00
|
|
|
|
@ptc.object_id = id.to_i
|
|
|
|
|
@ptc.object_type = type
|
|
|
|
|
@ptc.save
|
|
|
|
|
@ptc.plus(flag,1)
|
2013-08-11 23:44:12 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-08-08 17:01:56 +08:00
|
|
|
|
end
|