Chris@441: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@441: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@441: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: class Message < ActiveRecord::Base Chris@929: include Redmine::SafeAttributes Chris@0: belongs_to :board Chris@0: belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' Chris@0: acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC" Chris@0: acts_as_attachable Chris@0: belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id' Chris@441: Chris@0: acts_as_searchable :columns => ['subject', 'content'], Chris@0: :include => {:board => :project}, Chris@441: :project_key => "#{Board.table_name}.project_id", Chris@0: :date_column => "#{table_name}.created_on" Chris@0: acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"}, Chris@0: :description => :content, Chris@1464: :group => :parent, Chris@0: :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'}, Chris@909: :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} : Chris@0: {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})} Chris@0: Chris@0: acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]}, Chris@0: :author_key => :author_id Chris@0: acts_as_watchable Chris@441: Chris@0: validates_presence_of :board, :subject, :content Chris@0: validates_length_of :subject, :maximum => 255 Chris@909: validate :cannot_reply_to_locked_topic, :on => :create Chris@441: Chris@1115: after_create :add_author_as_watcher, :reset_counters! Chris@909: after_update :update_messages_board Chris@1115: after_destroy :reset_counters! Chris@1464: after_create :send_notification Chris@441: Chris@1464: scope :visible, lambda {|*args| Chris@1464: includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args)) Chris@1464: } Chris@441: Chris@929: safe_attributes 'subject', 'content' Chris@929: safe_attributes 'locked', 'sticky', 'board_id', Chris@929: :if => lambda {|message, user| Chris@929: user.allowed_to?(:edit_messages, message.project) Chris@929: } Chris@929: Chris@0: def visible?(user=User.current) Chris@0: !user.nil? && user.allowed_to?(:view_messages, project) Chris@0: end Chris@441: Chris@909: def cannot_reply_to_locked_topic Chris@0: # Can not reply to a locked topic Chris@909: errors.add :base, 'Topic is locked' if root.locked? && self != root Chris@0: end Chris@441: Chris@909: def update_messages_board Chris@0: if board_id_changed? Chris@1517: Message.where(["id = ? OR parent_id = ?", root.id, root.id]).update_all({:board_id => board_id}) Chris@0: Board.reset_counters!(board_id_was) Chris@0: Board.reset_counters!(board_id) Chris@0: end Chris@0: end Chris@441: Chris@1115: def reset_counters! Chris@1115: if parent && parent.id Chris@1517: Message.where({:id => parent.id}).update_all({:last_reply_id => parent.children.maximum(:id)}) Chris@1115: end Chris@0: board.reset_counters! Chris@0: end Chris@441: Chris@0: def sticky=(arg) Chris@0: write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0) Chris@0: end Chris@441: Chris@0: def sticky? Chris@0: sticky == 1 Chris@0: end Chris@441: Chris@0: def project Chris@0: board.project Chris@0: end Chris@0: Chris@0: def editable_by?(usr) Chris@0: usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))) Chris@0: end Chris@0: Chris@0: def destroyable_by?(usr) Chris@0: usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project))) Chris@0: end Chris@441: Chris@0: private Chris@441: Chris@0: def add_author_as_watcher Chris@0: Watcher.create(:watchable => self.root, :user => author) Chris@0: end Chris@1464: Chris@1464: def send_notification Chris@1464: if Setting.notified_events.include?('message_posted') Chris@1464: Mailer.message_posted(self).deliver Chris@1464: end Chris@1464: end Chris@0: end