]> git.openstreetmap.org Git - rails.git/blob - app/models/notifier.rb
Use https when redirecting to the render server
[rails.git] / app / models / notifier.rb
1 class Notifier < ActionMailer::Base
2   default :from => EMAIL_FROM,
3           :return_path => EMAIL_RETURN_PATH,
4           :auto_submitted => "auto-generated"
5   helper :application
6   before_action :set_shared_template_vars
7   before_action :attach_project_logo
8
9   def signup_confirm(user, token)
10     with_recipient_locale user do
11       @url = url_for(:host => SERVER_URL,
12                      :protocol => SERVER_PROTOCOL,
13                      :controller => "user", :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(:host => SERVER_URL,
26                      :protocol => SERVER_PROTOCOL,
27                      :controller => "user", :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(:host => SERVER_URL,
38                      :protocol => SERVER_PROTOCOL,
39                      :controller => "user", :action => "reset_password",
40                      :token => token.token)
41
42       mail :to => user.email,
43            :subject => I18n.t("notifier.lost_password.subject")
44     end
45   end
46
47   def gpx_success(trace, possible_points)
48     with_recipient_locale trace.user do
49       @trace_name = trace.name
50       @trace_points = trace.size
51       @trace_description = trace.description
52       @trace_tags = trace.tags
53       @possible_points = possible_points
54
55       mail :to => trace.user.email,
56            :subject => I18n.t("notifier.gpx_notification.success.subject")
57     end
58   end
59
60   def gpx_failure(trace, error)
61     with_recipient_locale trace.user do
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("notifier.gpx_notification.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 = url_for(:host => SERVER_URL,
79                          :protocol => SERVER_PROTOCOL,
80                          :controller => "message", :action => "read",
81                          :message_id => message.id)
82       @replyurl = url_for(:host => SERVER_URL,
83                           :protocol => SERVER_PROTOCOL,
84                           :controller => "message", :action => "reply",
85                           :message_id => message.id)
86       @author = @from_user
87
88       attach_user_avatar(message.sender)
89
90       mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
91            :to => message.recipient.email,
92            :subject => I18n.t("notifier.message_notification.subject_header", :subject => message.title)
93     end
94   end
95
96   def diary_comment_notification(comment, recipient)
97     with_recipient_locale recipient do
98       @to_user = recipient.display_name
99       @from_user = comment.user.display_name
100       @text = comment.body
101       @title = comment.diary_entry.title
102       @readurl = url_for(:host => SERVER_URL,
103                          :protocol => SERVER_PROTOCOL,
104                          :controller => "diary_entry",
105                          :action => "view",
106                          :display_name => comment.diary_entry.user.display_name,
107                          :id => comment.diary_entry.id,
108                          :anchor => "comment#{comment.id}")
109       @commenturl = url_for(:host => SERVER_URL,
110                             :protocol => SERVER_PROTOCOL,
111                             :controller => "diary_entry",
112                             :action => "view",
113                             :display_name => comment.diary_entry.user.display_name,
114                             :id => comment.diary_entry.id,
115                             :anchor => "newcomment")
116       @replyurl = url_for(:host => SERVER_URL,
117                           :protocol => SERVER_PROTOCOL,
118                           :controller => "message",
119                           :action => "new",
120                           :display_name => comment.user.display_name,
121                           :title => "Re: #{comment.diary_entry.title}")
122       @author = @from_user
123
124       attach_user_avatar(comment.user)
125
126       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
127            :to => recipient.email,
128            :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
129     end
130   end
131
132   def friend_notification(friend)
133     with_recipient_locale friend.befriendee do
134       @friend = friend
135       @viewurl = url_for(:host => SERVER_URL,
136                          :protocol => SERVER_PROTOCOL,
137                          :controller => "user", :action => "view",
138                          :display_name => @friend.befriender.display_name)
139       @friendurl = url_for(:host => SERVER_URL,
140                            :protocol => SERVER_PROTOCOL,
141                            :controller => "user", :action => "make_friend",
142                            :display_name => @friend.befriender.display_name)
143       @author = @friend.befriender.display_name
144
145       attach_user_avatar(@friend.befriender)
146       mail :to => friend.befriendee.email,
147            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
148     end
149   end
150
151   def note_comment_notification(comment, recipient)
152     with_recipient_locale recipient do
153       @noteurl = browse_note_url(comment.note, :host => SERVER_URL)
154       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
155       @comment = comment.body
156       @owner = recipient == comment.note.author
157       @event = comment.event
158
159       @commenter = if comment.author
160                      comment.author.display_name
161                    else
162                      I18n.t("notifier.note_comment_notification.anonymous")
163                    end
164
165       @author = @commenter
166       attach_user_avatar(comment.author)
167
168       subject = if @owner
169                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
170                 else
171                   I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
172                 end
173
174       mail :to => recipient.email, :subject => subject
175     end
176   end
177
178   def changeset_comment_notification(comment, recipient)
179     with_recipient_locale recipient do
180       @to_user = recipient.display_name
181       @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
182       @comment = comment.body
183       @owner = recipient == comment.changeset.user
184       @commenter = comment.author.display_name
185       @changeset_comment = comment.changeset.tags["comment"].presence
186       @time = comment.created_at
187       @changeset_author = comment.changeset.user.display_name
188       @author = @commenter
189
190       subject = if @owner
191                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
192                 else
193                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
194                 end
195
196       attach_user_avatar(comment.author)
197
198       mail :to => recipient.email, :subject => subject
199     end
200   end
201
202   private
203
204   def set_shared_template_vars
205     @root_url = root_url(:host => SERVER_URL)
206   end
207
208   def attach_project_logo
209     attachments.inline["logo.png"] = File.read(Rails.root.join("app", "assets", "images", "osm_logo_30.png"))
210   end
211
212   def attach_user_avatar(user)
213     attachments.inline["avatar.png"] = File.read(user_avatar_file_path(user))
214   end
215
216   def user_avatar_file_path(user)
217     image = user && user.image
218     if image && image.file?
219       return image.path(:small)
220     else
221       return Rails.root.join("app", "assets", "images", "users", "images", "small.png")
222     end
223   end
224
225   def with_recipient_locale(recipient)
226     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
227       yield
228     end
229   end
230
231   def from_address(name, type, id, digest, user_id = nil)
232     if Object.const_defined?(:MESSAGES_DOMAIN) && domain = MESSAGES_DOMAIN
233       if user_id
234         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
235       else
236         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
237       end
238     else
239       EMAIL_FROM
240     end
241   end
242 end