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