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

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