]> git.openstreetmap.org Git - rails.git/blob - app/mailers/user_mailer.rb
Use bootstrap utility class instead of custom css
[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       @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("user_mailer.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("user_mailer.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("user_mailer.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       @author = @from_user
98
99       attach_user_avatar(comment.user)
100
101       set_references("diary", comment.diary_entry)
102
103       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
104            :to => recipient.email,
105            :subject => I18n.t("user_mailer.diary_comment_notification.subject", :user => comment.user.display_name)
106     end
107   end
108
109   def friendship_notification(friendship)
110     with_recipient_locale friendship.befriendee do
111       @friendship = friendship
112       @viewurl = user_url(@friendship.befriender)
113       @friendurl = make_friend_url(@friendship.befriender)
114       @author = @friendship.befriender.display_name
115
116       attach_user_avatar(@friendship.befriender)
117       mail :to => friendship.befriendee.email,
118            :subject => I18n.t("user_mailer.friendship_notification.subject", :user => friendship.befriender.display_name)
119     end
120   end
121
122   def note_comment_notification(comment, recipient)
123     with_recipient_locale recipient do
124       @noteurl = browse_note_url(comment.note)
125       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
126       @comment = comment.body
127       @owner = recipient == comment.note.author
128       @event = comment.event
129
130       @commenter = if comment.author
131                      comment.author.display_name
132                    else
133                      I18n.t("user_mailer.note_comment_notification.anonymous")
134                    end
135
136       @author = @commenter
137       attach_user_avatar(comment.author)
138
139       set_references("note", comment.note)
140
141       subject = if @owner
142                   I18n.t("user_mailer.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
143                 else
144                   I18n.t("user_mailer.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
145                 end
146
147       mail :to => recipient.email, :subject => subject
148     end
149   end
150
151   def changeset_comment_notification(comment, recipient)
152     with_recipient_locale recipient do
153       @to_user = recipient.display_name
154       @changeset_url = changeset_url(comment.changeset)
155       @comment = comment.body
156       @owner = recipient == comment.changeset.user
157       @commenter = comment.author.display_name
158       @changeset_comment = comment.changeset.tags["comment"].presence
159       @time = comment.created_at
160       @changeset_author = comment.changeset.user.display_name
161       @author = @commenter
162
163       subject = if @owner
164                   I18n.t("user_mailer.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
165                 else
166                   I18n.t("user_mailer.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
167                 end
168
169       attach_user_avatar(comment.author)
170
171       set_references("changeset", comment.changeset)
172
173       mail :to => recipient.email, :subject => subject
174     end
175   end
176
177   private
178
179   def set_shared_template_vars
180     @root_url = root_url
181   end
182
183   def attach_project_logo
184     attachments.inline["logo.png"] = File.read(Rails.root.join("app/assets/images/osm_logo_30.png"))
185   end
186
187   def attach_user_avatar(user)
188     attachments.inline["avatar.png"] = user_avatar_file(user)
189   end
190
191   def user_avatar_file(user)
192     avatar = user&.avatar
193     if avatar&.attached?
194       if avatar.variable?
195         image = avatar.variant(:resize => "50x50>").processed
196         image.service.download(image.key)
197       else
198         avatar.blob.download
199       end
200     else
201       File.read(Rails.root.join("app/assets/images/avatar_small.png"))
202     end
203   end
204
205   def with_recipient_locale(recipient, &block)
206     I18n.with_locale(Locale.available.preferred(recipient.preferred_languages), &block)
207   end
208
209   def from_address(name, type, id, digest, user_id = nil)
210     if Settings.key?(:messages_domain) && domain = Settings.messages_domain
211       if user_id
212         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
213       else
214         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
215       end
216     else
217       Settings.email_from
218     end
219   end
220
221   def set_references(scope, reference_object)
222     ref = "osm-#{scope}-#{reference_object.id}@#{Settings.server_url}"
223
224     headers["X-Entity-Ref-ID"] = ref
225     headers["In-Reply-To"] = ref
226     headers["References"] = ref
227   end
228 end