Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

message.rb 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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'
  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'
  24. acts_as_searchable :columns => ['subject', 'content'],
  25. :preload => {:board => :project},
  26. :project_key => "#{Board.table_name}.project_id"
  27. acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
  28. :description => :content,
  29. :group => :parent,
  30. :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
  31. :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
  32. {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
  33. acts_as_activity_provider :scope => preload({:board => :project}, :author),
  34. :author_key => :author_id
  35. acts_as_watchable
  36. validates_presence_of :board, :subject, :content
  37. validates_length_of :subject, :maximum => 255
  38. validate :cannot_reply_to_locked_topic, :on => :create
  39. after_create :add_author_as_watcher, :reset_counters!
  40. after_update :update_messages_board
  41. after_destroy :reset_counters!
  42. after_create_commit :send_notification
  43. scope :visible, lambda {|*args|
  44. joins(:board => :project).
  45. where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
  46. }
  47. safe_attributes 'subject', 'content'
  48. safe_attributes 'locked', 'sticky', 'board_id',
  49. :if => lambda {|message, user|
  50. user.allowed_to?(:edit_messages, message.project)
  51. }
  52. def visible?(user=User.current)
  53. !user.nil? && user.allowed_to?(:view_messages, project)
  54. end
  55. def cannot_reply_to_locked_topic
  56. # Can not reply to a locked topic
  57. errors.add :base, 'Topic is locked' if root.locked? && self != root
  58. end
  59. def update_messages_board
  60. if saved_change_to_board_id?
  61. Message.where(["id = ? OR parent_id = ?", root.id, root.id]).update_all({:board_id => board_id})
  62. Board.reset_counters!(board_id_before_last_save)
  63. Board.reset_counters!(board_id)
  64. end
  65. end
  66. def reset_counters!
  67. if parent && parent.id
  68. Message.where({:id => parent.id}).update_all({:last_reply_id => parent.children.maximum(:id)})
  69. end
  70. board.reset_counters!
  71. end
  72. def sticky=(arg)
  73. write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
  74. end
  75. def sticky?
  76. sticky == 1
  77. end
  78. def project
  79. board.project
  80. end
  81. def editable_by?(usr)
  82. usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
  83. end
  84. def destroyable_by?(usr)
  85. usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
  86. end
  87. def notified_users
  88. project.notified_users.reject {|user| !visible?(user)}
  89. end
  90. private
  91. def add_author_as_watcher
  92. Watcher.create(:watchable => self.root, :user => author)
  93. end
  94. def send_notification
  95. if Setting.notified_events.include?('message_posted')
  96. Mailer.deliver_message_posted(self)
  97. end
  98. end
  99. end