1 class Notifier < ActionMailer::Base
2 default :from => EMAIL_FROM,
3 :return_path => EMAIL_RETURN_PATH,
4 :auto_submitted => "auto-generated"
6 before_action :set_shared_template_vars
7 before_action :attach_project_logo
9 def signup_confirm(user, token)
10 with_recipient_locale user do
11 @url = url_for(:host => SERVER_URL,
12 :protocol => SERVER_PROTOCOL,
13 :controller => "user", :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(:host => SERVER_URL,
26 :protocol => SERVER_PROTOCOL,
27 :controller => "user", :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(:host => SERVER_URL,
38 :protocol => SERVER_PROTOCOL,
39 :controller => "user", :action => "reset_password",
40 :token => token.token)
42 mail :to => user.email,
43 :subject => I18n.t("notifier.lost_password.subject")
47 def gpx_success(trace, possible_points)
48 with_recipient_locale trace.user do
49 @trace_name = trace.name
50 @trace_points = trace.size
51 @trace_description = trace.description
52 @trace_tags = trace.tags
53 @possible_points = possible_points
55 mail :to => trace.user.email,
56 :subject => I18n.t("notifier.gpx_notification.success.subject")
60 def gpx_failure(trace, error)
61 with_recipient_locale trace.user do
62 @trace_name = trace.name
63 @trace_description = trace.description
64 @trace_tags = trace.tags
67 mail :to => trace.user.email,
68 :subject => I18n.t("notifier.gpx_notification.failure.subject")
72 def message_notification(message)
73 with_recipient_locale message.recipient do
74 @to_user = message.recipient.display_name
75 @from_user = message.sender.display_name
77 @title = message.title
78 @readurl = url_for(:host => SERVER_URL,
79 :protocol => SERVER_PROTOCOL,
80 :controller => "message", :action => "read",
81 :message_id => message.id)
82 @replyurl = url_for(:host => SERVER_URL,
83 :protocol => SERVER_PROTOCOL,
84 :controller => "message", :action => "reply",
85 :message_id => message.id)
88 attach_user_avatar(message.sender)
90 mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
91 :to => message.recipient.email,
92 :subject => I18n.t("notifier.message_notification.subject_header", :subject => message.title)
96 def diary_comment_notification(comment, recipient)
97 with_recipient_locale recipient do
98 @to_user = recipient.display_name
99 @from_user = comment.user.display_name
101 @title = comment.diary_entry.title
102 @readurl = url_for(:host => SERVER_URL,
103 :protocol => SERVER_PROTOCOL,
104 :controller => "diary_entry",
106 :display_name => comment.diary_entry.user.display_name,
107 :id => comment.diary_entry.id,
108 :anchor => "comment#{comment.id}")
109 @commenturl = url_for(:host => SERVER_URL,
110 :protocol => SERVER_PROTOCOL,
111 :controller => "diary_entry",
113 :display_name => comment.diary_entry.user.display_name,
114 :id => comment.diary_entry.id,
115 :anchor => "newcomment")
116 @replyurl = url_for(:host => SERVER_URL,
117 :protocol => SERVER_PROTOCOL,
118 :controller => "message",
120 :display_name => comment.user.display_name,
121 :title => "Re: #{comment.diary_entry.title}")
124 attach_user_avatar(comment.user)
126 mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
127 :to => recipient.email,
128 :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
132 def friend_notification(friend)
133 with_recipient_locale friend.befriendee do
135 @viewurl = url_for(:host => SERVER_URL,
136 :protocol => SERVER_PROTOCOL,
137 :controller => "user", :action => "view",
138 :display_name => @friend.befriender.display_name)
139 @friendurl = url_for(:host => SERVER_URL,
140 :protocol => SERVER_PROTOCOL,
141 :controller => "user", :action => "make_friend",
142 :display_name => @friend.befriender.display_name)
143 @author = @friend.befriender.display_name
145 attach_user_avatar(@friend.befriender)
146 mail :to => friend.befriendee.email,
147 :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
151 def note_comment_notification(comment, recipient)
152 with_recipient_locale recipient do
153 @noteurl = browse_note_url(comment.note, :host => SERVER_URL)
154 @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
155 @comment = comment.body
156 @owner = recipient == comment.note.author
157 @event = comment.event
159 @commenter = if comment.author
160 comment.author.display_name
162 I18n.t("notifier.note_comment_notification.anonymous")
166 attach_user_avatar(comment.author)
169 I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
171 I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
174 mail :to => recipient.email, :subject => subject
178 def changeset_comment_notification(comment, recipient)
179 with_recipient_locale recipient do
180 @to_user = recipient.display_name
181 @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
182 @comment = comment.body
183 @owner = recipient == comment.changeset.user
184 @commenter = comment.author.display_name
185 @changeset_comment = comment.changeset.tags["comment"].presence
186 @time = comment.created_at
187 @changeset_author = comment.changeset.user.display_name
191 I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
193 I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
196 attach_user_avatar(comment.author)
198 mail :to => recipient.email, :subject => subject
204 def set_shared_template_vars
205 @root_url = root_url(:host => SERVER_URL)
208 def attach_project_logo
209 attachments.inline["logo.png"] = File.read(Rails.root.join("app", "assets", "images", "osm_logo_30.png"))
212 def attach_user_avatar(user)
213 attachments.inline["avatar.png"] = File.read(user_avatar_file_path(user))
216 def user_avatar_file_path(user)
217 image = user && user.image
218 if image && image.file?
219 return image.path(:small)
221 return Rails.root.join("app", "assets", "images", "users", "images", "small.png")
225 def with_recipient_locale(recipient)
226 I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
231 def from_address(name, type, id, digest, user_id = nil)
232 if Object.const_defined?(:MESSAGES_DOMAIN) && domain = MESSAGES_DOMAIN
234 "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
236 "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"