##// END OF EJS Templates
Fixed: email notification for changes I make still occurs when running Repository.fetch_changesets (#1957)....
Jean-Philippe Lang -
r2223:dfc937340d6b
parent child
Show More
@@ -1,251 +1,253
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 Mailer < ActionMailer::Base
19 19 helper :application
20 20 helper :issues
21 21 helper :custom_fields
22 22
23 23 include ActionController::UrlWriter
24 24
25 25 def issue_add(issue)
26 26 redmine_headers 'Project' => issue.project.identifier,
27 27 'Issue-Id' => issue.id,
28 28 'Issue-Author' => issue.author.login
29 29 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
30 30 recipients issue.recipients
31 31 cc(issue.watcher_recipients - @recipients)
32 32 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
33 33 body :issue => issue,
34 34 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
35 35 end
36 36
37 37 def issue_edit(journal)
38 38 issue = journal.journalized
39 39 redmine_headers 'Project' => issue.project.identifier,
40 40 'Issue-Id' => issue.id,
41 41 'Issue-Author' => issue.author.login
42 42 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
43 @author = journal.user
43 44 recipients issue.recipients
44 45 # Watchers in cc
45 46 cc(issue.watcher_recipients - @recipients)
46 47 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
47 48 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
48 49 s << issue.subject
49 50 subject s
50 51 body :issue => issue,
51 52 :journal => journal,
52 53 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
53 54 end
54 55
55 56 def reminder(user, issues, days)
56 57 set_language_if_valid user.language
57 58 recipients user.mail
58 59 subject l(:mail_subject_reminder, issues.size)
59 60 body :issues => issues,
60 61 :days => days,
61 62 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc')
62 63 end
63 64
64 65 def document_added(document)
65 66 redmine_headers 'Project' => document.project.identifier
66 67 recipients document.project.recipients
67 68 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
68 69 body :document => document,
69 70 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
70 71 end
71 72
72 73 def attachments_added(attachments)
73 74 container = attachments.first.container
74 75 added_to = ''
75 76 added_to_url = ''
76 77 case container.class.name
77 78 when 'Project'
78 79 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container)
79 80 added_to = "#{l(:label_project)}: #{container}"
80 81 when 'Version'
81 82 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
82 83 added_to = "#{l(:label_version)}: #{container.name}"
83 84 when 'Document'
84 85 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
85 86 added_to = "#{l(:label_document)}: #{container.title}"
86 87 end
87 88 redmine_headers 'Project' => container.project.identifier
88 89 recipients container.project.recipients
89 90 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
90 91 body :attachments => attachments,
91 92 :added_to => added_to,
92 93 :added_to_url => added_to_url
93 94 end
94 95
95 96 def news_added(news)
96 97 redmine_headers 'Project' => news.project.identifier
97 98 recipients news.project.recipients
98 99 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
99 100 body :news => news,
100 101 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
101 102 end
102 103
103 104 def message_posted(message, recipients)
104 105 redmine_headers 'Project' => message.project.identifier,
105 106 'Topic-Id' => (message.parent_id || message.id)
106 107 recipients(recipients)
107 108 subject "[#{message.board.project.name} - #{message.board.name}] #{message.subject}"
108 109 body :message => message,
109 110 :message_url => url_for(:controller => 'messages', :action => 'show', :board_id => message.board_id, :id => message.root)
110 111 end
111 112
112 113 def account_information(user, password)
113 114 set_language_if_valid user.language
114 115 recipients user.mail
115 116 subject l(:mail_subject_register, Setting.app_title)
116 117 body :user => user,
117 118 :password => password,
118 119 :login_url => url_for(:controller => 'account', :action => 'login')
119 120 end
120 121
121 122 def account_activation_request(user)
122 123 # Send the email to all active administrators
123 124 recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
124 125 subject l(:mail_subject_account_activation_request, Setting.app_title)
125 126 body :user => user,
126 127 :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc')
127 128 end
128 129
129 130 def lost_password(token)
130 131 set_language_if_valid(token.user.language)
131 132 recipients token.user.mail
132 133 subject l(:mail_subject_lost_password, Setting.app_title)
133 134 body :token => token,
134 135 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
135 136 end
136 137
137 138 def register(token)
138 139 set_language_if_valid(token.user.language)
139 140 recipients token.user.mail
140 141 subject l(:mail_subject_register, Setting.app_title)
141 142 body :token => token,
142 143 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
143 144 end
144 145
145 146 def test(user)
146 147 set_language_if_valid(user.language)
147 148 recipients user.mail
148 149 subject 'Redmine test'
149 150 body :url => url_for(:controller => 'welcome')
150 151 end
151 152
152 153 # Overrides default deliver! method to prevent from sending an email
153 154 # with no recipient, cc or bcc
154 155 def deliver!(mail = @mail)
155 156 return false if (recipients.nil? || recipients.empty?) &&
156 157 (cc.nil? || cc.empty?) &&
157 158 (bcc.nil? || bcc.empty?)
158 159 super
159 160 end
160 161
161 162 # Sends reminders to issue assignees
162 163 # Available options:
163 164 # * :days => how many days in the future to remind about (defaults to 7)
164 165 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
165 166 # * :project => id or identifier of project to process (defaults to all projects)
166 167 def self.reminders(options={})
167 168 days = options[:days] || 7
168 169 project = options[:project] ? Project.find(options[:project]) : nil
169 170 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
170 171
171 172 s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date]
172 173 s << "#{Issue.table_name}.assigned_to_id IS NOT NULL"
173 174 s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}"
174 175 s << "#{Issue.table_name}.project_id = #{project.id}" if project
175 176 s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker
176 177
177 178 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker],
178 179 :conditions => s.conditions
179 180 ).group_by(&:assigned_to)
180 181 issues_by_assignee.each do |assignee, issues|
181 182 deliver_reminder(assignee, issues, days) unless assignee.nil?
182 183 end
183 184 end
184 185
185 186 private
186 187 def initialize_defaults(method_name)
187 188 super
188 189 set_language_if_valid Setting.default_language
189 190 from Setting.mail_from
190 191
191 192 # URL options
192 193 h = Setting.host_name
193 194 h = h.to_s.gsub(%r{\/.*$}, '') unless ActionController::AbstractRequest.relative_url_root.blank?
194 195 default_url_options[:host] = h
195 196 default_url_options[:protocol] = Setting.protocol
196 197
197 198 # Common headers
198 199 headers 'X-Mailer' => 'Redmine',
199 200 'X-Redmine-Host' => Setting.host_name,
200 201 'X-Redmine-Site' => Setting.app_title
201 202 end
202 203
203 204 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
204 205 def redmine_headers(h)
205 206 h.each { |k,v| headers["X-Redmine-#{k}"] = v }
206 207 end
207 208
208 209 # Overrides the create_mail method
209 210 def create_mail
210 211 # Removes the current user from the recipients and cc
211 212 # if he doesn't want to receive notifications about what he does
212 if User.current.pref[:no_self_notified]
213 recipients.delete(User.current.mail) if recipients
214 cc.delete(User.current.mail) if cc
213 @author ||= User.current
214 if @author.pref[:no_self_notified]
215 recipients.delete(@author.mail) if recipients
216 cc.delete(@author.mail) if cc
215 217 end
216 218 # Blind carbon copy recipients
217 219 if Setting.bcc_recipients?
218 220 bcc([recipients, cc].flatten.compact.uniq)
219 221 recipients []
220 222 cc []
221 223 end
222 224 super
223 225 end
224 226
225 227 # Renders a message with the corresponding layout
226 228 def render_message(method_name, body)
227 229 layout = method_name.match(%r{text\.html\.(rhtml|rxml)}) ? 'layout.text.html.rhtml' : 'layout.text.plain.rhtml'
228 230 body[:content_for_layout] = render(:file => method_name, :body => body)
229 231 ActionView::Base.new(template_root, body, self).render(:file => "mailer/#{layout}", :use_full_path => true)
230 232 end
231 233
232 234 # for the case of plain text only
233 235 def body(*params)
234 236 value = super(*params)
235 237 if Setting.plain_text_mail?
236 238 templates = Dir.glob("#{template_path}/#{@template}.text.plain.{rhtml,erb}")
237 239 unless String === @body or templates.empty?
238 240 template = File.basename(templates.first)
239 241 @body[:content_for_layout] = render(:file => template, :body => @body)
240 242 @body = ActionView::Base.new(template_root, @body, self).render(:file => "mailer/layout.text.plain.rhtml", :use_full_path => true)
241 243 return @body
242 244 end
243 245 end
244 246 return value
245 247 end
246 248
247 249 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
248 250 def self.controller_path
249 251 ''
250 252 end unless respond_to?('controller_path')
251 253 end
General Comments 0
You need to be logged in to leave comments. Login now