]> git.openstreetmap.org Git - rails.git/blob - app/models/notifier.rb
Merge branch 'master' into moderation
[rails.git] / app / models / notifier.rb
1 class Notifier < ActionMailer::Base
2   default :from => EMAIL_FROM,
3           :return_path => EMAIL_RETURN_PATH,
4           :auto_submitted => "auto-generated"
5   helper :application
6   before_action :set_shared_template_vars
7   before_action :attach_project_logo
8
9   def signup_confirm(user, token)
10     with_recipient_locale user do
11       @url = url_for(:host => SERVER_URL,
12                      :controller => "user", :action => "confirm",
13                      :display_name => user.display_name,
14                      :confirm_string => token.token)
15
16       mail :to => user.email,
17            :subject => I18n.t("notifier.signup_confirm.subject")
18     end
19   end
20
21   def email_confirm(user, token)
22     with_recipient_locale user do
23       @address = user.new_email
24       @url = url_for(:host => SERVER_URL,
25                      :controller => "user", :action => "confirm_email",
26                      :confirm_string => token.token)
27
28       mail :to => user.new_email,
29            :subject => I18n.t("notifier.email_confirm.subject")
30     end
31   end
32
33   def lost_password(user, token)
34     with_recipient_locale user do
35       @url = url_for(:host => SERVER_URL,
36                      :controller => "user", :action => "reset_password",
37                      :token => token.token)
38
39       mail :to => user.email,
40            :subject => I18n.t("notifier.lost_password.subject")
41     end
42   end
43
44   def gpx_success(trace, possible_points)
45     with_recipient_locale trace.user do
46       @trace_name = trace.name
47       @trace_points = trace.size
48       @trace_description = trace.description
49       @trace_tags = trace.tags
50       @possible_points = possible_points
51
52       mail :to => trace.user.email,
53            :subject => I18n.t("notifier.gpx_notification.success.subject")
54     end
55   end
56
57   def gpx_failure(trace, error)
58     with_recipient_locale trace.user do
59       @trace_name = trace.name
60       @trace_description = trace.description
61       @trace_tags = trace.tags
62       @error = error
63
64       mail :to => trace.user.email,
65            :subject => I18n.t("notifier.gpx_notification.failure.subject")
66     end
67   end
68
69   def message_notification(message)
70     with_recipient_locale message.recipient do
71       @to_user = message.recipient.display_name
72       @from_user = message.sender.display_name
73       @text = message.body
74       @title = message.title
75       @readurl = url_for(:host => SERVER_URL,
76                          :controller => "message", :action => "read",
77                          :message_id => message.id)
78       @replyurl = url_for(:host => SERVER_URL,
79                           :controller => "message", :action => "reply",
80                           :message_id => message.id)
81       @author = @from_user
82
83       attach_user_avatar(message.sender)
84
85       mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
86            :to => message.recipient.email,
87            :subject => I18n.t("notifier.message_notification.subject_header", :subject => message.title)
88     end
89   end
90
91   def diary_comment_notification(comment, recipient)
92     with_recipient_locale recipient do
93       @to_user = recipient.display_name
94       @from_user = comment.user.display_name
95       @text = comment.body
96       @title = comment.diary_entry.title
97       @readurl = url_for(:host => SERVER_URL,
98                          :controller => "diary_entry",
99                          :action => "view",
100                          :display_name => comment.diary_entry.user.display_name,
101                          :id => comment.diary_entry.id,
102                          :anchor => "comment#{comment.id}")
103       @commenturl = url_for(:host => SERVER_URL,
104                             :controller => "diary_entry",
105                             :action => "view",
106                             :display_name => comment.diary_entry.user.display_name,
107                             :id => comment.diary_entry.id,
108                             :anchor => "newcomment")
109       @replyurl = url_for(:host => SERVER_URL,
110                           :controller => "message",
111                           :action => "new",
112                           :display_name => comment.user.display_name,
113                           :title => "Re: #{comment.diary_entry.title}")
114       @author = @from_user
115
116       attach_user_avatar(comment.user)
117
118       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
119            :to => recipient.email,
120            :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
121     end
122   end
123
124   def friend_notification(friend)
125     with_recipient_locale friend.befriendee do
126       @friend = friend
127       @viewurl = url_for(:host => SERVER_URL,
128                          :controller => "user", :action => "view",
129                          :display_name => @friend.befriender.display_name)
130       @friendurl = url_for(:host => SERVER_URL,
131                            :controller => "user", :action => "make_friend",
132                            :display_name => @friend.befriender.display_name)
133       @author = @friend.befriender.display_name
134
135       attach_user_avatar(@friend.befriender)
136       mail :to => friend.befriendee.email,
137            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
138     end
139   end
140
141   def note_comment_notification(comment, recipient)
142     with_recipient_locale recipient do
143       @noteurl = browse_note_url(comment.note, :host => SERVER_URL)
144       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
145       @comment = comment.body
146       @owner = recipient == comment.note.author
147       @event = comment.event
148
149       @commenter = if comment.author
150                      comment.author.display_name
151                    else
152                      I18n.t("notifier.note_comment_notification.anonymous")
153                    end
154
155       @author = @commenter
156       attach_user_avatar(comment.author)
157
158       subject = if @owner
159                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
160                 else
161                   I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
162                 end
163
164       mail :to => recipient.email, :subject => subject
165     end
166   end
167
168   def changeset_comment_notification(comment, recipient)
169     with_recipient_locale recipient do
170       @to_user = recipient.display_name
171       @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
172       @comment = comment.body
173       @owner = recipient == comment.changeset.user
174       @commenter = comment.author.display_name
175       @changeset_comment = comment.changeset.tags["comment"].presence
176       @time = comment.created_at
177       @changeset_author = comment.changeset.user.display_name
178       @author = @commenter
179
180       subject = if @owner
181                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
182                 else
183                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
184                 end
185
186       attach_user_avatar(comment.author)
187
188       mail :to => recipient.email, :subject => subject
189     end
190   end
191
192   def new_issue_notification(issue_id, recipient)
193     with_recipient_locale recipient do
194       @url = url_for(:host => SERVER_URL,
195                      :controller => "issues",
196                      :action => "show",
197                      :id => issue_id)
198       subject = I18n.t("notifier.new_issue_notification.subject")
199       mail :to => recipient.email, :subject => subject
200     end
201   end
202
203   private
204
205   def set_shared_template_vars
206     @root_url = root_url(:host => SERVER_URL)
207   end
208
209   def attach_project_logo
210     attachments.inline["logo.png"] = File.read(Rails.root.join("app", "assets", "images", "osm_logo_30.png"))
211   end
212
213   def attach_user_avatar(user)
214     attachments.inline["avatar.png"] = File.read(user_avatar_file_path(user))
215   end
216
217   def user_avatar_file_path(user)
218     image = user && user.image
219     if image && image.file?
220       return image.path(:small)
221     else
222       return Rails.root.join("app", "assets", "images", "users", "images", "small.png")
223     end
224   end
225
226   def with_recipient_locale(recipient)
227     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
228       yield
229     end
230   end
231
232   def from_address(name, type, id, digest, user_id = nil)
233     if Object.const_defined?(:MESSAGES_DOMAIN) && domain = MESSAGES_DOMAIN
234       if user_id
235         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
236       else
237         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
238       end
239     else
240       EMAIL_FROM
241     end
242   end
243 end