]> git.openstreetmap.org Git - rails.git/blob - app/mailers/user_mailer.rb
Merge remote-tracking branch 'upstream/pull/4449'
[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_name = trace.name
48       @trace_points = trace.size
49       @trace_description = trace.description
50       @trace_tags = trace.tags
51       @possible_points = possible_points
52
53       mail :to => trace.user.email,
54            :subject => t(".subject")
55     end
56   end
57
58   def gpx_failure(trace, error)
59     with_recipient_locale trace.user do
60       @to_user = trace.user.display_name
61       @trace_name = trace.name
62       @trace_description = trace.description
63       @trace_tags = trace.tags
64       @error = error
65
66       mail :to => trace.user.email,
67            :subject => t(".subject")
68     end
69   end
70
71   def message_notification(message)
72     with_recipient_locale message.recipient do
73       @to_user = message.recipient.display_name
74       @from_user = message.sender.display_name
75       @text = message.body
76       @title = message.title
77       @readurl = message_url(message)
78       @replyurl = message_reply_url(message)
79       @author = @from_user
80
81       attach_user_avatar(message.sender)
82
83       mail :from => from_address(message.sender.display_name, "m", message.id, message.notification_token),
84            :to => message.recipient.email,
85            :subject => t(".subject", :message_title => message.title)
86     end
87   end
88
89   def diary_comment_notification(comment, recipient)
90     with_recipient_locale recipient do
91       @to_user = recipient.display_name
92       @from_user = comment.user.display_name
93       @text = comment.body
94       @title = comment.diary_entry.title
95       @readurl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "comment#{comment.id}")
96       @commenturl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "newcomment")
97       @replyurl = new_message_url(comment.user, :message => { :title => "Re: #{comment.diary_entry.title}" })
98       @author = @from_user
99
100       attach_user_avatar(comment.user)
101
102       set_references("diary", comment.diary_entry)
103
104       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.notification_token(recipient.id), recipient.id),
105            :to => recipient.email,
106            :subject => t(".subject", :user => comment.user.display_name)
107     end
108   end
109
110   def friendship_notification(friendship)
111     with_recipient_locale friendship.befriendee do
112       @friendship = friendship
113       @viewurl = user_url(@friendship.befriender)
114       @friendurl = make_friend_url(@friendship.befriender)
115       @author = @friendship.befriender.display_name
116
117       attach_user_avatar(@friendship.befriender)
118       mail :to => friendship.befriendee.email,
119            :subject => t(".subject", :user => friendship.befriender.display_name)
120     end
121   end
122
123   def note_comment_notification(comment, recipient)
124     with_recipient_locale recipient do
125       @noteurl = note_url(comment.note)
126       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
127       @comment = comment.body
128       @owner = recipient == comment.note.author
129       @event = comment.event
130
131       @commenter = if comment.author
132                      comment.author.display_name
133                    else
134                      t(".anonymous")
135                    end
136
137       @author = @commenter
138       attach_user_avatar(comment.author)
139
140       set_references("note", comment.note)
141
142       subject = if @owner
143                   t(".#{@event}.subject_own", :commenter => @commenter)
144                 else
145                   t(".#{@event}.subject_other", :commenter => @commenter)
146                 end
147
148       mail :to => recipient.email, :subject => subject
149     end
150   end
151
152   def changeset_comment_notification(comment, recipient)
153     with_recipient_locale recipient do
154       @to_user = recipient.display_name
155       @changeset_url = changeset_url(comment.changeset)
156       @comment = comment.body
157       @owner = recipient == comment.changeset.user
158       @commenter = comment.author.display_name
159       @changeset_comment = comment.changeset.tags["comment"].presence
160       @time = comment.created_at
161       @changeset_author = comment.changeset.user.display_name
162       @author = @commenter
163
164       subject = if @owner
165                   t(".commented.subject_own", :commenter => @commenter)
166                 else
167                   t(".commented.subject_other", :commenter => @commenter)
168                 end
169
170       attach_user_avatar(comment.author)
171
172       set_references("changeset", comment.changeset)
173
174       mail :to => recipient.email, :subject => subject
175     end
176   end
177
178   private
179
180   def set_shared_template_vars
181     @root_url = root_url
182   end
183
184   def attach_project_logo
185     attachments.inline["logo.png"] = Rails.root.join("app/assets/images/osm_logo_30.png").read
186   end
187
188   def attach_user_avatar(user)
189     @avatar = user_avatar_filename(user)
190     attachments.inline[@avatar] = user_avatar_file(user)
191   end
192
193   def user_avatar_filename(user)
194     avatar = user&.avatar
195     if avatar&.attached?
196       case avatar.content_type
197       when "image/png" then "avatar.png"
198       when "image/jpeg" then "avatar.jpg"
199       when "image/gif" then "avatar.gif"
200       when "image/bmp" then "avatar.bmp"
201       when "image/tiff" then "avatar.tif"
202       when "image/svg+xml" then "avatar.svg"
203       else "avatar"
204       end
205     else
206       "avatar.png"
207     end
208   end
209
210   def user_avatar_file(user)
211     avatar = user&.avatar
212     if avatar&.attached?
213       if avatar.variable?
214         avatar.variant(:resize_to_limit => [50, 50]).download
215       else
216         avatar.blob.download
217       end
218     else
219       Rails.root.join("app/assets/images/avatar_small.png").read
220     end
221   end
222
223   def with_recipient_locale(recipient, &block)
224     I18n.with_locale(Locale.available.preferred(recipient.preferred_languages), &block)
225   end
226
227   def from_address(name, type, id, token, user_id = nil)
228     if Settings.key?(:messages_domain) && domain = Settings.messages_domain
229       if user_id
230         "#{name} <#{type}-#{id}-#{user_id}-#{token}@#{domain}>"
231       else
232         "#{name} <#{type}-#{id}-#{token}@#{domain}>"
233       end
234     else
235       Settings.email_from
236     end
237   end
238
239   def set_references(scope, reference_object)
240     ref = "osm-#{scope}-#{reference_object.id}@#{Settings.server_url}"
241
242     headers["X-Entity-Ref-ID"] = ref
243     headers["In-Reply-To"] = ref
244     headers["References"] = ref
245   end
246 end