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