Set default_url_options for action_mailer
[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(: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(: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(:controller => "user", :action => "reset_password",
34                      :token => token.token)
35
36       mail :to => user.email,
37            :subject => I18n.t("notifier.lost_password.subject")
38     end
39   end
40
41   def gpx_success(trace, possible_points)
42     with_recipient_locale trace.user do
43       @trace_name = trace.name
44       @trace_points = trace.size
45       @trace_description = trace.description
46       @trace_tags = trace.tags
47       @possible_points = possible_points
48
49       mail :to => trace.user.email,
50            :subject => I18n.t("notifier.gpx_notification.success.subject")
51     end
52   end
53
54   def gpx_failure(trace, error)
55     with_recipient_locale trace.user do
56       @trace_name = trace.name
57       @trace_description = trace.description
58       @trace_tags = trace.tags
59       @error = error
60
61       mail :to => trace.user.email,
62            :subject => I18n.t("notifier.gpx_notification.failure.subject")
63     end
64   end
65
66   def message_notification(message)
67     with_recipient_locale message.recipient do
68       @to_user = message.recipient.display_name
69       @from_user = message.sender.display_name
70       @text = message.body
71       @title = message.title
72       @readurl = url_for(:controller => "message", :action => "read",
73                          :message_id => message.id)
74       @replyurl = url_for(:controller => "message", :action => "reply",
75                           :message_id => message.id)
76       @author = @from_user
77
78       attach_user_avatar(message.sender)
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(:controller => "diary_entry",
93                          :action => "view",
94                          :display_name => comment.diary_entry.user.display_name,
95                          :id => comment.diary_entry.id,
96                          :anchor => "comment#{comment.id}")
97       @commenturl = url_for(:controller => "diary_entry",
98                             :action => "view",
99                             :display_name => comment.diary_entry.user.display_name,
100                             :id => comment.diary_entry.id,
101                             :anchor => "newcomment")
102       @replyurl = url_for(:controller => "message",
103                           :action => "new",
104                           :display_name => comment.user.display_name,
105                           :title => "Re: #{comment.diary_entry.title}")
106
107       @author = @from_user
108
109       attach_user_avatar(comment.user)
110
111       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
112            :to => recipient.email,
113            :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
114     end
115   end
116
117   def friend_notification(friend)
118     with_recipient_locale friend.befriendee do
119       @friend = friend
120       @viewurl = url_for(:controller => "user", :action => "view",
121                          :display_name => @friend.befriender.display_name)
122       @friendurl = url_for(:controller => "user", :action => "make_friend",
123                            :display_name => @friend.befriender.display_name)
124       @author = @friend.befriender.display_name
125
126       attach_user_avatar(@friend.befriender)
127       mail :to => friend.befriendee.email,
128            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
129     end
130   end
131
132   def note_comment_notification(comment, recipient)
133     with_recipient_locale recipient do
134       @noteurl = browse_note_url(comment.note)
135       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
136       @comment = comment.body
137       @owner = recipient == comment.note.author
138       @event = comment.event
139
140       @commenter = if comment.author
141                      comment.author.display_name
142                    else
143                      I18n.t("notifier.note_comment_notification.anonymous")
144                    end
145
146       @author = @commenter
147       attach_user_avatar(comment.author)
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       @to_user = recipient.display_name
162       @changeset_url = changeset_url(comment.changeset)
163       @comment = comment.body
164       @owner = recipient == comment.changeset.user
165       @commenter = comment.author.display_name
166       @changeset_comment = comment.changeset.tags["comment"].presence
167       @time = comment.created_at
168       @changeset_author = comment.changeset.user.display_name
169       @author = @commenter
170
171       subject = if @owner
172                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
173                 else
174                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
175                 end
176
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
187   end
188
189   def attach_project_logo
190     attachments.inline["logo.png"] = File.read(Rails.root.join("app", "assets", "images", "osm_logo_30.png"))
191   end
192
193   def attach_user_avatar(user)
194     attachments.inline["avatar.png"] = File.read(user_avatar_file_path(user))
195   end
196
197   def user_avatar_file_path(user)
198     image = user && user.image
199     if image && image.file?
200       return image.path(:small)
201     else
202       return Rails.root.join("app", "assets", "images", "users", "images", "small.png")
203     end
204   end
205
206   def with_recipient_locale(recipient)
207     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
208       yield
209     end
210   end
211
212   def from_address(name, type, id, digest, user_id = nil)
213     if Object.const_defined?(:MESSAGES_DOMAIN) && domain = MESSAGES_DOMAIN
214       if user_id
215         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
216       else
217         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
218       end
219     else
220       EMAIL_FROM
221     end
222   end
223 end