213 lines
7.6 KiB
Ruby
213 lines
7.6 KiB
Ruby
# Redmine - project management software
|
||
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
||
#
|
||
# This program is free software; you can redistribute it and/or
|
||
# modify it under the terms of the GNU General Public License
|
||
# as published by the Free Software Foundation; either version 2
|
||
# of the License, or (at your option) any later version.
|
||
#
|
||
# This program is distributed in the hope that it will be useful,
|
||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
# GNU General Public License for more details.
|
||
#
|
||
# You should have received a copy of the GNU General Public License
|
||
# along with this program; if not, write to the Free Software
|
||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
|
||
class Message < ActiveRecord::Base
|
||
include Redmine::SafeAttributes
|
||
include UserScoreHelper
|
||
belongs_to :board
|
||
belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
|
||
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
|
||
|
||
acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
|
||
acts_as_attachable
|
||
belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
|
||
|
||
# added by fq
|
||
has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy
|
||
# 被ForgeActivity虚拟关联
|
||
has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy
|
||
# end
|
||
|
||
acts_as_searchable :columns => ['subject', 'content'],
|
||
:include => {:board => :project},
|
||
:project_key => "#{Board.table_name}.project_id",
|
||
:date_column => "#{table_name}.created_on"
|
||
acts_as_searchable :columns => ['subject', 'content'],
|
||
:include => {:board => :course},
|
||
:course_key => "#{Board.table_name}.course_id",
|
||
:date_column => "#{table_name}.created_at"
|
||
acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
|
||
:description => :content,
|
||
:datetime => :updated_on,
|
||
# :datetime => "#{Message.table_name}.created_on",
|
||
:group => :parent,
|
||
:type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
|
||
:url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
|
||
{:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
|
||
|
||
acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
|
||
:author_key => :author_id
|
||
acts_as_activity_provider :find_options => {:include => [{:board => :course}, :author]},
|
||
:type => 'course_messages',
|
||
:author_key => :author_id
|
||
acts_as_watchable
|
||
|
||
validates_presence_of :board, :subject, :content
|
||
validates_length_of :subject, :maximum => 255
|
||
validate :cannot_reply_to_locked_topic, :on => :create
|
||
|
||
after_create :add_author_as_watcher, :reset_counters!
|
||
after_update :update_messages_board
|
||
after_destroy :reset_counters!,:down_user_score
|
||
|
||
# fq
|
||
after_create :act_as_activity,:be_user_score,:act_as_forge_activity
|
||
#before_save :be_user_score
|
||
# end
|
||
|
||
scope :visible, lambda {|*args|
|
||
includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
|
||
}
|
||
|
||
scope :course_visible, lambda {|*args|
|
||
includes(:board => :course).where(Course.allowed_to_condition(args.shift || User.current, :view_course_messages, *args))
|
||
}
|
||
|
||
|
||
safe_attributes 'subject', 'content'
|
||
safe_attributes 'board_id','locked', 'sticky',
|
||
:if => lambda {|message, user|
|
||
if message.project
|
||
user.allowed_to?(:edit_messages, message.project)
|
||
else
|
||
user.allowed_to?(:edit_messages, message.course)
|
||
end
|
||
}
|
||
|
||
def visible?(user=User.current)
|
||
if project
|
||
!user.nil? && user.allowed_to?(:view_messages, project)
|
||
elsif course
|
||
!user.nil? && user.allowed_to?(:view_messages, course)
|
||
end
|
||
end
|
||
|
||
def cannot_reply_to_locked_topic
|
||
# Can not reply to a locked topic
|
||
errors.add :base, 'Topic is locked' if root.locked? && self != root
|
||
end
|
||
|
||
def update_messages_board
|
||
if board_id_changed?
|
||
Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id])
|
||
Board.reset_counters!(board_id_was)
|
||
Board.reset_counters!(board_id)
|
||
end
|
||
end
|
||
|
||
def reset_counters!
|
||
if parent && parent.id
|
||
Message.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id})
|
||
end
|
||
board.reset_counters!
|
||
end
|
||
|
||
def sticky=(arg)
|
||
write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
|
||
end
|
||
|
||
def sticky?
|
||
sticky == 1
|
||
end
|
||
|
||
def project
|
||
board.project
|
||
end
|
||
|
||
def course
|
||
board.course
|
||
end
|
||
|
||
def course_editable_by?(usr)
|
||
usr && usr.logged? && (usr.allowed_to?(:edit_messages, course) || (self.author == usr && usr.allowed_to?(:edit_own_messages, course)))
|
||
end
|
||
|
||
def course_destroyable_by?(usr)
|
||
usr && usr.logged? && (usr.allowed_to?(:delete_messages, course) || (self.author == usr && usr.allowed_to?(:delete_own_messages, course)))
|
||
end
|
||
|
||
def editable_by?(usr)
|
||
usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
|
||
end
|
||
|
||
def destroyable_by?(usr)
|
||
usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
|
||
end
|
||
|
||
private
|
||
|
||
def add_author_as_watcher
|
||
Watcher.create(:watchable => self.root, :user => author)
|
||
end
|
||
|
||
# fq
|
||
def act_as_activity
|
||
self.acts << Activity.new(:user_id => self.author_id)
|
||
end
|
||
# end
|
||
|
||
# Time 2015-02-27 14:32:25
|
||
# Author lizanle
|
||
# Description
|
||
def act_as_forge_activity
|
||
# 如果project为空,那么就是课程相关的消息
|
||
if !self.board.project.nil?
|
||
self.forge_acts << ForgeActivity.new(:user_id => self.author_id,
|
||
:project_id => self.board.project.id)
|
||
end
|
||
end
|
||
|
||
#更新用户分数 -by zjc
|
||
def be_user_score
|
||
#新建message且无parent的为发帖
|
||
|
||
if self.parent_id.nil? && !self.board.project.nil?
|
||
UserScore.joint(:post_message, self.author,nil,self, { message_id: self.id })
|
||
update_memo_number(self.author,1)
|
||
if self.board.project_id != -1
|
||
update_memo_number(self.author,2,self.board.project)
|
||
end
|
||
#新建message且有parent的为回帖
|
||
elsif !self.parent_id.nil? && !self.board.project.nil?
|
||
UserScore.joint(:reply_posting, self.author,self.parent.author,self, { message_id: self.id })
|
||
update_replay_for_memo(self.author,1)
|
||
if self.board.project_id != -1
|
||
update_replay_for_memo(self.author,2,self.board.project)
|
||
end
|
||
end
|
||
end
|
||
|
||
#减少用户分数
|
||
def down_user_score
|
||
if self.parent_id.nil? && !self.board.project.nil?
|
||
UserScore.joint(:delete_message, self.author,nil,self, { message_id: self.id })
|
||
update_memo_number(User.current,1)
|
||
if self.board.project_id != -1
|
||
update_memo_number(self.author,2,self.board.project)
|
||
end
|
||
elsif !self.parent_id.nil? && !self.board.project.nil?
|
||
UserScore.joint(:reply_deleting, self.author,self.parent.author,self, { message_id: self.id })
|
||
update_replay_for_memo(User.current,1)
|
||
if self.board.project_id != -1
|
||
update_replay_for_memo(self.author,2,self.board.project)
|
||
end
|
||
end
|
||
end
|
||
|
||
|
||
end
|