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