1 class Notifier < 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 => "users", :action => "confirm",
16 :display_name => user.display_name,
17 :confirm_string => token.token)
19 mail :to => user.email,
20 :subject => I18n.t("notifier.signup_confirm.subject")
24 def email_confirm(user, token)
25 with_recipient_locale user do
26 @address = user.new_email
27 @url = url_for(:controller => "users", :action => "confirm_email",
28 :confirm_string => token.token)
30 mail :to => user.new_email,
31 :subject => I18n.t("notifier.email_confirm.subject")
35 def lost_password(user, token)
36 with_recipient_locale user do
37 @url = url_for(:controller => "users", :action => "reset_password",
38 :token => token.token)
40 mail :to => user.email,
41 :subject => I18n.t("notifier.lost_password.subject")
45 def gpx_success(trace, possible_points)
46 with_recipient_locale trace.user do
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
53 mail :to => trace.user.email,
54 :subject => I18n.t("notifier.gpx_notification.success.subject")
58 def gpx_failure(trace, error)
59 with_recipient_locale trace.user do
60 @trace_name = trace.name
61 @trace_description = trace.description
62 @trace_tags = trace.tags
65 mail :to => trace.user.email,
66 :subject => I18n.t("notifier.gpx_notification.failure.subject")
70 def message_notification(message)
71 with_recipient_locale message.recipient do
72 @to_user = message.recipient.display_name
73 @from_user = message.sender.display_name
75 @title = message.title
76 @readurl = message_url(message)
77 @replyurl = message_reply_url(message)
80 attach_user_avatar(message.sender)
82 mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
83 :to => message.recipient.email,
84 :subject => I18n.t("notifier.message_notification.subject_header", :subject => message.title)
88 def diary_comment_notification(comment, recipient)
89 with_recipient_locale recipient do
90 @to_user = recipient.display_name
91 @from_user = comment.user.display_name
93 @title = comment.diary_entry.title
94 @readurl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "comment#{comment.id}")
95 @commenturl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "newcomment")
96 @replyurl = new_message_url(comment.user, :message => { :title => "Re: #{comment.diary_entry.title}" })
99 attach_user_avatar(comment.user)
101 set_references("diary", comment.diary_entry)
103 mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
104 :to => recipient.email,
105 :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
109 def friend_notification(friend)
110 with_recipient_locale friend.befriendee do
112 @viewurl = user_url(@friend.befriender)
113 @friendurl = url_for(:controller => "users", :action => "make_friend",
114 :display_name => @friend.befriender.display_name)
115 @author = @friend.befriender.display_name
117 attach_user_avatar(@friend.befriender)
118 mail :to => friend.befriendee.email,
119 :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
123 def note_comment_notification(comment, recipient)
124 with_recipient_locale recipient do
125 @noteurl = browse_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
131 @commenter = if comment.author
132 comment.author.display_name
134 I18n.t("notifier.note_comment_notification.anonymous")
138 attach_user_avatar(comment.author)
140 set_references("note", comment.note)
143 I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
145 I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
148 mail :to => recipient.email, :subject => subject
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
165 I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
167 I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
170 attach_user_avatar(comment.author)
172 set_references("changeset", comment.changeset)
174 mail :to => recipient.email, :subject => subject
180 def set_shared_template_vars
184 def attach_project_logo
185 attachments.inline["logo.png"] = File.read(Rails.root.join("app/assets/images/osm_logo_30.png"))
188 def attach_user_avatar(user)
189 attachments.inline["avatar.png"] = user_avatar_file(user)
192 def user_avatar_file(user)
193 avatar = user&.avatar
195 image = avatar.variant(:resize => "50x50>").processed
196 image.service.download(image.key)
198 File.read(Rails.root.join("app/assets/images/avatar_small.png"))
202 def with_recipient_locale(recipient)
203 I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
208 def from_address(name, type, id, digest, user_id = nil)
209 if Settings.key?(:messages_domain) && domain = Settings.messages_domain
211 "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
213 "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
220 def set_references(scope, reference_object)
221 ref = "osm-#{scope}-#{reference_object.id}@#{Settings.server_url}"
223 headers["X-Entity-Ref-ID"] = ref
224 headers["In-Reply-To"] = ref
225 headers["References"] = ref