# 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 include ApplicationHelper has_many_kindeditor_assets :assets, :dependent => :destroy belongs_to :board,:touch => true belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' has_many :praise_tread, as: :praise_tread_object, dependent: :destroy # has_many :org_subfield_messages, :dependent => :destroy # has_many :org_subfields, :through => :org_subfield_messages 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 # 课程动态 has_many :course_acts, :class_name => 'CourseActivity',:as =>:course_act ,:dependent => :destroy # end # 课程/项目 消息 has_many :course_messages, :class_name =>'CourseMessage', :as => :course_message, :dependent => :destroy has_many :forge_messages, :class_name => 'ForgeMessage', :as => :forge_message, :dependent => :destroy has_many :at_messages, as: :at_message, dependent: :destroy has_many :ActivityNotifies,:as => :activity, :dependent => :destroy 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,:delete_kindeditor_assets after_create :act_as_activity,:act_as_course_activity,:be_user_score,:act_as_forge_activity, :act_as_system_message, :send_mail, :act_as_student_score, :act_as_at_message #before_save :be_user_score 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', 'reply_id' 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 topic? parent_id.nil? 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 org_subfield_editable_by?(usr) usr && usr.logged? && (usr.admin? || self.author == usr ||usr.admin_of_org?(self.board.org_subfield.organization)) 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 def set_notify_id(notify_id) @notify_id= notify_id end def get_notify_id() return @notify_id end def set_notify_is_read(notify_is_read) @notify_is_read = notify_is_read end def get_notify_is_read() return @notify_is_read 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_id != -1 && self.parent_id.nil? self.forge_acts << ForgeActivity.new(:user_id => self.author_id, :project_id => self.board.project.id) end end #课程动态公共表记录 def act_as_course_activity if self.course && self.parent_id.nil? self.course_acts << CourseActivity.new(:user_id => self.author_id,:course_id => self.board.course_id) end end # 课程讨论区添加消息: # 老师发帖所有人都能收到消息 # 学生发帖,有人回复则给该学生消息,没回复则不给其它人发送消息 # 帖子被回复的可以收到消息通知 # 项目讨论区添加消息: # 主贴项目成员都能收到 # 回帖:帖子的发布人收到 def act_as_system_message if self.course if self.parent_id.nil? # 主贴 self.course.members.each do |m| if self.author.allowed_to?(:as_teacher, self.course) && m.user_id != self.author_id # 老师 自己的帖子不给自己发送消息 self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.board.course_id, :viewed => false) end end else # 回帖 self.course.members.each do |m| if m.user_id == Message.find(self.parent_id).author_id && m.user_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.board.course_id, :viewed => false) end end end elsif self.project # 项目相关 if self.parent_id.nil? # 主贴 self.project.members.each do |m| if m.user_id != self.author_id self.forge_messages << ForgeMessage.new(:user_id => m.user_id, :project_id => self.board.project_id, :viewed => false) end end else # 回帖 self.project.members.each do |m| if m.user_id == Message.find(self.parent_id).author_id && m.user_id != self.author_id # 只针对主贴回复,回复自己的帖子不发消息 self.forge_messages << ForgeMessage.new(:user_id => m.user_id, :project_id => self.board.project_id, :viewed => false) end end end end end def act_as_at_message users = self.content.scan //m users && users.flatten.uniq.each do |uid| self.at_messages << AtMessage.new(user_id: uid, sender_id: self.author_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 def send_mail Mailer.run.message_posted(self) if Setting.notified_events.include?('message_posted') end # Time 2015-03-31 09:15:06 # Author lizanle # Description 删除对应消息的图片资源 def delete_kindeditor_assets delete_kindeditor_assets_from_disk self.id,OwnerTypeHelper::MESSAGE end # 课程成员得分(英雄榜) def act_as_student_score if self.course unless self.author.allowed_to?(:as_teacher, self.course) if self.parent_id.nil? # 发帖 course_member_score(self.course.id, self.author_id, "Message") else # 回帖 course_member_score(self.course.id, self.author_id, "MessageReply") end end end end end