]> git.openstreetmap.org Git - rails.git/blob - app/mailers/user_mailer.rb
Merge remote-tracking branch 'upstream/pull/2983' into master
[rails.git] / app / mailers / user_mailer.rb
1 class UserMailer < 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("user_mailer.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("user_mailer.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("user_mailer.lost_password.subject")
42     end
43   end
44
45   def gpx_success(trace, possible_points)
46     with_recipient_locale trace.user do
47       @to_user = trace.user.display_name
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
54       mail :to => trace.user.email,
55            :subject => I18n.t("user_mailer.gpx_success.subject")
56     end
57   end
58
59   def gpx_failure(trace, error)
60     with_recipient_locale trace.user do
61       @to_user = trace.user.display_name
62       @trace_name = trace.name
63       @trace_description = trace.description
64       @trace_tags = trace.tags
65       @error = error
66
67       mail :to => trace.user.email,
68            :subject => I18n.t("user_mailer.gpx_failure.subject")
69     end
70   end
71
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
76       @text = message.body
77       @title = message.title
78       @readurl = message_url(message)
79       @replyurl = message_reply_url(message)
80       @author = @from_user
81
82       attach_user_avatar(message.sender)
83
84       mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
85            :to => message.recipient.email,
86            :subject => I18n.t("user_mailer.message_notification.subject_header", :subject => message.title)
87     end
88   end
89
90   def diary_comment_notification(comment, recipient)
91     with_recipient_locale recipient do
92       @to_user = recipient.display_name
93       @from_user = comment.user.display_name
94       @text = comment.body
95       @title = comment.diary_entry.title
96       @readurl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "comment#{comment.id}")
97       @commenturl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "newcomment")
98       @replyurl = new_message_url(comment.user, :message => { :title => "Re: #{comment.diary_entry.title}" })
99       @author = @from_user
100
101       attach_user_avatar(comment.user)
102
103       set_references("diary", comment.diary_entry)
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("user_mailer.diary_comment_notification.subject", :user => comment.user.display_name)
108     end
109   end
110
111   def friendship_notification(friendship)
112     with_recipient_locale friendship.befriendee do
113       @friendship = friendship
114       @viewurl = user_url(@friendship.befriender)
115       @friendurl = make_friend_url(@friendship.befriender)
116       @author = @friendship.befriender.display_name
117
118       attach_user_avatar(@friendship.befriender)
119       mail :to => friendship.befriendee.email,
120            :subject => I18n.t("user_mailer.friendship_notification.subject", :user => friendship.befriender.display_name)
121     end
122   end
123
124   def note_comment_notification(comment, recipient)
125     with_recipient_locale recipient do
126       @noteurl = browse_note_url(comment.note)
127       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
128       @comment = comment.body
129       @owner = recipient == comment.note.author
130       @event = comment.event
131
132       @commenter = if comment.author
133                      comment.author.display_name
134                    else
135                      I18n.t("user_mailer.note_comment_notification.anonymous")
136                    end
137
138       @author = @commenter
139       attach_user_avatar(comment.author)
140
141       set_references("note", comment.note)
142
143       subject = if @owner
144                   I18n.t("user_mailer.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
145                 else
146                   I18n.t("user_mailer.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
147                 end
148
149       mail :to => recipient.email, :subject => subject
150     end
151   end
152
153   def changeset_comment_notification(comment, recipient)
154     with_recipient_locale recipient do
155       @to_user = recipient.display_name
156       @changeset_url = changeset_url(comment.changeset)
157       @comment = comment.body
158       @owner = recipient == comment.changeset.user
159       @commenter = comment.author.display_name
160       @changeset_comment = comment.changeset.tags["comment"].presence
161       @time = comment.created_at
162       @changeset_author = comment.changeset.user.display_name
163       @author = @commenter
164
165       subject = if @owner
166                   I18n.t("user_mailer.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
167                 else
168                   I18n.t("user_mailer.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
169                 end
170
171       attach_user_avatar(comment.author)
172
173       set_references("changeset", comment.changeset)
174
175       mail :to => recipient.email, :subject => subject
176     end
177   end
178
179   private
180
181   def set_shared_template_vars
182     @root_url = root_url
183   end
184
185   def attach_project_logo
186     attachments.inline["logo.png"] = File.read(Rails.root.join("app/assets/images/osm_logo_30.png"))
187   end
188
189   def attach_user_avatar(user)
190     attachments.inline["avatar.png"] = user_avatar_file(user)
191   end
192
193   def user_avatar_file(user)
194     avatar = user&.avatar
195     if avatar&.attached?
196       if avatar.variable?
197         image = avatar.variant(:resize => "50x50>").processed
198         image.service.download(image.key)
199       else
200         avatar.blob.download
201       end
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, &block)
208     I18n.with_locale(Locale.available.preferred(recipient.preferred_languages), &block)
209   end
210
211   def from_address(name, type, id, digest, user_id = nil)
212     if Settings.key?(:messages_domain) && domain = Settings.messages_domain
213       if user_id
214         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
215       else
216         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
217       end
218     else
219       Settings.email_from
220     end
221   end
222
223   def set_references(scope, reference_object)
224     ref = "osm-#{scope}-#{reference_object.id}@#{Settings.server_url}"
225
226     headers["X-Entity-Ref-ID"] = ref
227     headers["In-Reply-To"] = ref
228     headers["References"] = ref
229   end
230 end