ea10eeb7bd413166920f649ea86bba7f4d0c8fb1
[rails.git] / app / models / notifier.rb
1 require "base64"
2
3 class Notifier < ActionMailer::Base
4   default :from => EMAIL_FROM,
5           :return_path => EMAIL_RETURN_PATH,
6           :auto_submitted => "auto-generated"
7   helper :application
8
9   def signup_confirm(user, token)
10     with_recipient_locale user do
11       @url = url_for(:host => SERVER_URL,
12                      :controller => "user", :action => "confirm",
13                      :display_name => user.display_name,
14                      :confirm_string => token.token)
15
16       mail :to => user.email,
17            :subject => I18n.t("notifier.signup_confirm.subject")
18     end
19   end
20
21   def email_confirm(user, token)
22     with_recipient_locale user do
23       @address = user.new_email
24       @url = url_for(:host => SERVER_URL,
25                      :controller => "user", :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(:host => SERVER_URL,
36                      :controller => "user", :action => "reset_password",
37                      :token => token.token)
38
39       mail :to => user.email,
40            :subject => I18n.t("notifier.lost_password.subject")
41     end
42   end
43
44   def gpx_success(trace, possible_points)
45     with_recipient_locale trace.user do
46       @trace_name = trace.name
47       @trace_points = trace.size
48       @trace_description = trace.description
49       @trace_tags = trace.tags
50       @possible_points = possible_points
51
52       mail :to => trace.user.email,
53            :subject => I18n.t("notifier.gpx_notification.success.subject")
54     end
55   end
56
57   def gpx_failure(trace, error)
58     with_recipient_locale trace.user do
59       @trace_name = trace.name
60       @trace_description = trace.description
61       @trace_tags = trace.tags
62       @error = error
63
64       mail :to => trace.user.email,
65            :subject => I18n.t("notifier.gpx_notification.failure.subject")
66     end
67   end
68
69   def message_notification(message)
70     with_recipient_locale message.recipient do
71       @to_user = message.recipient.display_name
72       @from_user = message.sender.display_name
73       @text = message.body
74       @title = message.title
75       @readurl = url_for(:host => SERVER_URL,
76                          :controller => "message", :action => "read",
77                          :message_id => message.id)
78       @replyurl = url_for(:host => SERVER_URL,
79                           :controller => "message", :action => "reply",
80                           :message_id => message.id)
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 = url_for(:host => SERVER_URL,
95                          :controller => "diary_entry",
96                          :action => "view",
97                          :display_name => comment.diary_entry.user.display_name,
98                          :id => comment.diary_entry.id,
99                          :anchor => "comment#{comment.id}")
100       @commenturl = url_for(:host => SERVER_URL,
101                             :controller => "diary_entry",
102                             :action => "view",
103                             :display_name => comment.diary_entry.user.display_name,
104                             :id => comment.diary_entry.id,
105                             :anchor => "newcomment")
106       @replyurl = url_for(:host => SERVER_URL,
107                           :controller => "message",
108                           :action => "new",
109                           :display_name => comment.user.display_name,
110                           :title => "Re: #{comment.diary_entry.title}")
111
112       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
113            :to => recipient.email,
114            :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
115     end
116   end
117
118   def friend_notification(friend)
119     with_recipient_locale friend.befriendee do
120       @friend = friend
121       @viewurl = url_for(:host => SERVER_URL,
122                          :controller => "user", :action => "view",
123                          :display_name => @friend.befriender.display_name)
124       @friendurl = url_for(:host => SERVER_URL,
125                            :controller => "user", :action => "make_friend",
126                            :display_name => @friend.befriender.display_name)
127
128       mail :to => friend.befriendee.email,
129            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
130     end
131   end
132
133   def note_comment_notification(comment, recipient)
134     with_recipient_locale recipient do
135       @noteurl = browse_note_url(comment.note, :host => SERVER_URL)
136       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
137       @comment = comment.body
138       @owner = recipient == comment.note.author
139       @event = comment.event
140
141       @commenter = if comment.author
142                      comment.author.display_name
143                    else
144                      I18n.t("notifier.note_comment_notification.anonymous")
145                    end
146
147       subject = if @owner
148                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
149                 else
150                   I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
151                 end
152
153       mail :to => recipient.email, :subject => subject
154     end
155   end
156
157   def changeset_comment_notification(comment, recipient)
158     with_recipient_locale recipient do
159       @root_url = root_url(:host => SERVER_URL)
160       @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
161       @comment = comment.body
162       @owner = recipient == comment.changeset.user
163       @commenter = comment.author.display_name
164       @commenter_url = user_url(comment.author.display_name, :host => SERVER_URL)
165       @commenter_thumbnail_src = comment.author.image.url(:small, :host => SERVER_URL)
166       @changeset_comment = comment.changeset.tags["comment"].presence
167       @time = comment.created_at
168       @changeset_author = comment.changeset.user.display_name
169
170       subject = if @owner
171                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
172                 else
173                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
174                 end
175
176       attachments.inline['osm_logo_30x30.png'] = {
177         mime_type: 'image/png',
178         encoding: 'base64',
179         content: @@osm_logo_png_bytes,
180       }
181
182       File.open(user_avatar_file_path(comment.author), 'rb') do |file|
183         attachments.inline['avatar.png'] = file.read()
184       end
185
186       mail :to => recipient.email, :subject => subject
187     end
188   end
189
190   private
191
192   def user_avatar_file_path(user)
193     image = user.image
194     if image.file?
195       return image.path.sub('/original/', '/small/')
196     else
197       return Rails.root.join('app', 'assets', 'images', 'users', 'images', 'small.png')
198     end
199   end
200
201   def with_recipient_locale(recipient)
202     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
203       yield
204     end
205   end
206
207   def from_address(name, type, id, digest, user_id = nil)
208     if Object.const_defined?(:MESSAGES_DOMAIN) && domain = MESSAGES_DOMAIN
209       if user_id
210         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
211       else
212         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
213       end
214     else
215       EMAIL_FROM
216     end
217   end
218
219   # FIXME - 2016-12-28 - saintamh - someone please show me a better way to
220   # include this blob in here. If I save it under app/assets, how do I get a
221   # path to it in a portable way?
222   @@osm_logo_png_bytes = <<EOS
223 iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAABGdBTUEAALGPC/xhBQAAAAFzUkdC
224 AK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZiS0dE
225 AP8A/wD/oL2nkwAAAAlwSFlzAAAbrwAAG68BXhqRHAAACchJREFUSMetldmvntdVxn9rv/t9328+
226 43dGHw8nzvGQxnacOKSO2iqJGrV3lFZUrYQYVAmh/gUR3EPFBVfccAOVkEBBYNKWxoorh6RxBhLX
227 duIpHk48xMfHx2f8pnfce3FxHKgQCC54pH2ztbV+2ms9epacXXu1oqp/ChxX5TaqZxVOq5FPiE06
228 XLQRE4hS1o2Y3SvJxtfX+2vfWRssF1795Ujs+7vHd7w9Pda+M8gSP1ffQU3adnHjxhSY58SYbyvM
229 gX4PuLt/+AUArPc+ReQ8yg9Bn1X4LVSXZTl92wzXXvMNFwr+SSPmcOndfufcdBjEkRFLaPnaVHP8
230 98Vz45MLN05+/P7yWzMzn++enG0dG2s3j1Tq8Xwcxg1jglREngLu8kjy4cO/B2Qa9OeqHEFBCw/r
231 GTJRz+txKxAJgqTM6WUDcleCKpv5Gq0wprdc0F2zDNWntdUYy6y1UZonptPbZDN5wMxsnd07phAx
232 f4boKygcHHsJC0Iw3rhfrnTfAo4oCmmJRgaljAqXYYOI3GX08z5iDAaD5nD70wGTrYMceWIXtWok
233 Rqg478hdyRRT9Pq7Wbz1GcnGEnv3tQ8PjTSq6SBLAMyx9nfZxyTe6xlVzVVBnSJJge/kpGmfPE+p
234 SMBQVEN9ySDp8+Cq58Cur/Klg/tp1EPEeCR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