You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

message.rb 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2013 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class Message < ActiveRecord::Base
  18. include Redmine::SafeAttributes
  19. belongs_to :board
  20. belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
  21. acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
  22. acts_as_attachable
  23. belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
  24. acts_as_searchable :columns => ['subject', 'content'],
  25. :include => {:board => :project},
  26. :project_key => "#{Board.table_name}.project_id",
  27. :date_column => "#{table_name}.created_on"
  28. acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
  29. :description => :content,
  30. :group => :parent,
  31. :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
  32. :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
  33. {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
  34. acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
  35. :author_key => :author_id
  36. acts_as_watchable
  37. validates_presence_of :board, :subject, :content
  38. validates_length_of :subject, :maximum => 255
  39. validate :cannot_reply_to_locked_topic, :on => :create
  40. after_create :add_author_as_watcher, :reset_counters!
  41. after_update :update_messages_board
  42. after_destroy :reset_counters!
  43. scope :visible, lambda {|*args|
  44. includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
  45. }
  46. safe_attributes 'subject', 'content'
  47. safe_attributes 'locked', 'sticky', 'board_id',
  48. :if => lambda {|message, user|
  49. user.allowed_to?(:edit_messages, message.project)
  50. }
  51. def visible?(user=User.current)
  52. !user.nil? && user.allowed_to?(:view_messages, project)
  53. end
  54. def cannot_reply_to_locked_topic
  55. # Can not reply to a locked topic
  56. errors.add :base, 'Topic is locked' if root.locked? && self != root
  57. end
  58. def update_messages_board
  59. if board_id_changed?
  60. Message.update_all({:board_id => board_id}, ["id = ? OR parent_id = ?", root.id, root.id])
  61. Board.reset_counters!(board_id_was)
  62. Board.reset_counters!(board_id)
  63. end
  64. end
  65. def reset_counters!
  66. if parent && parent.id
  67. Message.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id})
  68. end
  69. board.reset_counters!
  70. end
  71. def sticky=(arg)
  72. write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
  73. end
  74. def sticky?
  75. sticky == 1
  76. end
  77. def project
  78. board.project
  79. end
  80. def editable_by?(usr)
  81. usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
  82. end
  83. def destroyable_by?(usr)
  84. usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
  85. end
  86. private
  87. def add_author_as_watcher
  88. Watcher.create(:watchable => self.root, :user => author)
  89. end
  90. end