]> git.openstreetmap.org Git - rails.git/blob - app/mailers/notifier.rb
4509f10114bec03308da57e7744426389caf1a1b
[rails.git] / app / mailers / notifier.rb
1 class Notifier < ActionMailer::Base
2   include ActionView::Helpers::AssetUrlHelper
3
4   default :from => Settings.email_from,
5           :return_path => Settings.email_return_path,
6           :auto_submitted => "auto-generated"
7   helper :application
8   before_action :set_shared_template_vars
9   before_action :attach_project_logo
10
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)
16
17       mail :to => user.email,
18            :subject => I18n.t("notifier.signup_confirm.subject")
19     end
20   end
21
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)
27
28       mail :to => user.new_email,
29            :subject => I18n.t("notifier.email_confirm.subject")
30     end
31   end
32
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)
37
38       mail :to => user.email,
39            :subject => I18n.t("notifier.lost_password.subject")
40     end
41   end
42
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
50
51       mail :to => trace.user.email,
52            :subject => I18n.t("notifier.gpx_notification.success.subject")
53     end
54   end
55
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
61       @error = error
62
63       mail :to => trace.user.email,
64            :subject => I18n.t("notifier.gpx_notification.failure.subject")
65     end
66   end
67
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
72       @text = message.body
73       @title = message.title
74       @readurl = message_url(message)
75       @replyurl = message_reply_url(message)
76       @author = @from_user
77
78       attach_user_avatar(message.sender)
79
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)
83     end
84   end
85
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
90       @text = comment.body
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}" })
95
96       @author = @from_user
97
98       attach_user_avatar(comment.user)
99
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)
103     end
104   end
105
106   def friend_notification(friend)
107     with_recipient_locale friend.befriendee do
108       @friend = friend
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
113
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)
117     end
118   end
119
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
127
128       @commenter = if comment.author
129                      comment.author.display_name
130                    else
131                      I18n.t("notifier.note_comment_notification.anonymous")
132                    end
133
134       @author = @commenter
135       attach_user_avatar(comment.author)
136
137       subject = if @owner
138                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
139                 else
140                   I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
141                 end
142
143       mail :to => recipient.email, :subject => subject
144     end
145   end
146
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
157       @author = @commenter
158
159       subject = if @owner
160                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
161                 else
162                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
163                 end
164
165       attach_user_avatar(comment.author)
166
167       mail :to => recipient.email, :subject => subject
168     end
169   end
170
171   private
172
173   def set_shared_template_vars
174     @root_url = root_url
175   end
176
177   def attach_project_logo
178     attachments.inline["logo.png"] = File.read(Rails.root.join("app", "assets", "images", "osm_logo_30.png"))
179   end
180
181   def attach_user_avatar(user)
182     attachments.inline["avatar.png"] = user_avatar_file(user)
183   end
184
185   def user_avatar_file(user)
186     avatar = user&.avatar
187     if avatar&.attached?
188       return avatar.variant(:resize => "50x50>").blob.download
189     else
190       return File.read(Rails.root.join("app", "assets", "images", "avatar_small.png"))
191     end
192   end
193
194   def with_recipient_locale(recipient)
195     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
196       yield
197     end
198   end
199
200   def from_address(name, type, id, digest, user_id = nil)
201     if Settings.key?(:messages_domain) && domain = Settings.messages_domain
202       if user_id
203         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
204       else
205         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
206       end
207     else
208       Settings.email_from
209     end
210   end
211 end