2019-03-17 00:29:58 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2011-03-15 17:16:45 +00:00
|
|
|
# Redmine - project management software
|
2020-03-03 00:24:10 +00:00
|
|
|
# Copyright (C) 2006-2020 Jean-Philippe Lang
|
2006-11-27 22:31:14 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-31 07:04:22 +00:00
|
|
|
#
|
2006-11-27 22:31:14 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
2011-08-31 07:04:22 +00:00
|
|
|
#
|
2006-11-27 22:31:14 +00:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
class Journal < ActiveRecord::Base
|
2016-07-10 10:58:00 +00:00
|
|
|
include Redmine::SafeAttributes
|
|
|
|
|
|
2006-11-27 22:31:14 +00:00
|
|
|
belongs_to :journalized, :polymorphic => true
|
2007-03-17 15:18:50 +00:00
|
|
|
# added as a quick fix to allow eager loading of the polymorphic association
|
|
|
|
|
# since always associated to an issue, for now
|
|
|
|
|
belongs_to :issue, :foreign_key => :journalized_id
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2006-11-27 22:31:14 +00:00
|
|
|
belongs_to :user
|
2015-01-24 10:23:53 +00:00
|
|
|
has_many :details, :class_name => "JournalDetail", :dependent => :delete_all, :inverse_of => :journal
|
2008-03-05 15:41:54 +00:00
|
|
|
attr_accessor :indice
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2020-10-22 14:17:43 +00:00
|
|
|
acts_as_event(
|
|
|
|
|
:title =>
|
|
|
|
|
Proc.new do |o|
|
|
|
|
|
status = ((s = o.new_status) ? " (#{s})" : nil)
|
|
|
|
|
"#{o.issue.tracker} ##{o.issue.id}#{status}: #{o.issue.subject}"
|
|
|
|
|
end,
|
|
|
|
|
:description => :notes,
|
|
|
|
|
:author => :user,
|
|
|
|
|
:group => :issue,
|
|
|
|
|
:type =>
|
|
|
|
|
Proc.new do |o|
|
|
|
|
|
(s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note'
|
|
|
|
|
end,
|
|
|
|
|
:url =>
|
|
|
|
|
Proc.new do |o|
|
|
|
|
|
{:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}
|
|
|
|
|
end
|
|
|
|
|
)
|
|
|
|
|
acts_as_activity_provider(
|
|
|
|
|
:type => 'issues',
|
|
|
|
|
:author_key => :user_id,
|
|
|
|
|
:scope =>
|
|
|
|
|
proc do
|
|
|
|
|
preload({:issue => :project}, :user).
|
|
|
|
|
joins("LEFT OUTER JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id").
|
|
|
|
|
where("#{Journal.table_name}.journalized_type = 'Issue' AND" +
|
|
|
|
|
" (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')").distinct
|
|
|
|
|
end
|
|
|
|
|
)
|
2012-10-03 21:36:19 +00:00
|
|
|
before_create :split_private_notes
|
2018-10-10 17:13:09 +00:00
|
|
|
after_create_commit :send_notification
|
2012-10-03 21:36:19 +00:00
|
|
|
|
|
|
|
|
scope :visible, lambda {|*args|
|
|
|
|
|
user = args.shift || User.current
|
2017-01-19 20:16:49 +00:00
|
|
|
options = args.shift || {}
|
|
|
|
|
|
2014-10-22 17:37:16 +00:00
|
|
|
joins(:issue => :project).
|
2017-01-19 20:16:49 +00:00
|
|
|
where(Issue.visible_condition(user, options)).
|
|
|
|
|
where(Journal.visible_notes_condition(user, :skip_pre_condition => true))
|
2012-10-03 21:36:19 +00:00
|
|
|
}
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2019-11-08 00:46:13 +00:00
|
|
|
safe_attributes(
|
|
|
|
|
'notes',
|
|
|
|
|
:if => lambda {|journal, user| journal.new_record? || journal.editable_by?(user)})
|
|
|
|
|
safe_attributes(
|
|
|
|
|
'private_notes',
|
|
|
|
|
:if => lambda {|journal, user| user.allowed_to?(:set_notes_private, journal.project)})
|
2016-07-10 10:58:00 +00:00
|
|
|
|
2017-01-19 20:16:49 +00:00
|
|
|
# Returns a SQL condition to filter out journals with notes that are not visible to user
|
|
|
|
|
def self.visible_notes_condition(user=User.current, options={})
|
|
|
|
|
private_notes_permission = Project.allowed_to_condition(user, :view_private_notes, options)
|
|
|
|
|
sanitize_sql_for_conditions(["(#{table_name}.private_notes = ? OR #{table_name}.user_id = ? OR (#{private_notes_permission}))", false, user.id])
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 08:03:32 +00:00
|
|
|
def initialize(*args)
|
|
|
|
|
super
|
2014-12-08 17:18:28 +00:00
|
|
|
if journalized
|
|
|
|
|
if journalized.new_record?
|
|
|
|
|
self.notify = false
|
|
|
|
|
else
|
|
|
|
|
start
|
|
|
|
|
end
|
2014-12-05 08:03:32 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2009-03-23 17:18:04 +00:00
|
|
|
def save(*args)
|
2014-12-05 08:03:32 +00:00
|
|
|
journalize_changes
|
2007-10-19 19:09:47 +00:00
|
|
|
# Do not save an empty journal
|
|
|
|
|
(details.empty? && notes.blank?) ? false : super
|
|
|
|
|
end
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2013-07-13 09:56:58 +00:00
|
|
|
# Returns journal details that are visible to user
|
2013-07-13 09:20:11 +00:00
|
|
|
def visible_details(user=User.current)
|
|
|
|
|
details.select do |detail|
|
2013-09-05 11:14:30 +00:00
|
|
|
if detail.property == 'cf'
|
2013-10-13 10:04:59 +00:00
|
|
|
detail.custom_field && detail.custom_field.visible_by?(project, user)
|
2013-07-13 09:56:58 +00:00
|
|
|
elsif detail.property == 'relation'
|
|
|
|
|
Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user)
|
2013-07-13 09:20:11 +00:00
|
|
|
else
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-17 08:19:45 +00:00
|
|
|
# Returns the JournalDetail for the given attribute, or nil if the attribute
|
|
|
|
|
# was not updated
|
|
|
|
|
def detail_for_attribute(attribute)
|
|
|
|
|
details.detect {|detail| detail.prop_key == attribute}
|
|
|
|
|
end
|
|
|
|
|
|
2007-11-08 19:00:37 +00:00
|
|
|
# Returns the new status if the journal contains a status change, otherwise nil
|
|
|
|
|
def new_status
|
2014-03-17 08:19:45 +00:00
|
|
|
s = new_value_for('status_id')
|
|
|
|
|
s ? IssueStatus.find_by_id(s.to_i) : nil
|
2007-11-08 19:00:37 +00:00
|
|
|
end
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2008-01-20 10:42:16 +00:00
|
|
|
def new_value_for(prop)
|
2014-03-17 08:19:45 +00:00
|
|
|
detail_for_attribute(prop).try(:value)
|
2008-01-20 10:42:16 +00:00
|
|
|
end
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2008-02-02 10:50:31 +00:00
|
|
|
def editable_by?(usr)
|
2008-02-16 13:19:33 +00:00
|
|
|
usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
|
2008-02-02 10:50:31 +00:00
|
|
|
end
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2008-02-17 14:17:20 +00:00
|
|
|
def project
|
|
|
|
|
journalized.respond_to?(:project) ? journalized.project : nil
|
|
|
|
|
end
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2008-02-17 14:17:20 +00:00
|
|
|
def attachments
|
2016-11-18 08:41:56 +00:00
|
|
|
journalized.respond_to?(:attachments) ? journalized.attachments : []
|
2008-02-17 14:17:20 +00:00
|
|
|
end
|
2010-09-05 22:57:20 +00:00
|
|
|
|
|
|
|
|
# Returns a string of css classes
|
|
|
|
|
def css_classes
|
2019-03-17 00:29:58 +00:00
|
|
|
s = +'journal'
|
2010-09-05 22:57:20 +00:00
|
|
|
s << ' has-notes' unless notes.blank?
|
|
|
|
|
s << ' has-details' unless details.blank?
|
2012-10-03 21:36:19 +00:00
|
|
|
s << ' private-notes' if private_notes?
|
2010-09-05 22:57:20 +00:00
|
|
|
s
|
|
|
|
|
end
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2011-04-29 13:27:50 +00:00
|
|
|
def notify?
|
|
|
|
|
@notify != false
|
|
|
|
|
end
|
2011-08-31 07:04:22 +00:00
|
|
|
|
2011-04-29 13:27:50 +00:00
|
|
|
def notify=(arg)
|
|
|
|
|
@notify = arg
|
|
|
|
|
end
|
2012-10-03 21:36:19 +00:00
|
|
|
|
2013-07-13 09:20:11 +00:00
|
|
|
def notified_users
|
2012-10-03 21:36:19 +00:00
|
|
|
notified = journalized.notified_users
|
|
|
|
|
if private_notes?
|
|
|
|
|
notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
|
|
|
|
|
end
|
2013-07-13 09:20:11 +00:00
|
|
|
notified
|
2012-10-03 21:36:19 +00:00
|
|
|
end
|
|
|
|
|
|
2013-07-13 09:20:11 +00:00
|
|
|
def recipients
|
|
|
|
|
notified_users.map(&:mail)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def notified_watchers
|
2012-11-05 15:49:07 +00:00
|
|
|
notified = journalized.notified_watchers
|
|
|
|
|
if private_notes?
|
|
|
|
|
notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
|
|
|
|
|
end
|
2013-07-13 09:20:11 +00:00
|
|
|
notified
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def watcher_recipients
|
|
|
|
|
notified_watchers.map(&:mail)
|
2012-11-05 15:49:07 +00:00
|
|
|
end
|
|
|
|
|
|
2013-10-13 10:04:59 +00:00
|
|
|
# Sets @custom_field instance variable on journals details using a single query
|
|
|
|
|
def self.preload_journals_details_custom_fields(journals)
|
|
|
|
|
field_ids = journals.map(&:details).flatten.select {|d| d.property == 'cf'}.map(&:prop_key).uniq
|
|
|
|
|
if field_ids.any?
|
2014-01-27 02:17:57 +00:00
|
|
|
fields_by_id = CustomField.where(:id => field_ids).inject({}) {|h, f| h[f.id] = f; h}
|
2013-10-13 10:04:59 +00:00
|
|
|
journals.each do |journal|
|
|
|
|
|
journal.details.each do |detail|
|
|
|
|
|
if detail.property == 'cf'
|
|
|
|
|
detail.instance_variable_set "@custom_field", fields_by_id[detail.prop_key.to_i]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
journals
|
|
|
|
|
end
|
|
|
|
|
|
2014-12-05 08:03:32 +00:00
|
|
|
# Stores the values of the attributes and custom fields of the journalized object
|
|
|
|
|
def start
|
|
|
|
|
if journalized
|
|
|
|
|
@attributes_before_change = journalized.journalized_attribute_names.inject({}) do |h, attribute|
|
|
|
|
|
h[attribute] = journalized.send(attribute)
|
|
|
|
|
h
|
|
|
|
|
end
|
|
|
|
|
@custom_values_before_change = journalized.custom_field_values.inject({}) do |h, c|
|
|
|
|
|
h[c.custom_field_id] = c.value
|
|
|
|
|
h
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
self
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Adds a journal detail for an attachment that was added or removed
|
|
|
|
|
def journalize_attachment(attachment, added_or_removed)
|
|
|
|
|
key = (added_or_removed == :removed ? :old_value : :value)
|
2019-11-25 18:58:05 +00:00
|
|
|
details <<
|
|
|
|
|
JournalDetail.new(
|
2014-12-05 08:03:32 +00:00
|
|
|
:property => 'attachment',
|
|
|
|
|
:prop_key => attachment.id,
|
|
|
|
|
key => attachment.filename
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Adds a journal detail for an issue relation that was added or removed
|
|
|
|
|
def journalize_relation(relation, added_or_removed)
|
|
|
|
|
key = (added_or_removed == :removed ? :old_value : :value)
|
2019-11-25 18:58:05 +00:00
|
|
|
details <<
|
|
|
|
|
JournalDetail.new(
|
2014-12-05 08:03:32 +00:00
|
|
|
:property => 'relation',
|
|
|
|
|
:prop_key => relation.relation_type_for(journalized),
|
|
|
|
|
key => relation.other_issue(journalized).try(:id)
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2012-10-03 21:36:19 +00:00
|
|
|
private
|
|
|
|
|
|
2014-12-05 08:03:32 +00:00
|
|
|
# Generates journal details for attribute and custom field changes
|
|
|
|
|
def journalize_changes
|
|
|
|
|
# attributes changes
|
|
|
|
|
if @attributes_before_change
|
2016-09-03 07:26:49 +00:00
|
|
|
attrs = (journalized.journalized_attribute_names + @attributes_before_change.keys).uniq
|
|
|
|
|
attrs.each do |attribute|
|
2014-12-05 08:03:32 +00:00
|
|
|
before = @attributes_before_change[attribute]
|
|
|
|
|
after = journalized.send(attribute)
|
|
|
|
|
next if before == after || (before.blank? && after.blank?)
|
2019-11-22 14:54:54 +00:00
|
|
|
|
2014-12-05 08:03:32 +00:00
|
|
|
add_attribute_detail(attribute, before, after)
|
2016-09-03 07:26:49 +00:00
|
|
|
end
|
2014-12-05 08:03:32 +00:00
|
|
|
end
|
2016-09-03 07:26:49 +00:00
|
|
|
# custom fields changes
|
2014-12-05 08:03:32 +00:00
|
|
|
if @custom_values_before_change
|
2016-09-03 07:26:49 +00:00
|
|
|
values_by_custom_field_id = {}
|
|
|
|
|
@custom_values_before_change.each do |custom_field_id, value|
|
|
|
|
|
values_by_custom_field_id[custom_field_id] = nil
|
|
|
|
|
end
|
|
|
|
|
journalized.custom_field_values.each do |c|
|
|
|
|
|
values_by_custom_field_id[c.custom_field_id] = c.value
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
values_by_custom_field_id.each do |custom_field_id, after|
|
|
|
|
|
before = @custom_values_before_change[custom_field_id]
|
2014-12-05 08:03:32 +00:00
|
|
|
next if before == after || (before.blank? && after.blank?)
|
|
|
|
|
|
|
|
|
|
if before.is_a?(Array) || after.is_a?(Array)
|
|
|
|
|
before = [before] unless before.is_a?(Array)
|
|
|
|
|
after = [after] unless after.is_a?(Array)
|
|
|
|
|
|
|
|
|
|
# values removed
|
|
|
|
|
(before - after).reject(&:blank?).each do |value|
|
2016-09-03 07:26:49 +00:00
|
|
|
add_custom_field_detail(custom_field_id, value, nil)
|
2014-12-05 08:03:32 +00:00
|
|
|
end
|
|
|
|
|
# values added
|
|
|
|
|
(after - before).reject(&:blank?).each do |value|
|
2016-09-03 07:26:49 +00:00
|
|
|
add_custom_field_detail(custom_field_id, nil, value)
|
2014-12-05 08:03:32 +00:00
|
|
|
end
|
|
|
|
|
else
|
2016-09-03 07:26:49 +00:00
|
|
|
add_custom_field_detail(custom_field_id, before, after)
|
2014-12-05 08:03:32 +00:00
|
|
|
end
|
2016-09-03 07:26:49 +00:00
|
|
|
end
|
2014-12-05 08:03:32 +00:00
|
|
|
end
|
|
|
|
|
start
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Adds a journal detail for an attribute change
|
|
|
|
|
def add_attribute_detail(attribute, old_value, value)
|
|
|
|
|
add_detail('attr', attribute, old_value, value)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Adds a journal detail for a custom field value change
|
2016-09-03 07:26:49 +00:00
|
|
|
def add_custom_field_detail(custom_field_id, old_value, value)
|
|
|
|
|
add_detail('cf', custom_field_id, old_value, value)
|
2014-12-05 08:03:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Adds a journal detail
|
|
|
|
|
def add_detail(property, prop_key, old_value, value)
|
2019-11-25 18:58:05 +00:00
|
|
|
details <<
|
|
|
|
|
JournalDetail.new(
|
2014-12-05 08:03:32 +00:00
|
|
|
:property => property,
|
|
|
|
|
:prop_key => prop_key,
|
|
|
|
|
:old_value => old_value,
|
|
|
|
|
:value => value
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
2012-10-03 21:36:19 +00:00
|
|
|
def split_private_notes
|
|
|
|
|
if private_notes?
|
|
|
|
|
if notes.present?
|
|
|
|
|
if details.any?
|
|
|
|
|
# Split the journal (notes/changes) so we don't have half-private journals
|
|
|
|
|
journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
|
|
|
|
|
journal.details = details
|
|
|
|
|
journal.save
|
|
|
|
|
self.details = []
|
|
|
|
|
self.created_on = journal.created_on
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
# Blank notes should not be private
|
|
|
|
|
self.private_notes = false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
true
|
|
|
|
|
end
|
2013-07-14 14:26:27 +00:00
|
|
|
|
|
|
|
|
def send_notification
|
2019-11-26 06:30:01 +00:00
|
|
|
if notify? &&
|
|
|
|
|
(
|
|
|
|
|
Setting.notified_events.include?('issue_updated') ||
|
|
|
|
|
(Setting.notified_events.include?('issue_note_added') && notes.present?) ||
|
|
|
|
|
(Setting.notified_events.include?('issue_status_updated') && new_status.present?) ||
|
|
|
|
|
(Setting.notified_events.include?('issue_assigned_to_updated') && detail_for_attribute('assigned_to_id').present?) ||
|
|
|
|
|
(Setting.notified_events.include?('issue_priority_updated') && new_value_for('priority_id').present?) ||
|
|
|
|
|
(Setting.notified_events.include?('issue_fixed_version_updated') && detail_for_attribute('fixed_version_id').present?)
|
|
|
|
|
)
|
2013-07-14 14:26:27 +00:00
|
|
|
Mailer.deliver_issue_edit(self)
|
|
|
|
|
end
|
|
|
|
|
end
|
2006-11-27 22:31:14 +00:00
|
|
|
end
|