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