]> git.openstreetmap.org Git - rails.git/blob - app/models/notifier.rb
1eb39e67844a8b2295f343284030d7b75c303f40
[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                      :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       @author = @from_user
82
83       attach_user_avatar(message.sender)
84
85       mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
86            :to => message.recipient.email,
87            :subject => I18n.t("notifier.message_notification.subject_header", :subject => message.title)
88     end
89   end
90
91   def diary_comment_notification(comment, recipient)
92     with_recipient_locale recipient do
93       @to_user = recipient.display_name
94       @from_user = comment.user.display_name
95       @text = comment.body
96       @title = comment.diary_entry.title
97       @readurl = url_for(:host => SERVER_URL,
98                          :controller => "diary_entry",
99                          :action => "view",
100                          :display_name => comment.diary_entry.user.display_name,
101                          :id => comment.diary_entry.id,
102                          :anchor => "comment#{comment.id}")
103       @commenturl = url_for(:host => SERVER_URL,
104                             :controller => "diary_entry",
105                             :action => "view",
106                             :display_name => comment.diary_entry.user.display_name,
107                             :id => comment.diary_entry.id,
108                             :anchor => "newcomment")
109       @replyurl = url_for(:host => SERVER_URL,
110                           :controller => "message",
111                           :action => "new",
112                           :display_name => comment.user.display_name,
113                           :title => "Re: #{comment.diary_entry.title}")
114       @author = @from_user
115
116       attach_user_avatar(comment.user)
117
118       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
119            :to => recipient.email,
120            :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
121     end
122   end
123
124   def friend_notification(friend)
125     with_recipient_locale friend.befriendee do
126       @friend = friend
127       @viewurl = url_for(:host => SERVER_URL,
128                          :controller => "user", :action => "view",
129                          :display_name => @friend.befriender.display_name)
130       @friendurl = url_for(:host => SERVER_URL,
131                            :controller => "user", :action => "make_friend",
132                            :display_name => @friend.befriender.display_name)
133       @author = @friend.befriender.display_name
134
135       attach_user_avatar(@friend.befriender)
136       mail :to => friend.befriendee.email,
137            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
138     end
139   end
140
141   def note_comment_notification(comment, recipient)
142     with_recipient_locale recipient do
143       @noteurl = browse_note_url(comment.note, :host => SERVER_URL)
144       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
145       @comment = comment.body
146       @owner = recipient == comment.note.author
147       @event = comment.event
148
149       @commenter = if comment.author
150                      comment.author.display_name
151                    else
152                      I18n.t("notifier.note_comment_notification.anonymous")
153                    end
154
155       @author = @commenter
156       attach_user_avatar(comment.author)
157
158       subject = if @owner
159                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
160                 else
161                   I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
162                 end
163
164       mail :to => recipient.email, :subject => subject
165     end
166   end
167
168   def changeset_comment_notification(comment, recipient)
169     with_recipient_locale recipient do
170       @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
171       @comment = comment.body
172       @owner = recipient == comment.changeset.user
173       @commenter = comment.author.display_name
174       @changeset_comment = comment.changeset.tags["comment"].presence
175       @time = comment.created_at
176       @changeset_author = comment.changeset.user.display_name
177       @author = @commenter
178
179       subject = if @owner
180                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
181                 else
182                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
183                 end
184
185       attach_user_avatar(comment.author)
186
187       mail :to => recipient.email, :subject => subject
188     end
189   end
190
191   private
192
193   def set_shared_template_vars
194     @root_url = root_url(:host => SERVER_URL)
195   end
196
197   def attach_project_logo
198     attachments.inline["logo.png"] = File.read(Rails.root.join("app", "assets", "images", "osm_logo_30.png"))
199   end
200
201   def attach_user_avatar(user)
202     attachments.inline["avatar.png"] = File.read(user_avatar_file_path(user))
203   end
204
205   def user_avatar_file_path(user)
206     image = user && user.image
207     if image && image.file?
208       return image.path(:small)
209     else
210       return Rails.root.join("app", "assets", "images", "users", "images", "small.png")
211     end
212   end
213
214   def with_recipient_locale(recipient)
215     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
216       yield
217     end
218   end
219
220   def from_address(name, type, id, digest, user_id = nil)
221     if Object.const_defined?(:MESSAGES_DOMAIN) && domain = MESSAGES_DOMAIN
222       if user_id
223         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
224       else
225         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
226       end
227     else
228       EMAIL_FROM
229     end
230   end
231 end