1 class Notifier < ActionMailer::Base
2 include ActionView::Helpers::AssetUrlHelper
4 default :from => Settings.email_from,
5 :return_path => Settings.email_return_path,
6 :auto_submitted => "auto-generated"
8 before_action :set_shared_template_vars
9 before_action :attach_project_logo
11 def signup_confirm(user, token)
12 with_recipient_locale user do
13 @url = url_for(:controller => "users", :action => "confirm",
14 :display_name => user.display_name,
15 :confirm_string => token.token)
17 mail :to => user.email,
18 :subject => I18n.t("notifier.signup_confirm.subject")
22 def email_confirm(user, token)
23 with_recipient_locale user do
24 @address = user.new_email
25 @url = url_for(:controller => "users", :action => "confirm_email",
26 :confirm_string => token.token)
28 mail :to => user.new_email,
29 :subject => I18n.t("notifier.email_confirm.subject")
33 def lost_password(user, token)
34 with_recipient_locale user do
35 @url = url_for(:controller => "users", :action => "reset_password",
36 :token => token.token)
38 mail :to => user.email,
39 :subject => I18n.t("notifier.lost_password.subject")
43 def gpx_success(trace, possible_points)
44 with_recipient_locale trace.user do
45 @trace_name = trace.name
46 @trace_points = trace.size
47 @trace_description = trace.description
48 @trace_tags = trace.tags
49 @possible_points = possible_points
51 mail :to => trace.user.email,
52 :subject => I18n.t("notifier.gpx_notification.success.subject")
56 def gpx_failure(trace, error)
57 with_recipient_locale trace.user do
58 @trace_name = trace.name
59 @trace_description = trace.description
60 @trace_tags = trace.tags
63 mail :to => trace.user.email,
64 :subject => I18n.t("notifier.gpx_notification.failure.subject")
68 def message_notification(message)
69 with_recipient_locale message.recipient do
70 @to_user = message.recipient.display_name
71 @from_user = message.sender.display_name
73 @title = message.title
74 @readurl = message_url(message)
75 @replyurl = message_reply_url(message)
78 attach_user_avatar(message.sender)
80 mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
81 :to => message.recipient.email,
82 :subject => I18n.t("notifier.message_notification.subject_header", :subject => message.title)
86 def diary_comment_notification(comment, recipient)
87 with_recipient_locale recipient do
88 @to_user = recipient.display_name
89 @from_user = comment.user.display_name
91 @title = comment.diary_entry.title
92 @readurl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "comment#{comment.id}")
93 @commenturl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "newcomment")
94 @replyurl = new_message_url(comment.user, :message => { :title => "Re: #{comment.diary_entry.title}" })
98 attach_user_avatar(comment.user)
100 mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
101 :to => recipient.email,
102 :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
106 def friend_notification(friend)
107 with_recipient_locale friend.befriendee do
109 @viewurl = user_url(@friend.befriender)
110 @friendurl = url_for(:controller => "users", :action => "make_friend",
111 :display_name => @friend.befriender.display_name)
112 @author = @friend.befriender.display_name
114 attach_user_avatar(@friend.befriender)
115 mail :to => friend.befriendee.email,
116 :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
120 def note_comment_notification(comment, recipient)
121 with_recipient_locale recipient do
122 @noteurl = browse_note_url(comment.note)
123 @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
124 @comment = comment.body
125 @owner = recipient == comment.note.author
126 @event = comment.event
128 @commenter = if comment.author
129 comment.author.display_name
131 I18n.t("notifier.note_comment_notification.anonymous")
135 attach_user_avatar(comment.author)
138 I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
140 I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
143 mail :to => recipient.email, :subject => subject
147 def changeset_comment_notification(comment, recipient)
148 with_recipient_locale recipient do
149 @to_user = recipient.display_name
150 @changeset_url = changeset_url(comment.changeset)
151 @comment = comment.body
152 @owner = recipient == comment.changeset.user
153 @commenter = comment.author.display_name
154 @changeset_comment = comment.changeset.tags["comment"].presence
155 @time = comment.created_at
156 @changeset_author = comment.changeset.user.display_name
160 I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
162 I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
165 attach_user_avatar(comment.author)
167 mail :to => recipient.email, :subject => subject
173 def set_shared_template_vars
177 def attach_project_logo
178 attachments.inline["logo.png"] = File.read(Rails.root.join("app", "assets", "images", "osm_logo_30.png"))
181 def attach_user_avatar(user)
182 attachments.inline["avatar.png"] = user_avatar_file(user)
185 def user_avatar_file(user)
186 avatar = user&.avatar
188 return avatar.variant(:resize => "50x50>").blob.download
190 return File.read(user_avatar_file_path(user))
194 def user_avatar_file_path(user)
197 return image.path(:small)
199 return Rails.root.join("app", "assets", "images", "avatar_small.png")
203 def with_recipient_locale(recipient)
204 I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
209 def from_address(name, type, id, digest, user_id = nil)
210 if Settings.key?(:messages_domain) && domain = Settings.messages_domain
212 "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
214 "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"