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 11KB

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