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

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