##// END OF EJS Templates
Fixed that journals are shown multiple times in activity (#19168)....
Jean-Philippe Lang -
r13781:cb9d405db3ba
parent child
Show More
@@ -1,305 +1,305
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Journal < ActiveRecord::Base
19 19 belongs_to :journalized, :polymorphic => true
20 20 # added as a quick fix to allow eager loading of the polymorphic association
21 21 # since always associated to an issue, for now
22 22 belongs_to :issue, :foreign_key => :journalized_id
23 23
24 24 belongs_to :user
25 25 has_many :details, :class_name => "JournalDetail", :dependent => :delete_all, :inverse_of => :journal
26 26 attr_accessor :indice
27 27 attr_protected :id
28 28
29 29 acts_as_event :title => Proc.new {|o| status = ((s = o.new_status) ? " (#{s})" : nil); "#{o.issue.tracker} ##{o.issue.id}#{status}: #{o.issue.subject}" },
30 30 :description => :notes,
31 31 :author => :user,
32 32 :group => :issue,
33 33 :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' },
34 34 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}}
35 35
36 36 acts_as_activity_provider :type => 'issues',
37 37 :author_key => :user_id,
38 38 :scope => preload({:issue => :project}, :user).
39 39 joins("LEFT OUTER JOIN #{JournalDetail.table_name} ON #{JournalDetail.table_name}.journal_id = #{Journal.table_name}.id").
40 40 where("#{Journal.table_name}.journalized_type = 'Issue' AND" +
41 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')")
41 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')").uniq
42 42
43 43 before_create :split_private_notes
44 44 after_create :send_notification
45 45
46 46 scope :visible, lambda {|*args|
47 47 user = args.shift || User.current
48 48 joins(:issue => :project).
49 49 where(Issue.visible_condition(user, *args)).
50 50 where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false)
51 51 }
52 52
53 53 def initialize(*args)
54 54 super
55 55 if journalized
56 56 if journalized.new_record?
57 57 self.notify = false
58 58 else
59 59 start
60 60 end
61 61 end
62 62 end
63 63
64 64 def save(*args)
65 65 journalize_changes
66 66 # Do not save an empty journal
67 67 (details.empty? && notes.blank?) ? false : super
68 68 end
69 69
70 70 # Returns journal details that are visible to user
71 71 def visible_details(user=User.current)
72 72 details.select do |detail|
73 73 if detail.property == 'cf'
74 74 detail.custom_field && detail.custom_field.visible_by?(project, user)
75 75 elsif detail.property == 'relation'
76 76 Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user)
77 77 else
78 78 true
79 79 end
80 80 end
81 81 end
82 82
83 83 def each_notification(users, &block)
84 84 if users.any?
85 85 users_by_details_visibility = users.group_by do |user|
86 86 visible_details(user)
87 87 end
88 88 users_by_details_visibility.each do |visible_details, users|
89 89 if notes? || visible_details.any?
90 90 yield(users)
91 91 end
92 92 end
93 93 end
94 94 end
95 95
96 96 # Returns the JournalDetail for the given attribute, or nil if the attribute
97 97 # was not updated
98 98 def detail_for_attribute(attribute)
99 99 details.detect {|detail| detail.prop_key == attribute}
100 100 end
101 101
102 102 # Returns the new status if the journal contains a status change, otherwise nil
103 103 def new_status
104 104 s = new_value_for('status_id')
105 105 s ? IssueStatus.find_by_id(s.to_i) : nil
106 106 end
107 107
108 108 def new_value_for(prop)
109 109 detail_for_attribute(prop).try(:value)
110 110 end
111 111
112 112 def editable_by?(usr)
113 113 usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
114 114 end
115 115
116 116 def project
117 117 journalized.respond_to?(:project) ? journalized.project : nil
118 118 end
119 119
120 120 def attachments
121 121 journalized.respond_to?(:attachments) ? journalized.attachments : nil
122 122 end
123 123
124 124 # Returns a string of css classes
125 125 def css_classes
126 126 s = 'journal'
127 127 s << ' has-notes' unless notes.blank?
128 128 s << ' has-details' unless details.blank?
129 129 s << ' private-notes' if private_notes?
130 130 s
131 131 end
132 132
133 133 def notify?
134 134 @notify != false
135 135 end
136 136
137 137 def notify=(arg)
138 138 @notify = arg
139 139 end
140 140
141 141 def notified_users
142 142 notified = journalized.notified_users
143 143 if private_notes?
144 144 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
145 145 end
146 146 notified
147 147 end
148 148
149 149 def recipients
150 150 notified_users.map(&:mail)
151 151 end
152 152
153 153 def notified_watchers
154 154 notified = journalized.notified_watchers
155 155 if private_notes?
156 156 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
157 157 end
158 158 notified
159 159 end
160 160
161 161 def watcher_recipients
162 162 notified_watchers.map(&:mail)
163 163 end
164 164
165 165 # Sets @custom_field instance variable on journals details using a single query
166 166 def self.preload_journals_details_custom_fields(journals)
167 167 field_ids = journals.map(&:details).flatten.select {|d| d.property == 'cf'}.map(&:prop_key).uniq
168 168 if field_ids.any?
169 169 fields_by_id = CustomField.where(:id => field_ids).inject({}) {|h, f| h[f.id] = f; h}
170 170 journals.each do |journal|
171 171 journal.details.each do |detail|
172 172 if detail.property == 'cf'
173 173 detail.instance_variable_set "@custom_field", fields_by_id[detail.prop_key.to_i]
174 174 end
175 175 end
176 176 end
177 177 end
178 178 journals
179 179 end
180 180
181 181 # Stores the values of the attributes and custom fields of the journalized object
182 182 def start
183 183 if journalized
184 184 @attributes_before_change = journalized.journalized_attribute_names.inject({}) do |h, attribute|
185 185 h[attribute] = journalized.send(attribute)
186 186 h
187 187 end
188 188 @custom_values_before_change = journalized.custom_field_values.inject({}) do |h, c|
189 189 h[c.custom_field_id] = c.value
190 190 h
191 191 end
192 192 end
193 193 self
194 194 end
195 195
196 196 # Adds a journal detail for an attachment that was added or removed
197 197 def journalize_attachment(attachment, added_or_removed)
198 198 key = (added_or_removed == :removed ? :old_value : :value)
199 199 details << JournalDetail.new(
200 200 :property => 'attachment',
201 201 :prop_key => attachment.id,
202 202 key => attachment.filename
203 203 )
204 204 end
205 205
206 206 # Adds a journal detail for an issue relation that was added or removed
207 207 def journalize_relation(relation, added_or_removed)
208 208 key = (added_or_removed == :removed ? :old_value : :value)
209 209 details << JournalDetail.new(
210 210 :property => 'relation',
211 211 :prop_key => relation.relation_type_for(journalized),
212 212 key => relation.other_issue(journalized).try(:id)
213 213 )
214 214 end
215 215
216 216 private
217 217
218 218 # Generates journal details for attribute and custom field changes
219 219 def journalize_changes
220 220 # attributes changes
221 221 if @attributes_before_change
222 222 journalized.journalized_attribute_names.each {|attribute|
223 223 before = @attributes_before_change[attribute]
224 224 after = journalized.send(attribute)
225 225 next if before == after || (before.blank? && after.blank?)
226 226 add_attribute_detail(attribute, before, after)
227 227 }
228 228 end
229 229 if @custom_values_before_change
230 230 # custom fields changes
231 231 journalized.custom_field_values.each {|c|
232 232 before = @custom_values_before_change[c.custom_field_id]
233 233 after = c.value
234 234 next if before == after || (before.blank? && after.blank?)
235 235
236 236 if before.is_a?(Array) || after.is_a?(Array)
237 237 before = [before] unless before.is_a?(Array)
238 238 after = [after] unless after.is_a?(Array)
239 239
240 240 # values removed
241 241 (before - after).reject(&:blank?).each do |value|
242 242 add_custom_value_detail(c, value, nil)
243 243 end
244 244 # values added
245 245 (after - before).reject(&:blank?).each do |value|
246 246 add_custom_value_detail(c, nil, value)
247 247 end
248 248 else
249 249 add_custom_value_detail(c, before, after)
250 250 end
251 251 }
252 252 end
253 253 start
254 254 end
255 255
256 256 # Adds a journal detail for an attribute change
257 257 def add_attribute_detail(attribute, old_value, value)
258 258 add_detail('attr', attribute, old_value, value)
259 259 end
260 260
261 261 # Adds a journal detail for a custom field value change
262 262 def add_custom_value_detail(custom_value, old_value, value)
263 263 add_detail('cf', custom_value.custom_field_id, old_value, value)
264 264 end
265 265
266 266 # Adds a journal detail
267 267 def add_detail(property, prop_key, old_value, value)
268 268 details << JournalDetail.new(
269 269 :property => property,
270 270 :prop_key => prop_key,
271 271 :old_value => old_value,
272 272 :value => value
273 273 )
274 274 end
275 275
276 276 def split_private_notes
277 277 if private_notes?
278 278 if notes.present?
279 279 if details.any?
280 280 # Split the journal (notes/changes) so we don't have half-private journals
281 281 journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
282 282 journal.details = details
283 283 journal.save
284 284 self.details = []
285 285 self.created_on = journal.created_on
286 286 end
287 287 else
288 288 # Blank notes should not be private
289 289 self.private_notes = false
290 290 end
291 291 end
292 292 true
293 293 end
294 294
295 295 def send_notification
296 296 if notify? && (Setting.notified_events.include?('issue_updated') ||
297 297 (Setting.notified_events.include?('issue_note_added') && notes.present?) ||
298 298 (Setting.notified_events.include?('issue_status_updated') && new_status.present?) ||
299 299 (Setting.notified_events.include?('issue_assigned_to_updated') && detail_for_attribute('assigned_to_id').present?) ||
300 300 (Setting.notified_events.include?('issue_priority_updated') && new_value_for('priority_id').present?)
301 301 )
302 302 Mailer.deliver_issue_edit(self)
303 303 end
304 304 end
305 305 end
@@ -1,189 +1,197
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class ActivityTest < ActiveSupport::TestCase
21 21 fixtures :projects, :versions, :attachments, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
22 22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, :time_entries,
23 23 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
24 24
25 25 def setup
26 26 @project = Project.find(1)
27 27 end
28 28
29 29 def test_activity_without_subprojects
30 30 events = find_events(User.anonymous, :project => @project)
31 31 assert_not_nil events
32 32
33 33 assert events.include?(Issue.find(1))
34 34 assert !events.include?(Issue.find(4))
35 35 # subproject issue
36 36 assert !events.include?(Issue.find(5))
37 37 end
38 38
39 39 def test_activity_with_subprojects
40 40 events = find_events(User.anonymous, :project => @project, :with_subprojects => 1)
41 41 assert_not_nil events
42 42
43 43 assert events.include?(Issue.find(1))
44 44 # subproject issue
45 45 assert events.include?(Issue.find(5))
46 46 end
47 47
48 48 def test_global_activity_anonymous
49 49 events = find_events(User.anonymous)
50 50 assert_not_nil events
51 51
52 52 assert events.include?(Issue.find(1))
53 53 assert events.include?(Message.find(5))
54 54 # Issue of a private project
55 55 assert !events.include?(Issue.find(4))
56 56 # Private issue and comment
57 57 assert !events.include?(Issue.find(14))
58 58 assert !events.include?(Journal.find(5))
59 59 end
60 60
61 61 def test_global_activity_logged_user
62 62 events = find_events(User.find(2)) # manager
63 63 assert_not_nil events
64 64
65 65 assert events.include?(Issue.find(1))
66 66 # Issue of a private project the user belongs to
67 67 assert events.include?(Issue.find(4))
68 68 end
69 69
70 70 def test_user_activity
71 71 user = User.find(2)
72 72 events = Redmine::Activity::Fetcher.new(User.anonymous, :author => user).events(nil, nil, :limit => 10)
73 73
74 74 assert(events.size > 0)
75 75 assert(events.size <= 10)
76 76 assert_nil(events.detect {|e| e.event_author != user})
77 77 end
78 78
79 def test_journal_with_notes_and_changes_should_be_returned_once
80 f = Redmine::Activity::Fetcher.new(User.anonymous, :project => Project.find(1))
81 f.scope = ['issues']
82 events = f.events
83
84 assert_equal events, events.uniq
85 end
86
79 87 def test_files_activity
80 88 f = Redmine::Activity::Fetcher.new(User.anonymous, :project => Project.find(1))
81 89 f.scope = ['files']
82 90 events = f.events
83 91
84 92 assert_kind_of Array, events
85 93 assert events.include?(Attachment.find_by_container_type_and_container_id('Project', 1))
86 94 assert events.include?(Attachment.find_by_container_type_and_container_id('Version', 1))
87 95 assert_equal [Attachment], events.collect(&:class).uniq
88 96 assert_equal %w(Project Version), events.collect(&:container_type).uniq.sort
89 97 end
90 98
91 99 def test_event_group_for_issue
92 100 issue = Issue.find(1)
93 101 assert_equal issue, issue.event_group
94 102 end
95 103
96 104 def test_event_group_for_journal
97 105 issue = Issue.find(1)
98 106 journal = issue.journals.first
99 107 assert_equal issue, journal.event_group
100 108 end
101 109
102 110 def test_event_group_for_issue_time_entry
103 111 time = TimeEntry.where(:issue_id => 1).first
104 112 assert_equal time.issue, time.event_group
105 113 end
106 114
107 115 def test_event_group_for_project_time_entry
108 116 time = TimeEntry.where(:issue_id => nil).first
109 117 assert_equal time, time.event_group
110 118 end
111 119
112 120 def test_event_group_for_message
113 121 message = Message.find(1)
114 122 reply = message.children.first
115 123 assert_equal message, message.event_group
116 124 assert_equal message, reply.event_group
117 125 end
118 126
119 127 def test_event_group_for_wiki_content_version
120 128 content = WikiContent::Version.find(1)
121 129 assert_equal content.page, content.event_group
122 130 end
123 131
124 132 class TestActivityProviderWithPermission
125 133 def self.activity_provider_options
126 134 {'test' => {:permission => :custom_permission}}
127 135 end
128 136 end
129 137
130 138 class TestActivityProviderWithNilPermission
131 139 def self.activity_provider_options
132 140 {'test' => {:permission => nil}}
133 141 end
134 142 end
135 143
136 144 class TestActivityProviderWithoutPermission
137 145 def self.activity_provider_options
138 146 {'test' => {}}
139 147 end
140 148 end
141 149
142 150 class MockUser
143 151 def initialize(*permissions)
144 152 @permissions = permissions
145 153 end
146 154
147 155 def allowed_to?(permission, *args)
148 156 @permissions.include?(permission)
149 157 end
150 158 end
151 159
152 160 def test_event_types_should_consider_activity_provider_permission
153 161 Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithPermission'
154 162 user = MockUser.new(:custom_permission)
155 163 f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
156 164 assert_include 'test', f.event_types
157 165 ensure
158 166 Redmine::Activity.delete 'test'
159 167 end
160 168
161 169 def test_event_types_should_include_activity_provider_with_nil_permission
162 170 Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithNilPermission'
163 171 user = MockUser.new()
164 172 f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
165 173 assert_include 'test', f.event_types
166 174 ensure
167 175 Redmine::Activity.delete 'test'
168 176 end
169 177
170 178 def test_event_types_should_use_default_permission_for_activity_provider_without_permission
171 179 Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithoutPermission'
172 180
173 181 user = MockUser.new()
174 182 f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
175 183 assert_not_include 'test', f.event_types
176 184
177 185 user = MockUser.new(:view_test)
178 186 f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
179 187 assert_include 'test', f.event_types
180 188 ensure
181 189 Redmine::Activity.delete 'test'
182 190 end
183 191
184 192 private
185 193
186 194 def find_events(user, options={})
187 195 Redmine::Activity::Fetcher.new(user, options).events(Date.today - 30, Date.today + 1)
188 196 end
189 197 end
General Comments 0
You need to be logged in to leave comments. Login now