]> git.openstreetmap.org Git - rails.git/blob - app/models/notifier.rb
RTL support
[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       # If we are passed an email address verification token, create
10       # the confirumation URL for account activation.
11       #
12       # Otherwise the email has already been verified e.g. through
13       # a trusted openID provider and the account is active and a
14       # confirmation URL is not needed.
15       if token
16         @url = url_for(:host => SERVER_URL,
17                        :controller => "user", :action => "confirm",
18                        :display_name => user.display_name,
19                        :confirm_string => token.token)
20       end
21
22       mail :to => user.email,
23            :subject => I18n.t('notifier.signup_confirm.subject')
24     end
25   end
26
27   def email_confirm(user, token)
28     with_recipient_locale user do
29       @address = user.new_email
30       @url = url_for(:host => SERVER_URL,
31                      :controller => "user", :action => "confirm_email",
32                      :confirm_string => token.token)
33
34       mail :to => user.new_email,
35            :subject => I18n.t('notifier.email_confirm.subject')
36     end
37   end
38
39   def lost_password(user, token)
40     with_recipient_locale user do
41       @url = url_for(:host => SERVER_URL,
42                      :controller => "user", :action => "reset_password",
43                      :token => token.token)
44
45       mail :to => user.email,
46            :subject => I18n.t('notifier.lost_password.subject')
47     end
48   end
49
50   def gpx_success(trace, possible_points)
51     with_recipient_locale trace.user do
52       @trace_name = trace.name
53       @trace_points = trace.size
54       @trace_description = trace.description
55       @trace_tags = trace.tags
56       @possible_points = possible_points
57
58       mail :to => trace.user.email,
59            :subject => I18n.t('notifier.gpx_notification.success.subject')
60     end
61   end
62
63   def gpx_failure(trace, error)
64     with_recipient_locale trace.user do
65       @trace_name = trace.name
66       @trace_description = trace.description
67       @trace_tags = trace.tags
68       @error = error
69
70       mail :to => trace.user.email,
71            :subject => I18n.t('notifier.gpx_notification.failure.subject')
72     end
73   end
74
75   def message_notification(message)
76     with_recipient_locale message.recipient do
77       @to_user = message.recipient.display_name
78       @from_user = message.sender.display_name
79       @text = message.body
80       @title = message.title
81       @readurl = url_for(:host => SERVER_URL,
82                          :controller => "message", :action => "read",
83                          :message_id => message.id)
84       @replyurl = url_for(:host => SERVER_URL,
85                           :controller => "message", :action => "reply",
86                           :message_id => message.id)
87
88       mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
89            :to => message.recipient.email,
90            :subject => I18n.t('notifier.message_notification.subject_header', :subject => message.title)
91     end
92   end
93
94   def diary_comment_notification(comment)
95     with_recipient_locale comment.diary_entry.user do
96       @to_user = comment.diary_entry.user.display_name
97       @from_user = comment.user.display_name
98       @text = comment.body
99       @title = comment.diary_entry.title
100       @readurl = 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 => "comment#{comment.id}")
106       @commenturl = url_for(:host => SERVER_URL,
107                             :controller => "diary_entry",
108                             :action => "view",
109                             :display_name => comment.diary_entry.user.display_name,
110                             :id => comment.diary_entry.id,
111                             :anchor => "newcomment")
112       @replyurl = url_for(:host => SERVER_URL,
113                           :controller => "message",
114                           :action => "new",
115                           :display_name => comment.user.display_name,
116                           :title => "Re: #{comment.diary_entry.title}")
117
118       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest),
119            :to =>  comment.diary_entry.user.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
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       if comment.author
142         @commenter = comment.author.display_name
143       else
144         @commenter = I18n.t("notifier.note_comment_notification.anonymous")
145       end
146
147       if @owner
148         subject = I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
149       else
150         subject = 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 private
158
159   def with_recipient_locale(recipient)
160     old_locale = I18n.locale
161
162     begin
163       I18n.locale = recipient.preferred_language_from(I18n.available_locales)
164
165       yield
166     ensure
167       I18n.locale = old_locale
168     end
169   end
170
171   def from_address(name, type, id, digest)
172     if Object.const_defined?(:MESSAGES_DOMAIN) and domain = MESSAGES_DOMAIN
173       "#{name} <#{type}-#{id}-#{digest[0,6]}@#{domain}>"
174     else
175       EMAIL_FROM
176     end
177   end
178 end