]> git.openstreetmap.org Git - rails.git/blob - app/models/notifier.rb
Remove HTML markup from locale file
[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
7   def signup_confirm(user, token)
8     with_recipient_locale user do
9       @url = url_for(:host => SERVER_URL,
10                      :controller => "user", :action => "confirm",
11                      :display_name => user.display_name,
12                      :confirm_string => token.token)
13
14       mail :to => user.email,
15            :subject => I18n.t("notifier.signup_confirm.subject")
16     end
17   end
18
19   def email_confirm(user, token)
20     with_recipient_locale user do
21       @address = user.new_email
22       @url = url_for(:host => SERVER_URL,
23                      :controller => "user", :action => "confirm_email",
24                      :confirm_string => token.token)
25
26       mail :to => user.new_email,
27            :subject => I18n.t("notifier.email_confirm.subject")
28     end
29   end
30
31   def lost_password(user, token)
32     with_recipient_locale user do
33       @url = url_for(:host => SERVER_URL,
34                      :controller => "user", :action => "reset_password",
35                      :token => token.token)
36
37       mail :to => user.email,
38            :subject => I18n.t("notifier.lost_password.subject")
39     end
40   end
41
42   def gpx_success(trace, possible_points)
43     with_recipient_locale trace.user do
44       @trace_name = trace.name
45       @trace_points = trace.size
46       @trace_description = trace.description
47       @trace_tags = trace.tags
48       @possible_points = possible_points
49
50       mail :to => trace.user.email,
51            :subject => I18n.t("notifier.gpx_notification.success.subject")
52     end
53   end
54
55   def gpx_failure(trace, error)
56     with_recipient_locale trace.user do
57       @trace_name = trace.name
58       @trace_description = trace.description
59       @trace_tags = trace.tags
60       @error = error
61
62       mail :to => trace.user.email,
63            :subject => I18n.t("notifier.gpx_notification.failure.subject")
64     end
65   end
66
67   def message_notification(message)
68     with_recipient_locale message.recipient do
69       @to_user = message.recipient.display_name
70       @from_user = message.sender.display_name
71       @text = message.body
72       @title = message.title
73       @readurl = url_for(:host => SERVER_URL,
74                          :controller => "message", :action => "read",
75                          :message_id => message.id)
76       @replyurl = url_for(:host => SERVER_URL,
77                           :controller => "message", :action => "reply",
78                           :message_id => message.id)
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 = url_for(:host => SERVER_URL,
93                          :controller => "diary_entry",
94                          :action => "view",
95                          :display_name => comment.diary_entry.user.display_name,
96                          :id => comment.diary_entry.id,
97                          :anchor => "comment#{comment.id}")
98       @commenturl = url_for(:host => SERVER_URL,
99                             :controller => "diary_entry",
100                             :action => "view",
101                             :display_name => comment.diary_entry.user.display_name,
102                             :id => comment.diary_entry.id,
103                             :anchor => "newcomment")
104       @replyurl = url_for(:host => SERVER_URL,
105                           :controller => "message",
106                           :action => "new",
107                           :display_name => comment.user.display_name,
108                           :title => "Re: #{comment.diary_entry.title}")
109
110       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
111            :to => recipient.email,
112            :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
113     end
114   end
115
116   def friend_notification(friend)
117     with_recipient_locale friend.befriendee do
118       @friend = friend
119       @viewurl = url_for(:host => SERVER_URL,
120                          :controller => "user", :action => "view",
121                          :display_name => @friend.befriender.display_name)
122       @friendurl = url_for(:host => SERVER_URL,
123                            :controller => "user", :action => "make_friend",
124                            :display_name => @friend.befriender.display_name)
125
126       mail :to => friend.befriendee.email,
127            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
128     end
129   end
130
131   def note_comment_notification(comment, recipient)
132     with_recipient_locale recipient do
133       @noteurl = browse_note_url(comment.note, :host => SERVER_URL)
134       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
135       @comment = comment.body
136       @owner = recipient == comment.note.author
137       @event = comment.event
138
139       @commenter = if comment.author
140                      comment.author.display_name
141                    else
142                      I18n.t("notifier.note_comment_notification.anonymous")
143                    end
144
145       subject = if @owner
146                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
147                 else
148                   I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
149                 end
150
151       mail :to => recipient.email, :subject => subject
152     end
153   end
154
155   def changeset_comment_notification(comment, recipient)
156     with_recipient_locale recipient do
157       @root_url = root_url(:host => SERVER_URL)
158       @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
159       @comment = comment.body
160       @owner = recipient == comment.changeset.user
161       @commenter = comment.author.display_name
162       @changeset_comment = comment.changeset.tags["comment"].presence
163       @time = comment.created_at
164       @changeset_author = comment.changeset.user.display_name
165
166       subject = if @owner
167                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
168                 else
169                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
170                 end
171
172       attachments.inline["logo.png"] = File.read("#{Rails.root}/app/assets/images/osm_logo_30.png")
173       attachments.inline["avatar.png"] = File.read(user_avatar_file_path(comment.author))
174
175       mail :to => recipient.email, :subject => subject
176     end
177   end
178
179   private
180
181   def user_avatar_file_path(user)
182     image = user.image
183     if image.file?
184       return image.path(:small)
185     else
186       return "#{Rails.root}/app/assets/images/users/images/small.png"
187     end
188   end
189
190   def with_recipient_locale(recipient)
191     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
192       yield
193     end
194   end
195
196   def from_address(name, type, id, digest, user_id = nil)
197     if Object.const_defined?(:MESSAGES_DOMAIN) && domain = MESSAGES_DOMAIN
198       if user_id
199         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
200       else
201         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
202       end
203     else
204       EMAIL_FROM
205     end
206   end
207 end