##// END OF EJS Templates
Use scopes instead of ARCondition....
Jean-Philippe Lang -
r7967:0a92e382fa18
parent child
Show More
@@ -1,475 +1,474
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 'ar_condition'
19 19
20 20 class Mailer < ActionMailer::Base
21 21 layout 'mailer'
22 22 helper :application
23 23 helper :issues
24 24 helper :custom_fields
25 25
26 26 include ActionController::UrlWriter
27 27 include Redmine::I18n
28 28
29 29 def self.default_url_options
30 30 h = Setting.host_name
31 31 h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank?
32 32 { :host => h, :protocol => Setting.protocol }
33 33 end
34 34
35 35 # Builds a tmail object used to email recipients of the added issue.
36 36 #
37 37 # Example:
38 38 # issue_add(issue) => tmail object
39 39 # Mailer.deliver_issue_add(issue) => sends an email to issue recipients
40 40 def issue_add(issue)
41 41 redmine_headers 'Project' => issue.project.identifier,
42 42 'Issue-Id' => issue.id,
43 43 'Issue-Author' => issue.author.login
44 44 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
45 45 message_id issue
46 46 recipients issue.recipients
47 47 cc(issue.watcher_recipients - @recipients)
48 48 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
49 49 body :issue => issue,
50 50 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
51 51 render_multipart('issue_add', body)
52 52 end
53 53
54 54 # Builds a tmail object used to email recipients of the edited issue.
55 55 #
56 56 # Example:
57 57 # issue_edit(journal) => tmail object
58 58 # Mailer.deliver_issue_edit(journal) => sends an email to issue recipients
59 59 def issue_edit(journal)
60 60 issue = journal.journalized.reload
61 61 redmine_headers 'Project' => issue.project.identifier,
62 62 'Issue-Id' => issue.id,
63 63 'Issue-Author' => issue.author.login
64 64 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
65 65 message_id journal
66 66 references issue
67 67 @author = journal.user
68 68 recipients issue.recipients
69 69 # Watchers in cc
70 70 cc(issue.watcher_recipients - @recipients)
71 71 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
72 72 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
73 73 s << issue.subject
74 74 subject s
75 75 body :issue => issue,
76 76 :journal => journal,
77 77 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
78 78
79 79 render_multipart('issue_edit', body)
80 80 end
81 81
82 82 def reminder(user, issues, days)
83 83 set_language_if_valid user.language
84 84 recipients user.mail
85 85 subject l(:mail_subject_reminder, :count => issues.size, :days => days)
86 86 body :issues => issues,
87 87 :days => days,
88 88 :issues_url => url_for(:controller => 'issues', :action => 'index',
89 89 :set_filter => 1, :assigned_to_id => user.id,
90 90 :sort => 'due_date:asc')
91 91 render_multipart('reminder', body)
92 92 end
93 93
94 94 # Builds a tmail object used to email users belonging to the added document's project.
95 95 #
96 96 # Example:
97 97 # document_added(document) => tmail object
98 98 # Mailer.deliver_document_added(document) => sends an email to the document's project recipients
99 99 def document_added(document)
100 100 redmine_headers 'Project' => document.project.identifier
101 101 recipients document.recipients
102 102 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
103 103 body :document => document,
104 104 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
105 105 render_multipart('document_added', body)
106 106 end
107 107
108 108 # Builds a tmail object used to email recipients of a project when an attachements are added.
109 109 #
110 110 # Example:
111 111 # attachments_added(attachments) => tmail object
112 112 # Mailer.deliver_attachments_added(attachments) => sends an email to the project's recipients
113 113 def attachments_added(attachments)
114 114 container = attachments.first.container
115 115 added_to = ''
116 116 added_to_url = ''
117 117 case container.class.name
118 118 when 'Project'
119 119 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
120 120 added_to = "#{l(:label_project)}: #{container}"
121 121 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
122 122 when 'Version'
123 123 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
124 124 added_to = "#{l(:label_version)}: #{container.name}"
125 125 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
126 126 when 'Document'
127 127 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
128 128 added_to = "#{l(:label_document)}: #{container.title}"
129 129 recipients container.recipients
130 130 end
131 131 redmine_headers 'Project' => container.project.identifier
132 132 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
133 133 body :attachments => attachments,
134 134 :added_to => added_to,
135 135 :added_to_url => added_to_url
136 136 render_multipart('attachments_added', body)
137 137 end
138 138
139 139 # Builds a tmail object used to email recipients of a news' project when a news item is added.
140 140 #
141 141 # Example:
142 142 # news_added(news) => tmail object
143 143 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients
144 144 def news_added(news)
145 145 redmine_headers 'Project' => news.project.identifier
146 146 message_id news
147 147 recipients news.recipients
148 148 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
149 149 body :news => news,
150 150 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
151 151 render_multipart('news_added', body)
152 152 end
153 153
154 154 # Builds a tmail object used to email recipients of a news' project when a news comment is added.
155 155 #
156 156 # Example:
157 157 # news_comment_added(comment) => tmail object
158 158 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
159 159 def news_comment_added(comment)
160 160 news = comment.commented
161 161 redmine_headers 'Project' => news.project.identifier
162 162 message_id comment
163 163 recipients news.recipients
164 164 cc news.watcher_recipients
165 165 subject "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
166 166 body :news => news,
167 167 :comment => comment,
168 168 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
169 169 render_multipart('news_comment_added', body)
170 170 end
171 171
172 172 # Builds a tmail object used to email the recipients of the specified message that was posted.
173 173 #
174 174 # Example:
175 175 # message_posted(message) => tmail object
176 176 # Mailer.deliver_message_posted(message) => sends an email to the recipients
177 177 def message_posted(message)
178 178 redmine_headers 'Project' => message.project.identifier,
179 179 'Topic-Id' => (message.parent_id || message.id)
180 180 message_id message
181 181 references message.parent unless message.parent.nil?
182 182 recipients(message.recipients)
183 183 cc((message.root.watcher_recipients + message.board.watcher_recipients).uniq - @recipients)
184 184 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
185 185 body :message => message,
186 186 :message_url => url_for(message.event_url)
187 187 render_multipart('message_posted', body)
188 188 end
189 189
190 190 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added.
191 191 #
192 192 # Example:
193 193 # wiki_content_added(wiki_content) => tmail object
194 194 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients
195 195 def wiki_content_added(wiki_content)
196 196 redmine_headers 'Project' => wiki_content.project.identifier,
197 197 'Wiki-Page-Id' => wiki_content.page.id
198 198 message_id wiki_content
199 199 recipients wiki_content.recipients
200 200 cc(wiki_content.page.wiki.watcher_recipients - recipients)
201 201 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
202 202 body :wiki_content => wiki_content,
203 203 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show',
204 204 :project_id => wiki_content.project,
205 205 :id => wiki_content.page.title)
206 206 render_multipart('wiki_content_added', body)
207 207 end
208 208
209 209 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated.
210 210 #
211 211 # Example:
212 212 # wiki_content_updated(wiki_content) => tmail object
213 213 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients
214 214 def wiki_content_updated(wiki_content)
215 215 redmine_headers 'Project' => wiki_content.project.identifier,
216 216 'Wiki-Page-Id' => wiki_content.page.id
217 217 message_id wiki_content
218 218 recipients wiki_content.recipients
219 219 cc(wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients)
220 220 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
221 221 body :wiki_content => wiki_content,
222 222 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show',
223 223 :project_id => wiki_content.project,
224 224 :id => wiki_content.page.title),
225 225 :wiki_diff_url => url_for(:controller => 'wiki', :action => 'diff',
226 226 :project_id => wiki_content.project, :id => wiki_content.page.title,
227 227 :version => wiki_content.version)
228 228 render_multipart('wiki_content_updated', body)
229 229 end
230 230
231 231 # Builds a tmail object used to email the specified user their account information.
232 232 #
233 233 # Example:
234 234 # account_information(user, password) => tmail object
235 235 # Mailer.deliver_account_information(user, password) => sends account information to the user
236 236 def account_information(user, password)
237 237 set_language_if_valid user.language
238 238 recipients user.mail
239 239 subject l(:mail_subject_register, Setting.app_title)
240 240 body :user => user,
241 241 :password => password,
242 242 :login_url => url_for(:controller => 'account', :action => 'login')
243 243 render_multipart('account_information', body)
244 244 end
245 245
246 246 # Builds a tmail object used to email all active administrators of an account activation request.
247 247 #
248 248 # Example:
249 249 # account_activation_request(user) => tmail object
250 250 # Mailer.deliver_account_activation_request(user)=> sends an email to all active administrators
251 251 def account_activation_request(user)
252 252 # Send the email to all active administrators
253 253 recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
254 254 subject l(:mail_subject_account_activation_request, Setting.app_title)
255 255 body :user => user,
256 256 :url => url_for(:controller => 'users', :action => 'index',
257 257 :status => User::STATUS_REGISTERED,
258 258 :sort_key => 'created_on', :sort_order => 'desc')
259 259 render_multipart('account_activation_request', body)
260 260 end
261 261
262 262 # Builds a tmail object used to email the specified user that their account was activated by an administrator.
263 263 #
264 264 # Example:
265 265 # account_activated(user) => tmail object
266 266 # Mailer.deliver_account_activated(user) => sends an email to the registered user
267 267 def account_activated(user)
268 268 set_language_if_valid user.language
269 269 recipients user.mail
270 270 subject l(:mail_subject_register, Setting.app_title)
271 271 body :user => user,
272 272 :login_url => url_for(:controller => 'account', :action => 'login')
273 273 render_multipart('account_activated', body)
274 274 end
275 275
276 276 def lost_password(token)
277 277 set_language_if_valid(token.user.language)
278 278 recipients token.user.mail
279 279 subject l(:mail_subject_lost_password, Setting.app_title)
280 280 body :token => token,
281 281 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
282 282 render_multipart('lost_password', body)
283 283 end
284 284
285 285 def register(token)
286 286 set_language_if_valid(token.user.language)
287 287 recipients token.user.mail
288 288 subject l(:mail_subject_register, Setting.app_title)
289 289 body :token => token,
290 290 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
291 291 render_multipart('register', body)
292 292 end
293 293
294 294 def test(user)
295 295 set_language_if_valid(user.language)
296 296 recipients user.mail
297 297 subject 'Redmine test'
298 298 body :url => url_for(:controller => 'welcome')
299 299 render_multipart('test', body)
300 300 end
301 301
302 302 # Overrides default deliver! method to prevent from sending an email
303 303 # with no recipient, cc or bcc
304 304 def deliver!(mail = @mail)
305 305 set_language_if_valid @initial_language
306 306 return false if (recipients.nil? || recipients.empty?) &&
307 307 (cc.nil? || cc.empty?) &&
308 308 (bcc.nil? || bcc.empty?)
309 309
310 310 # Set Message-Id and References
311 311 if @message_id_object
312 312 mail.message_id = self.class.message_id_for(@message_id_object)
313 313 end
314 314 if @references_objects
315 315 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
316 316 end
317 317
318 318 # Log errors when raise_delivery_errors is set to false, Rails does not
319 319 raise_errors = self.class.raise_delivery_errors
320 320 self.class.raise_delivery_errors = true
321 321 begin
322 322 return super(mail)
323 323 rescue Exception => e
324 324 if raise_errors
325 325 raise e
326 326 elsif mylogger
327 327 mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/configuration.yml."
328 328 end
329 329 ensure
330 330 self.class.raise_delivery_errors = raise_errors
331 331 end
332 332 end
333 333
334 334 # Sends reminders to issue assignees
335 335 # Available options:
336 336 # * :days => how many days in the future to remind about (defaults to 7)
337 337 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
338 338 # * :project => id or identifier of project to process (defaults to all projects)
339 339 # * :users => array of user ids who should be reminded
340 340 def self.reminders(options={})
341 341 days = options[:days] || 7
342 342 project = options[:project] ? Project.find(options[:project]) : nil
343 343 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
344 344 user_ids = options[:users]
345 345
346 s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date]
347 s << "#{Issue.table_name}.assigned_to_id IS NOT NULL"
348 s << ["#{Issue.table_name}.assigned_to_id IN (?)", user_ids] if user_ids.present?
349 s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}"
350 s << "#{Issue.table_name}.project_id = #{project.id}" if project
351 s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker
352
353 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker],
354 :conditions => s.conditions
355 ).group_by(&:assigned_to)
346 scope = Issue.scoped(:conditions => ["#{Issue.table_name}.assigned_to_id IS NOT NULL" +
347 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
348 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date]
349 )
350 scope = scope.scoped(:conditions => {:assigned_to_id => user_ids}) if user_ids.present?
351 scope = scope.scoped(:conditions => {:project_id => project.id}) if project
352 scope = scope.scoped(:conditions => {:tracker_id => tracker.id}) if tracker
353
354 issues_by_assignee = scope.all(:include => [:status, :assigned_to, :project, :tracker]).group_by(&:assigned_to)
356 355 issues_by_assignee.each do |assignee, issues|
357 356 deliver_reminder(assignee, issues, days) if assignee && assignee.active?
358 357 end
359 358 end
360 359
361 360 # Activates/desactivates email deliveries during +block+
362 361 def self.with_deliveries(enabled = true, &block)
363 362 was_enabled = ActionMailer::Base.perform_deliveries
364 363 ActionMailer::Base.perform_deliveries = !!enabled
365 364 yield
366 365 ensure
367 366 ActionMailer::Base.perform_deliveries = was_enabled
368 367 end
369 368
370 369 private
371 370 def initialize_defaults(method_name)
372 371 super
373 372 @initial_language = current_language
374 373 set_language_if_valid Setting.default_language
375 374 from Setting.mail_from
376 375
377 376 # Common headers
378 377 headers 'X-Mailer' => 'Redmine',
379 378 'X-Redmine-Host' => Setting.host_name,
380 379 'X-Redmine-Site' => Setting.app_title,
381 380 'X-Auto-Response-Suppress' => 'OOF',
382 381 'Auto-Submitted' => 'auto-generated'
383 382 end
384 383
385 384 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
386 385 def redmine_headers(h)
387 386 h.each { |k,v| headers["X-Redmine-#{k}"] = v }
388 387 end
389 388
390 389 # Overrides the create_mail method
391 390 def create_mail
392 391 # Removes the current user from the recipients and cc
393 392 # if he doesn't want to receive notifications about what he does
394 393 @author ||= User.current
395 394 if @author.pref[:no_self_notified]
396 395 recipients.delete(@author.mail) if recipients
397 396 cc.delete(@author.mail) if cc
398 397 end
399 398
400 399 notified_users = [recipients, cc].flatten.compact.uniq
401 400 # Rails would log recipients only, not cc and bcc
402 401 mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger
403 402
404 403 # Blind carbon copy recipients
405 404 if Setting.bcc_recipients?
406 405 bcc(notified_users)
407 406 recipients []
408 407 cc []
409 408 end
410 409 super
411 410 end
412 411
413 412 # Rails 2.3 has problems rendering implicit multipart messages with
414 413 # layouts so this method will wrap an multipart messages with
415 414 # explicit parts.
416 415 #
417 416 # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type
418 417 # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts
419 418
420 419 def render_multipart(method_name, body)
421 420 if Setting.plain_text_mail?
422 421 content_type "text/plain"
423 422 body render(:file => "#{method_name}.text.erb",
424 423 :body => body,
425 424 :layout => 'mailer.text.erb')
426 425 else
427 426 content_type "multipart/alternative"
428 427 part :content_type => "text/plain",
429 428 :body => render(:file => "#{method_name}.text.erb",
430 429 :body => body, :layout => 'mailer.text.erb')
431 430 part :content_type => "text/html",
432 431 :body => render_message("#{method_name}.html.erb", body)
433 432 end
434 433 end
435 434
436 435 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
437 436 def self.controller_path
438 437 ''
439 438 end unless respond_to?('controller_path')
440 439
441 440 # Returns a predictable Message-Id for the given object
442 441 def self.message_id_for(object)
443 442 # id + timestamp should reduce the odds of a collision
444 443 # as far as we don't send multiple emails for the same object
445 444 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
446 445 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}"
447 446 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
448 447 host = "#{::Socket.gethostname}.redmine" if host.empty?
449 448 "<#{hash}@#{host}>"
450 449 end
451 450
452 451 private
453 452
454 453 def message_id(object)
455 454 @message_id_object = object
456 455 end
457 456
458 457 def references(object)
459 458 @references_objects ||= []
460 459 @references_objects << object
461 460 end
462 461
463 462 def mylogger
464 463 Rails.logger
465 464 end
466 465 end
467 466
468 467 # Patch TMail so that message_id is not overwritten
469 468 module TMail
470 469 class Mail
471 470 def add_message_id( fqdn = nil )
472 471 self.message_id ||= ::TMail::new_message_id(fqdn)
473 472 end
474 473 end
475 474 end
General Comments 0
You need to be logged in to leave comments. Login now