]> git.openstreetmap.org Git - rails.git/blob - app/mailers/user_mailer.rb
Add standard List-XXX headers to some notification messages
[rails.git] / app / mailers / user_mailer.rb
1 class UserMailer < ApplicationMailer
2   include ActionView::Helpers::AssetUrlHelper
3
4   self.delivery_job = ActionMailer::MailDeliveryJob
5
6   default :from => Settings.email_from,
7           :return_path => Settings.email_return_path,
8           :auto_submitted => "auto-generated"
9   helper :application
10   before_action :set_shared_template_vars
11   before_action :attach_project_logo
12
13   def signup_confirm(user, token)
14     with_recipient_locale user do
15       @url = url_for(:controller => "confirmations", :action => "confirm",
16                      :display_name => user.display_name,
17                      :confirm_string => token.token)
18
19       mail :to => user.email,
20            :subject => t(".subject")
21     end
22   end
23
24   def email_confirm(user, token)
25     with_recipient_locale user do
26       @address = user.new_email
27       @url = url_for(:controller => "confirmations", :action => "confirm_email",
28                      :confirm_string => token.token)
29
30       mail :to => user.new_email,
31            :subject => t(".subject")
32     end
33   end
34
35   def lost_password(user, token)
36     with_recipient_locale user do
37       @url = user_reset_password_url(:token => token.token)
38
39       mail :to => user.email,
40            :subject => t(".subject")
41     end
42   end
43
44   def gpx_success(trace, possible_points)
45     with_recipient_locale trace.user do
46       @to_user = trace.user.display_name
47       @trace_url = show_trace_url(trace.user, trace)
48       @trace_name = trace.name
49       @trace_points = trace.size
50       @trace_description = trace.description
51       @trace_tags = trace.tags
52       @possible_points = possible_points
53       @my_traces_url = url_for(:controller => "traces", :action => "mine")
54
55       mail :to => trace.user.email,
56            :subject => t(".subject")
57     end
58   end
59
60   def gpx_failure(trace, error)
61     with_recipient_locale trace.user do
62       @to_user = trace.user.display_name
63       @trace_name = trace.name
64       @trace_description = trace.description
65       @trace_tags = trace.tags
66       @error = error
67
68       mail :to => trace.user.email,
69            :subject => t(".subject")
70     end
71   end
72
73   def message_notification(message)
74     with_recipient_locale message.recipient do
75       @to_user = message.recipient.display_name
76       @from_user = message.sender.display_name
77       @text = message.body
78       @title = message.title
79       @readurl = message_url(message)
80       @replyurl = message_reply_url(message)
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.notification_token),
86            :to => message.recipient.email,
87            :subject => t(".subject", :message_title => 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 = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "comment#{comment.id}")
98       @commenturl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "newcomment")
99       @replyurl = new_message_url(comment.user, :message => { :title => "Re: #{comment.diary_entry.title}" })
100       @unsubscribeurl = diary_entry_unsubscribe_url(comment.diary_entry.user, comment.diary_entry)
101       @author = @from_user
102
103       attach_user_avatar(comment.user)
104
105       set_references("diary", comment.diary_entry)
106
107       set_list_headers(
108         "#{comment.diary_entry.id}.diary.www.openstreetmap.org",
109         t(".description", :id => comment.diary_entry.id),
110         :archive => @readurl,
111         :subscribe => diary_entry_subscribe_url(comment.diary_entry.user, comment.diary_entry),
112         :unsubscribe => @unsubscribeurl
113       )
114
115       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.notification_token(recipient.id), recipient.id),
116            :to => recipient.email,
117            :subject => t(".subject", :user => comment.user.display_name)
118     end
119   end
120
121   def friendship_notification(friendship)
122     with_recipient_locale friendship.befriendee do
123       @friendship = friendship
124       @viewurl = user_url(@friendship.befriender)
125       @friendurl = make_friend_url(@friendship.befriender)
126       @author = @friendship.befriender.display_name
127
128       attach_user_avatar(@friendship.befriender)
129       mail :to => friendship.befriendee.email,
130            :subject => t(".subject", :user => friendship.befriender.display_name)
131     end
132   end
133
134   def note_comment_notification(comment, recipient)
135     with_recipient_locale recipient do
136       @noteurl = note_url(comment.note)
137       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
138       @comment = comment.body
139       @owner = recipient == comment.note.author
140       @event = comment.event
141
142       @commenter = if comment.author
143                      comment.author.display_name
144                    else
145                      t(".anonymous")
146                    end
147
148       @author = @commenter
149       attach_user_avatar(comment.author)
150
151       set_references("note", comment.note)
152
153       set_list_headers(
154         "#{comment.note.id}.note.www.openstreetmap.org",
155         t(".description", :id => comment.note.id),
156         :archive => @noteurl
157       )
158
159       subject = if @owner
160                   t(".#{@event}.subject_own", :commenter => @commenter)
161                 else
162                   t(".#{@event}.subject_other", :commenter => @commenter)
163                 end
164
165       mail :to => recipient.email, :subject => subject
166     end
167   end
168
169   def changeset_comment_notification(comment, recipient)
170     with_recipient_locale recipient do
171       @to_user = recipient.display_name
172       @changeset_url = changeset_url(comment.changeset)
173       @comment = comment.body
174       @owner = recipient == comment.changeset.user
175       @commenter = comment.author.display_name
176       @changeset_comment = comment.changeset.tags["comment"].presence
177       @time = comment.created_at
178       @changeset_author = comment.changeset.user.display_name
179       @unsubscribe_url = changeset_unsubscribe_url(comment.changeset)
180       @author = @commenter
181
182       subject = if @owner
183                   t(".commented.subject_own", :commenter => @commenter)
184                 else
185                   t(".commented.subject_other", :commenter => @commenter)
186                 end
187
188       attach_user_avatar(comment.author)
189
190       set_references("changeset", comment.changeset)
191
192       set_list_headers(
193         "#{comment.changeset.id}.changeset.www.openstreetmap.org",
194         t(".description", :id => comment.changeset.id),
195         :subscribe => changeset_subscribe_url(comment.changeset),
196         :unsubscribe => @unsubscribe_url,
197         :archive => @changeset_url
198       )
199
200       mail :to => recipient.email, :subject => subject
201     end
202   end
203
204   private
205
206   def set_shared_template_vars
207     @root_url = root_url
208   end
209
210   def attach_project_logo
211     attachments.inline["logo.png"] = Rails.root.join("app/assets/images/osm_logo_30.png").read
212   end
213
214   def attach_user_avatar(user)
215     @avatar = user_avatar_filename(user)
216     attachments.inline[@avatar] = user_avatar_file(user)
217   end
218
219   def user_avatar_filename(user)
220     avatar = user&.avatar
221     if avatar&.attached?
222       case avatar.content_type
223       when "image/png" then "avatar.png"
224       when "image/jpeg" then "avatar.jpg"
225       when "image/gif" then "avatar.gif"
226       when "image/bmp" then "avatar.bmp"
227       when "image/tiff" then "avatar.tif"
228       when "image/svg+xml" then "avatar.svg"
229       else "avatar"
230       end
231     else
232       "avatar.png"
233     end
234   end
235
236   def user_avatar_file(user)
237     avatar = user&.avatar
238     if avatar&.attached?
239       if avatar.variable?
240         avatar.variant(:resize_to_limit => [50, 50]).download
241       else
242         avatar.blob.download
243       end
244     else
245       Rails.root.join("app/assets/images/avatar_small.png").read
246     end
247   end
248
249   def with_recipient_locale(recipient, &block)
250     I18n.with_locale(Locale.available.preferred(recipient.preferred_languages), &block)
251   end
252
253   def from_address(name, type, id, token, user_id = nil)
254     if Settings.key?(:messages_domain) && domain = Settings.messages_domain
255       if user_id
256         "#{name} <#{type}-#{id}-#{user_id}-#{token}@#{domain}>"
257       else
258         "#{name} <#{type}-#{id}-#{token}@#{domain}>"
259       end
260     else
261       Settings.email_from
262     end
263   end
264
265   def set_references(scope, reference_object)
266     ref = "osm-#{scope}-#{reference_object.id}@#{Settings.server_url}"
267
268     headers["X-Entity-Ref-ID"] = ref
269     headers["In-Reply-To"] = ref
270     headers["References"] = ref
271   end
272
273   def set_list_headers(id, description, options = {})
274     headers["List-ID"] = "#{description} <#{id}>"
275     headers["List-Archive"] = "<#{options[:archive]}>" if options[:archive]
276     headers["List-Subscribe"] = "<#{options[:subscribe]}>" if options[:subscribe]
277     headers["List-Unsubscribe"] = "<#{options[:unsubscribe]}>" if options[:unsubscribe]
278   end
279 end