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.2KB

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