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.

journal.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 Journal < ApplicationRecord
  19. include Redmine::SafeAttributes
  20. belongs_to :journalized, :polymorphic => true
  21. # added as a quick fix to allow eager loading of the polymorphic association
  22. # since always associated to an issue, for now
  23. belongs_to :issue, :foreign_key => :journalized_id
  24. belongs_to :user
  25. belongs_to :updated_by, :class_name => 'User'
  26. has_many :details, :class_name => "JournalDetail", :dependent => :delete_all, :inverse_of => :journal
  27. attr_accessor :indice
  28. acts_as_event(
  29. :title =>
  30. Proc.new do |o|
  31. status = ((s = o.new_status) ? " (#{s})" : nil)
  32. "#{o.issue.tracker} ##{o.issue.id}#{status}: #{o.issue.subject}"
  33. end,
  34. :description => :notes,
  35. :author => :user,
  36. :group => :issue,
  37. :type =>
  38. Proc.new do |o|
  39. (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note'
  40. end,
  41. :url =>
  42. Proc.new do |o|
  43. {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}
  44. end
  45. )
  46. acts_as_activity_provider(
  47. :type => 'issues',
  48. :author_key => :user_id,
  49. :scope =>
  50. proc do
  51. preload({:issue => :project}, :user).
  52. joins("LEFT OUTER JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id").
  53. where("#{Journal.table_name}.journalized_type = 'Issue' AND" +
  54. " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')").distinct
  55. end
  56. )
  57. acts_as_mentionable :attributes => ['notes']
  58. before_create :split_private_notes
  59. before_create :add_watcher
  60. after_create_commit :send_notification
  61. scope :visible, (lambda do |*args|
  62. user = args.shift || User.current
  63. options = args.shift || {}
  64. joins(:issue => :project).
  65. where(Issue.visible_condition(user, options)).
  66. where(Journal.visible_notes_condition(user, :skip_pre_condition => true))
  67. end)
  68. safe_attributes(
  69. 'notes',
  70. :if => lambda {|journal, user| journal.new_record? || journal.editable_by?(user)})
  71. safe_attributes(
  72. 'private_notes',
  73. :if => lambda {|journal, user| user.allowed_to?(:set_notes_private, journal.project)})
  74. safe_attributes 'updated_by'
  75. # Returns a SQL condition to filter out journals with notes that are not visible to user
  76. def self.visible_notes_condition(user=User.current, options={})
  77. private_notes_permission = Project.allowed_to_condition(user, :view_private_notes, options)
  78. sanitize_sql_for_conditions(["(#{table_name}.private_notes = ? OR #{table_name}.user_id = ? OR (#{private_notes_permission}))", false, user.id])
  79. end
  80. def initialize(*args)
  81. super
  82. if journalized
  83. if journalized.new_record?
  84. self.notify = false
  85. else
  86. start
  87. end
  88. end
  89. end
  90. def save(*args)
  91. journalize_changes
  92. # Do not save an empty journal
  93. (details.empty? && notes.blank?) ? false : super()
  94. end
  95. # Returns journal details that are visible to user
  96. def visible_details(user=User.current)
  97. details.select do |detail|
  98. if detail.property == 'cf'
  99. detail.custom_field && detail.custom_field.visible_by?(project, user)
  100. elsif detail.property == 'relation'
  101. Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user)
  102. else
  103. true
  104. end
  105. end
  106. end
  107. # Returns the JournalDetail for the given attribute, or nil if the attribute
  108. # was not updated
  109. def detail_for_attribute(attribute)
  110. details.detect {|detail| detail.prop_key == attribute}
  111. end
  112. # Returns the new status if the journal contains a status change, otherwise nil
  113. def new_status
  114. s = new_value_for('status_id')
  115. s ? IssueStatus.find_by_id(s.to_i) : nil
  116. end
  117. def new_value_for(prop)
  118. detail_for_attribute(prop).try(:value)
  119. end
  120. def editable_by?(usr)
  121. usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
  122. end
  123. def project
  124. journalized.respond_to?(:project) ? journalized.project : nil
  125. end
  126. def attachments
  127. @attachments ||= begin
  128. ids = details.select {|d| d.property == 'attachment' && d.value.present?}.map(&:prop_key)
  129. ids.empty? ? [] : Attachment.where(id: ids).sort_by {|a| ids.index(a.id.to_s)}
  130. end
  131. end
  132. def visible?(*args)
  133. journalized.visible?(*args)
  134. end
  135. # Returns a string of css classes
  136. def css_classes
  137. s = +'journal'
  138. s << ' has-notes' unless notes.blank?
  139. s << ' has-details' unless details.blank?
  140. s << ' private-notes' if private_notes?
  141. s
  142. end
  143. def notify?
  144. @notify != false
  145. end
  146. def notify=(arg)
  147. @notify = arg
  148. end
  149. def notified_users
  150. notified = journalized.notified_users
  151. if private_notes?
  152. notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
  153. end
  154. notified
  155. end
  156. def recipients
  157. notified_users.map(&:mail)
  158. end
  159. def notified_watchers
  160. notified = journalized.notified_watchers
  161. select_journal_visible_user(notified)
  162. end
  163. def notified_mentions
  164. notified = super
  165. select_journal_visible_user(notified)
  166. end
  167. def watcher_recipients
  168. notified_watchers.map(&:mail)
  169. end
  170. # Sets @custom_field instance variable on journals details using a single query
  171. def self.preload_journals_details_custom_fields(journals)
  172. field_ids = journals.map(&:details).flatten.select {|d| d.property == 'cf'}.map(&:prop_key).uniq
  173. if field_ids.any?
  174. fields_by_id = CustomField.where(:id => field_ids).inject({}) {|h, f| h[f.id] = f; h}
  175. journals.each do |journal|
  176. journal.details.each do |detail|
  177. if detail.property == 'cf'
  178. detail.instance_variable_set :@custom_field, fields_by_id[detail.prop_key.to_i]
  179. end
  180. end
  181. end
  182. end
  183. journals
  184. end
  185. # Stores the values of the attributes and custom fields of the journalized object
  186. def start
  187. if journalized
  188. @attributes_before_change = journalized.journalized_attribute_names.inject({}) do |h, attribute|
  189. h[attribute] = journalized.send(attribute)
  190. h
  191. end
  192. @custom_values_before_change = journalized.custom_field_values.inject({}) do |h, c|
  193. h[c.custom_field_id] = c.value
  194. h
  195. end
  196. end
  197. self
  198. end
  199. # Adds a journal detail for an attachment that was added or removed
  200. def journalize_attachment(attachment, added_or_removed)
  201. key = (added_or_removed == :removed ? :old_value : :value)
  202. details <<
  203. JournalDetail.new(
  204. :property => 'attachment',
  205. :prop_key => attachment.id,
  206. key => attachment.filename
  207. )
  208. end
  209. # Adds a journal detail for an issue relation that was added or removed
  210. def journalize_relation(relation, added_or_removed)
  211. key = (added_or_removed == :removed ? :old_value : :value)
  212. details <<
  213. JournalDetail.new(
  214. :property => 'relation',
  215. :prop_key => relation.relation_type_for(journalized),
  216. key => relation.other_issue(journalized).try(:id)
  217. )
  218. end
  219. private
  220. # Generates journal details for attribute and custom field changes
  221. def journalize_changes
  222. # attributes changes
  223. if @attributes_before_change
  224. attrs = (journalized.journalized_attribute_names + @attributes_before_change.keys).uniq
  225. attrs.each do |attribute|
  226. before = @attributes_before_change[attribute]
  227. after = journalized.send(attribute)
  228. next if before == after || (before.blank? && after.blank?)
  229. add_attribute_detail(attribute, before, after)
  230. end
  231. end
  232. # custom fields changes
  233. if @custom_values_before_change
  234. values_by_custom_field_id = {}
  235. @custom_values_before_change.each_key do |custom_field_id|
  236. values_by_custom_field_id[custom_field_id] = nil
  237. end
  238. journalized.custom_field_values.each do |c|
  239. values_by_custom_field_id[c.custom_field_id] = c.value
  240. end
  241. values_by_custom_field_id.each do |custom_field_id, after|
  242. before = @custom_values_before_change[custom_field_id]
  243. next if before == after || (before.blank? && after.blank?)
  244. if before.is_a?(Array) || after.is_a?(Array)
  245. before = [before] unless before.is_a?(Array)
  246. after = [after] unless after.is_a?(Array)
  247. # values removed
  248. (before - after).reject(&:blank?).each do |value|
  249. add_custom_field_detail(custom_field_id, value, nil)
  250. end
  251. # values added
  252. (after - before).reject(&:blank?).each do |value|
  253. add_custom_field_detail(custom_field_id, nil, value)
  254. end
  255. else
  256. add_custom_field_detail(custom_field_id, before, after)
  257. end
  258. end
  259. end
  260. start
  261. end
  262. # Adds a journal detail for an attribute change
  263. def add_attribute_detail(attribute, old_value, value)
  264. add_detail('attr', attribute, old_value, value)
  265. end
  266. # Adds a journal detail for a custom field value change
  267. def add_custom_field_detail(custom_field_id, old_value, value)
  268. add_detail('cf', custom_field_id, old_value, value)
  269. end
  270. # Adds a journal detail
  271. def add_detail(property, prop_key, old_value, value)
  272. details <<
  273. JournalDetail.new(
  274. :property => property,
  275. :prop_key => prop_key,
  276. :old_value => old_value,
  277. :value => value
  278. )
  279. end
  280. def split_private_notes
  281. if private_notes?
  282. if notes.present?
  283. if details.any?
  284. # Split the journal (notes/changes) so we don't have half-private journals
  285. journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
  286. journal.details = details
  287. journal.save
  288. self.details = []
  289. self.created_on = journal.created_on
  290. end
  291. else
  292. # Blank notes should not be private
  293. self.private_notes = false
  294. end
  295. end
  296. true
  297. end
  298. def add_watcher
  299. if user&.active? &&
  300. user&.allowed_to?(:add_issue_watchers, project) &&
  301. user.pref.auto_watch_on?('issue_contributed_to') &&
  302. !Watcher.any_watched?(Array.wrap(journalized), user)
  303. journalized.set_watcher(user, true)
  304. end
  305. end
  306. def send_notification
  307. if notify? &&
  308. (
  309. Setting.notified_events.include?('issue_updated') ||
  310. (Setting.notified_events.include?('issue_note_added') && notes.present?) ||
  311. (Setting.notified_events.include?('issue_status_updated') && new_status.present?) ||
  312. (Setting.notified_events.include?('issue_assigned_to_updated') && detail_for_attribute('assigned_to_id').present?) ||
  313. (Setting.notified_events.include?('issue_priority_updated') && new_value_for('priority_id').present?) ||
  314. (Setting.notified_events.include?('issue_fixed_version_updated') && detail_for_attribute('fixed_version_id').present?)
  315. )
  316. Mailer.deliver_issue_edit(self)
  317. end
  318. end
  319. def select_journal_visible_user(notified)
  320. if private_notes?
  321. notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
  322. end
  323. notified
  324. end
  325. end