1 class UserMailer < ApplicationMailer
2 include ActionView::Helpers::AssetUrlHelper
4 self.delivery_job = ActionMailer::MailDeliveryJob
6 default :from => Settings.email_from,
7 :return_path => Settings.email_return_path,
8 :auto_submitted => "auto-generated"
10 before_action :set_shared_template_vars
11 before_action :attach_project_logo
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)
19 mail :to => user.email,
20 :subject => t(".subject")
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)
30 mail :to => user.new_email,
31 :subject => t(".subject")
35 def lost_password(user, token)
36 with_recipient_locale user do
37 @url = user_reset_password_url(:token => token.token)
39 mail :to => user.email,
40 :subject => t(".subject")
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")
55 mail :to => trace.user.email,
56 :subject => t(".subject")
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
68 mail :to => trace.user.email,
69 :subject => t(".subject")
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
78 @title = message.title
79 @readurl = message_url(message)
80 @replyurl = message_reply_url(message)
83 attach_user_avatar(message.sender)
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)
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
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}" })
102 attach_user_avatar(comment.user)
104 set_references("diary", comment.diary_entry)
106 mail :from => from_address(comment.user.display_name, "c", comment.id, comment.notification_token(recipient.id), recipient.id),
107 :to => recipient.email,
108 :subject => t(".subject", :user => comment.user.display_name)
112 def friendship_notification(friendship)
113 with_recipient_locale friendship.befriendee do
114 @friendship = friendship
115 @viewurl = user_url(@friendship.befriender)
116 @friendurl = make_friend_url(@friendship.befriender)
117 @author = @friendship.befriender.display_name
119 attach_user_avatar(@friendship.befriender)
120 mail :to => friendship.befriendee.email,
121 :subject => t(".subject", :user => friendship.befriender.display_name)
125 def note_comment_notification(comment, recipient)
126 with_recipient_locale recipient do
127 @noteurl = note_url(comment.note)
128 @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
129 @comment = comment.body
130 @owner = recipient == comment.note.author
131 @event = comment.event
133 @commenter = if comment.author
134 comment.author.display_name
140 attach_user_avatar(comment.author)
142 set_references("note", comment.note)
145 t(".#{@event}.subject_own", :commenter => @commenter)
147 t(".#{@event}.subject_other", :commenter => @commenter)
150 mail :to => recipient.email, :subject => subject
154 def changeset_comment_notification(comment, recipient)
155 with_recipient_locale recipient do
156 @to_user = recipient.display_name
157 @changeset_url = changeset_url(comment.changeset)
158 @comment = comment.body
159 @owner = recipient == comment.changeset.user
160 @commenter = comment.author.display_name
161 @changeset_comment = comment.changeset.tags["comment"].presence
162 @time = comment.created_at
163 @changeset_author = comment.changeset.user.display_name
167 t(".commented.subject_own", :commenter => @commenter)
169 t(".commented.subject_other", :commenter => @commenter)
172 attach_user_avatar(comment.author)
174 set_references("changeset", comment.changeset)
176 mail :to => recipient.email, :subject => subject
182 def set_shared_template_vars
186 def attach_project_logo
187 attachments.inline["logo.png"] = Rails.root.join("app/assets/images/osm_logo_30.png").read
190 def attach_user_avatar(user)
191 @avatar = user_avatar_filename(user)
192 attachments.inline[@avatar] = user_avatar_file(user)
195 def user_avatar_filename(user)
196 avatar = user&.avatar
198 case avatar.content_type
199 when "image/png" then "avatar.png"
200 when "image/jpeg" then "avatar.jpg"
201 when "image/gif" then "avatar.gif"
202 when "image/bmp" then "avatar.bmp"
203 when "image/tiff" then "avatar.tif"
204 when "image/svg+xml" then "avatar.svg"
212 def user_avatar_file(user)
213 avatar = user&.avatar
216 avatar.variant(:resize_to_limit => [50, 50]).download
221 Rails.root.join("app/assets/images/avatar_small.png").read
225 def with_recipient_locale(recipient, &block)
226 I18n.with_locale(Locale.available.preferred(recipient.preferred_languages), &block)
229 def from_address(name, type, id, token, user_id = nil)
230 if Settings.key?(:messages_domain) && domain = Settings.messages_domain
232 "#{name} <#{type}-#{id}-#{user_id}-#{token}@#{domain}>"
234 "#{name} <#{type}-#{id}-#{token}@#{domain}>"
241 def set_references(scope, reference_object)
242 ref = "osm-#{scope}-#{reference_object.id}@#{Settings.server_url}"
244 headers["X-Entity-Ref-ID"] = ref
245 headers["In-Reply-To"] = ref
246 headers["References"] = ref