被关注+2分、取消关注-2分

讨论区帖子加顶踩功能
顶踩帖子加减分接口
This commit is contained in:
z9hang 2014-06-11 17:41:47 +08:00
parent e8b25aef06
commit e606fd89d2
11 changed files with 160 additions and 23 deletions

View File

@ -71,6 +71,10 @@ class PraiseTreadController < ApplicationController
@obj = Bid.find_by_id(id)
when 'Contest'
@obj = Contest.find_by_id(id)
when 'Memo'
@obj = Memo.find_by_id(id)
when 'Message'
@obj = Message.find_by_id(id)
end
return @obj
end

View File

@ -124,7 +124,7 @@ class WatchersController < ApplicationController
def find_watchables
#根据参数获取关注对象的类型user、project
klass = Object.const_get(params[:object_type].camelcase) rescue nil
#判断获取的对象能否响应watched_by方法
#判断获取的对象类型能否响应watched_by方法
if klass && klass.respond_to?('watched_by')
@watchables = klass.find_all_by_id(Array.wrap(params[:object_id]))
raise Unauthorized if @watchables.any? {|w| w.respond_to?(:visible?) && !w.visible?}

View File

@ -46,9 +46,11 @@ class Journal < ActiveRecord::Base
# fq
after_create :act_as_activity
before_save :be_user_score
# end
before_save :be_user_score
#before_destroy :down_user_score
scope :visible, lambda {|*args|
user = args.shift || User.current
@ -155,10 +157,18 @@ class Journal < ActiveRecord::Base
# 更新用户分数 -by zjc
def be_user_score
#新建了留言且留言不为空,不为空白
#新建了缺陷留言且留言不为空,不为空白
if self.new_record? && !self.notes.nil? && self.notes.gsub(' ','') != ''
#协同得分加分
UserScore.joint(:post_issue, User.current,self.issue.author, { message_id: self.id })
end
end
# 减少用户分数 -by zjc
def down_user_score
#删除有效缺陷留言
if !self.notes.nil? && self.notes.gsub(' ','') != ''
#协同得分减分
UserScore.joint(:delete_issue, User.current,self.issue.author, { message_id: self.id })
end
end
end

View File

@ -43,6 +43,7 @@ class JournalsForMessage < ActiveRecord::Base
after_create :reset_counters!
after_destroy :reset_counters!
before_save :be_user_score
#before_destroy :down_user_score
# default_scope { where('m_parent_id IS NULL') }
@ -103,10 +104,18 @@ class JournalsForMessage < ActiveRecord::Base
# 更新用户分数 -by zjc
def be_user_score
#新建了留言且留言不为空,不为空白
if self.new_record?
#新建了留言回复
if self.new_record? && self.reply_id != 0
#协同得分加分
UserScore.joint(:reply_message, User.current,User.find(self.reply_id), { journals_for_messages_id: self.id })
end
end
# 更新用户分数 -by zjc
def down_user_score
#删除了留言回复
if self.reply_id != 0
#协同得分减分
UserScore.joint(:reply_message_delete, User.current,User.find(self.reply_id), { journals_for_messages_id: self.id })
end
end
end

View File

@ -46,7 +46,7 @@ class Memo < ActiveRecord::Base
# after_create :send_notification
# after_save :plusParentAndForum
# after_destroy :minusParentAndForum
before_save :be_user_score
# scope :visible, lambda { |*args|
# includes(:forum => ).where()
# }
@ -140,5 +140,17 @@ class Memo < ActiveRecord::Base
@forum.topic_count = 0 if @forum.topic_count.to_int < 0
end
@forum.save
end
end
#更新用户分数 -by zjc
def be_user_score
#新建memo且无parent的为发帖
if self.new_record? && self.parent_id.nil?
UserScore.joint(:post_message, User.current,nil, { memo_id: self.id })
#新建memo且有parent的为回帖
elsif self.new_record? && !self.parent_id.nil?
UserScore.joint(:reply_posting, User.current,self.parent.author, { memo_id: self.id })
end
end
end

View File

@ -55,6 +55,7 @@ class Message < ActiveRecord::Base
# fq
after_create :act_as_activity
before_save :be_user_score
#before_destroy :down_user_score
# end
scope :visible, lambda {|*args|
@ -133,4 +134,14 @@ class Message < ActiveRecord::Base
UserScore.joint(:reply_posting, User.current,self.parent.author, { message_id: self.id })
end
end
#减少用户分数
def down_user_score
#删除发帖
if self.parent_id.nil?
UserScore.joint(:delete_message, User.current,nil, { message_id: self.id })
#删除留言
else
UserScore.joint(:reply_deleting, User.current,self.parent.author, { message_id: self.id })
end
end
end

View File

@ -2,5 +2,41 @@ class PraiseTread < ActiveRecord::Base
attr_accessible :user_id,:praise_tread_object_id,:praise_tread_object_type,:praise_or_tread
belongs_to :user
belongs_to :praise_tread_object, polymorphic: true
before_save :be_user_score
def find_object_by_type_and_id(type,id)
@obj = nil
case type
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)
when 'Memo'
@obj = Memo.find_by_id(id)
when 'Message'
@obj = Message.find_by_id(id)
end
return @obj
end
#更新用户分数 - by zjc
def be_user_score
#踩贴吧或讨论区帖子
if self.new_record? && self.praise_or_tread == 0 && (self.praise_tread_object_type == 'Memo' || self.praise_tread_object_type == 'Message')
obj = find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id)
target_user = obj.author
UserScore.skill(:treaded_by_user, User.current,target_user, { praise_tread_id: self.id })
#顶贴吧或讨论区帖子
elsif self.new_record? && self.praise_or_tread == 1 && (self.praise_tread_object_id == 'Memo' || self.praise_tread_object_type == 'Message')
obj = find_object_by_type_and_id(self.praise_tread_object_type,praise_tread_object_id)
target_user = obj.author
UserScore.skill(:praised_by_user, User.current,target_user,{ praise_tread_id: self.id })
end
end
end

View File

@ -56,10 +56,18 @@ class UserScore < ActiveRecord::Base
user_score.collaboration = user_score.collaboration.to_i + 2
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} posting a message. options => (#{options.to_s})"
when :delete_message # current_user 发帖了 Delete Message
user_score.collaboration = user_score.collaboration.to_i - 2
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} deleting a message. options => (#{options.to_s})"
when :post_issue # current_user 对 target_user 的缺陷留言了 Add Journal
user_score.collaboration = user_score.collaboration.to_i + 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} posting a issue. options => (#{options.to_s})"
when :delete_issue # current_user 删除了 对 target_user 的缺陷留言了 Delete Journal
user_score.collaboration = user_score.collaboration.to_i - 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} deleting a issue. options => (#{options.to_s})"
when :change_issue_status # current_user 更改了缺陷的状态 Changed Issue
user_score.collaboration = user_score.collaboration.to_i + 1
user_score.save
@ -68,10 +76,18 @@ class UserScore < ActiveRecord::Base
user_score.collaboration = user_score.collaboration.to_i + 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} reply message. options => (#{options.to_s})"
when :reply_message_delete # current_user 删除了对 target_user 留言的回复 delete Journals_for_messages
user_score.collaboration = user_score.collaboration.to_i - 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} delete reply message. options => (#{options.to_s})"
when :reply_posting # current_user 对 target_user 帖子的回复 Add Message
user_score.collaboration = user_score.collaboration.to_i + 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} reply posting. options => (#{options.to_s})"
when :reply_deleting # current_user 删除了 对 target_user 帖子的回复 Delete Message
user_score.collaboration = user_score.collaboration.to_i - 1
user_score.save
Rails.logger.info "[UserScore#joint] ===> User: #{current_user} reply deleting. options => (#{options.to_s})"
else
Rails.logger.error "[UserScore#joint] ===> #{operate} is not define."
return false
@ -95,19 +111,19 @@ class UserScore < ActiveRecord::Base
user_score = target_user.user_score_attr
case operate
when :followed_by # current_user 关注了target_user
user_score.active = user_score.influence.to_i + 2
user_score.influence = user_score.influence.to_i + 2
user_score.save
Rails.logger.info "[UserScore#influence] ===> User: #{current_user} be followed. options => (#{options.to_s})"
when :cancel_followed # current_uer 取消了对 target_user的关注
user_score.active = user_score.influence.to_i - 2
user_score.influence = user_score.influence.to_i - 2
user_score.save
Rails.logger.info "[UserScore#influence] ===> User: #{current_user} canceled followed. options => (#{options.to_s})"
else
Rails.logger.error "[UserScore#influence] ===> #{operate} is not define."
return false
end
rescue => e
Rails.logger.error "[UserScore#influence] ===> Exception: #{e}."
#rescue => e
#Rails.logger.error "[UserScore#influence] ===> Exception: #{e}."
end
# 前略·技术得分
@ -122,13 +138,24 @@ class UserScore < ActiveRecord::Base
# Returns boolean. 返回积分保存结果
def self.skill(operate, current_user, target_user, options={})
current_user, target_user = get_users(current_user, target_user)
user_score = current_user.user_score_attr
current_user_score = current_user.user_score_attr
target_user_score = target_user.user_score_attr
case operate
# when :treading # current_user 踩了 target_user 的帖子
# Rails.logger.info "[UserScore#skill] ===> User: #{current_user} treading #{target_user}'s posting. options => (#{options.to_s})"
when :treaded_by_user # current_user 踩了 target_user 的帖子
when :treaded_by_user # current_user 踩了 target_user 的帖子
current_user_score.skill = current_user_score.skill.to_i - 2 #踩别人的帖子减2分
current_user_score.save
if current_user.id == target_user.id
target_user.reload
target_user_score = target_user.user_score_attr
end
target_user_score.skill = target_user_score.skill.to_i - 2 #帖子被踩扣分
target_user_score.save
Rails.logger.info "[UserScore#skill] ===> User: #{current_user} treaded_by #{target_user}. options => (#{options.to_s})"
when :praised_by_user # current_user 顶了 target_user 的帖子
when :praised_by_user # current_user 顶了 target_user 的帖子
target_user_score.skill = target_user_score.skill.to_i + 2 #帖子被顶加分
target_user_score.save
Rails.logger.info "[UserScore#skill] ===> User: #{current_user} praised_by #{target_user}. options => (#{options.to_s})"
else
Rails.logger.error "[UserScore#skill] ===> #{operate} is not define."
@ -194,17 +221,17 @@ class UserScore < ActiveRecord::Base
#分数小于0时修正为0
#分数大于等于0时不修正 -by zjc
def correct_score
if self.collaboration < 0
if !self.collaboration.nil? && self.collaboration < 0
self.collaboration = 0
end
if self.active < 0
self.active = 0
end
if self.influence < 0
if !self.influence.nil? && self.influence < 0
self.influence = 0
end
if self.skill < 0
if !self.skill.nil? && self.skill < 0
self.skill = 0
end
if !self.active.nil? && self.active < 0
self.active = 0
end
end
end

View File

@ -22,6 +22,8 @@ class Watcher < ActiveRecord::Base
has_one :project_status
has_one :users_status
#end
before_save :be_user_score
before_destroy :down_user_score
validates_presence_of :user
validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id]
@ -70,4 +72,22 @@ class Watcher < ActiveRecord::Base
end
pruned
end
# 更新用户分数 -by zjc
def be_user_score
#添加了关注
if self.new_record? && self.watchable_type == 'Principal'
#影响力得分
UserScore.influence(:followed_by, self.user,self.watchable, { watcher_id: self.id })
end
end
#减分操作 -by zjc
def down_user_score
#取消关注
if self.watchable_type == 'Principal'
UserScore.influence(:cancel_followed, self.user,self.watchable, { watcher_id: self.id })
end
end
end

View File

@ -60,6 +60,8 @@
</style>
<div class="lz">
<!-- 在这里添加赞和踩-->
<span id="praise_tread" style="float: right"> <%= render :partial => "/praise_tread/praise_tread",:locals => {:obj => @topic,:show_flag => true,:user_id =>User.current.id}%> </span>
<div class="contextual">
<%= watcher_link(@topic, User.current) %>
<%= link_to(
@ -82,6 +84,7 @@
:class => 'icon icon-del'
) if @message.destroyable_by?(User.current) %>
</div>
<div class="lz-left">
<div class="">
<%= link_to image_tag(url_to_avatar(@topic.author), :class => "avatar"), user_path(@topic.author) %>

View File

@ -47,8 +47,13 @@ module Redmine
################### modified by liuping
def remove_watcher(user)
return nil unless user && user.is_a?(User)
if self.instance_of?(User)
Watcher.delete_all "watchable_type = 'Principal' AND watchable_id = #{self.id} AND user_id = #{user.id}"
if self.instance_of?(User)
#通过model层删除以触发before_destroy事件 -by zjc
watchers = Watcher.find_by_sql "select * from `watchers` where watchable_type = 'Principal' AND watchable_id = #{self.id} AND user_id = #{user.id}"
watchers.each do |watcher|
watcher.destroy
end
#Watcher.delete_all "watchable_type = 'Principal' AND watchable_id = #{self.id} AND user_id = #{user.id}"
else
Watcher.delete_all "watchable_type = '#{self.class}' AND watchable_id = #{self.id} AND user_id = #{user.id}"
end