]> git.openstreetmap.org Git - rails.git/blob - app/mailers/notifier.rb
Add In-Reply-To, References headers to email
[rails.git] / app / mailers / notifier.rb
1 class Notifier < 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 => "users", :action => "confirm",
16                      :display_name => user.display_name,
17                      :confirm_string => token.token)
18
19       mail :to => user.email,
20            :subject => I18n.t("notifier.signup_confirm.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 => "users", :action => "confirm_email",
28                      :confirm_string => token.token)
29
30       mail :to => user.new_email,
31            :subject => I18n.t("notifier.email_confirm.subject")
32     end
33   end
34
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)
39
40       mail :to => user.email,
41            :subject => I18n.t("notifier.lost_password.subject")
42     end
43   end
44
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
52
53       mail :to => trace.user.email,
54            :subject => I18n.t("notifier.gpx_notification.success.subject")
55     end
56   end
57
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
63       @error = error
64
65       mail :to => trace.user.email,
66            :subject => I18n.t("notifier.gpx_notification.failure.subject")
67     end
68   end
69
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
74       @text = message.body
75       @title = message.title
76       @readurl = message_url(message)
77       @replyurl = message_reply_url(message)
78       @author = @from_user
79
80       attach_user_avatar(message.sender)
81
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)
85     end
86   end
87
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
92       @text = comment.body
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}" })
97       @ref = "osm-diary-#{comment.diary_entry.id}@#{Settings.server_url}"
98       @author = @from_user
99
100       attach_user_avatar(comment.user)
101
102       headers["In-Reply-To"] = @ref
103       headers["References"]  = @ref
104
105       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
106            :to => recipient.email,
107            :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
108     end
109   end
110
111   def friend_notification(friend)
112     with_recipient_locale friend.befriendee do
113       @friend = friend
114       @viewurl = user_url(@friend.befriender)
115       @friendurl = url_for(:controller => "users", :action => "make_friend",
116                            :display_name => @friend.befriender.display_name)
117       @author = @friend.befriender.display_name
118
119       attach_user_avatar(@friend.befriender)
120       mail :to => friend.befriendee.email,
121            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
122     end
123   end
124
125   def note_comment_notification(comment, recipient)
126     with_recipient_locale recipient do
127       @noteurl = browse_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
132       @ref = "osm-note-#{comment.note.id}@#{Settings.server_url}"
133
134       @commenter = if comment.author
135                      comment.author.display_name
136                    else
137                      I18n.t("notifier.note_comment_notification.anonymous")
138                    end
139
140       @author = @commenter
141       attach_user_avatar(comment.author)
142
143       headers["In-Reply-To"] = @ref
144       headers["References"]  = @ref
145
146       subject = if @owner
147                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
148                 else
149                   I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
150                 end
151
152       mail :to => recipient.email, :subject => subject
153     end
154   end
155
156   def changeset_comment_notification(comment, recipient)
157     with_recipient_locale recipient do
158       @to_user = recipient.display_name
159       @changeset_url = changeset_url(comment.changeset)
160       @comment = comment.body
161       @owner = recipient == comment.changeset.user
162       @commenter = comment.author.display_name
163       @changeset_comment = comment.changeset.tags["comment"].presence
164       @time = comment.created_at
165       @changeset_author = comment.changeset.user.display_name
166       @author = @commenter
167       @ref = "osm-changeset-#{comment.changeset.id}@#{Settings.server_url}"
168
169       subject = if @owner
170                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
171                 else
172                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
173                 end
174
175       attach_user_avatar(comment.author)
176
177       headers["In-Reply-To"] = @ref
178       headers["References"]  = @ref
179
180       mail :to => recipient.email, :subject => subject
181     end
182   end
183
184   private
185
186   def set_shared_template_vars
187     @root_url = root_url
188   end
189
190   def attach_project_logo
191     attachments.inline["logo.png"] = File.read(Rails.root.join("app/assets/images/osm_logo_30.png"))
192   end
193
194   def attach_user_avatar(user)
195     attachments.inline["avatar.png"] = user_avatar_file(user)
196   end
197
198   def user_avatar_file(user)
199     avatar = user&.avatar
200     if avatar&.attached?
201       avatar.variant(:resize => "50x50>").blob.download
202     else
203       File.read(Rails.root.join("app/assets/images/avatar_small.png"))
204     end
205   end
206
207   def with_recipient_locale(recipient)
208     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
209       yield
210     end
211   end
212
213   def from_address(name, type, id, digest, user_id = nil)
214     if Settings.key?(:messages_domain) && domain = Settings.messages_domain
215       if user_id
216         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
217       else
218         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
219       end
220     else
221       Settings.email_from
222     end
223   end
224 end