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