Better loading of attached images
[rails.git] / app / models / notifier.rb
1 require "base64"
2
3 class Notifier < ActionMailer::Base
4   default :from => EMAIL_FROM,
5           :return_path => EMAIL_RETURN_PATH,
6           :auto_submitted => "auto-generated"
7   helper :application
8
9   def signup_confirm(user, token)
10     with_recipient_locale user do
11       @url = url_for(:host => SERVER_URL,
12                      :controller => "user", :action => "confirm",
13                      :display_name => user.display_name,
14                      :confirm_string => token.token)
15
16       mail :to => user.email,
17            :subject => I18n.t("notifier.signup_confirm.subject")
18     end
19   end
20
21   def email_confirm(user, token)
22     with_recipient_locale user do
23       @address = user.new_email
24       @url = url_for(:host => SERVER_URL,
25                      :controller => "user", :action => "confirm_email",
26                      :confirm_string => token.token)
27
28       mail :to => user.new_email,
29            :subject => I18n.t("notifier.email_confirm.subject")
30     end
31   end
32
33   def lost_password(user, token)
34     with_recipient_locale user do
35       @url = url_for(:host => SERVER_URL,
36                      :controller => "user", :action => "reset_password",
37                      :token => token.token)
38
39       mail :to => user.email,
40            :subject => I18n.t("notifier.lost_password.subject")
41     end
42   end
43
44   def gpx_success(trace, possible_points)
45     with_recipient_locale trace.user do
46       @trace_name = trace.name
47       @trace_points = trace.size
48       @trace_description = trace.description
49       @trace_tags = trace.tags
50       @possible_points = possible_points
51
52       mail :to => trace.user.email,
53            :subject => I18n.t("notifier.gpx_notification.success.subject")
54     end
55   end
56
57   def gpx_failure(trace, error)
58     with_recipient_locale trace.user do
59       @trace_name = trace.name
60       @trace_description = trace.description
61       @trace_tags = trace.tags
62       @error = error
63
64       mail :to => trace.user.email,
65            :subject => I18n.t("notifier.gpx_notification.failure.subject")
66     end
67   end
68
69   def message_notification(message)
70     with_recipient_locale message.recipient do
71       @to_user = message.recipient.display_name
72       @from_user = message.sender.display_name
73       @text = message.body
74       @title = message.title
75       @readurl = url_for(:host => SERVER_URL,
76                          :controller => "message", :action => "read",
77                          :message_id => message.id)
78       @replyurl = url_for(:host => SERVER_URL,
79                           :controller => "message", :action => "reply",
80                           :message_id => message.id)
81
82       mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
83            :to => message.recipient.email,
84            :subject => I18n.t("notifier.message_notification.subject_header", :subject => message.title)
85     end
86   end
87
88   def diary_comment_notification(comment, recipient)
89     with_recipient_locale recipient do
90       @to_user = recipient.display_name
91       @from_user = comment.user.display_name
92       @text = comment.body
93       @title = comment.diary_entry.title
94       @readurl = url_for(:host => SERVER_URL,
95                          :controller => "diary_entry",
96                          :action => "view",
97                          :display_name => comment.diary_entry.user.display_name,
98                          :id => comment.diary_entry.id,
99                          :anchor => "comment#{comment.id}")
100       @commenturl = 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 => "newcomment")
106       @replyurl = url_for(:host => SERVER_URL,
107                           :controller => "message",
108                           :action => "new",
109                           :display_name => comment.user.display_name,
110                           :title => "Re: #{comment.diary_entry.title}")
111
112       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
113            :to => recipient.email,
114            :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
115     end
116   end
117
118   def friend_notification(friend)
119     with_recipient_locale friend.befriendee do
120       @friend = friend
121       @viewurl = url_for(:host => SERVER_URL,
122                          :controller => "user", :action => "view",
123                          :display_name => @friend.befriender.display_name)
124       @friendurl = url_for(:host => SERVER_URL,
125                            :controller => "user", :action => "make_friend",
126                            :display_name => @friend.befriender.display_name)
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       @commenter = if comment.author
142                      comment.author.display_name
143                    else
144                      I18n.t("notifier.note_comment_notification.anonymous")
145                    end
146
147       subject = if @owner
148                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
149                 else
150                   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   def changeset_comment_notification(comment, recipient)
158     with_recipient_locale recipient do
159       @root_url = root_url(:host => SERVER_URL)
160       @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
161       @comment = comment.body
162       @owner = recipient == comment.changeset.user
163       @commenter = comment.author.display_name
164       @commenter_url = user_url(comment.author.display_name, :host => SERVER_URL)
165       @commenter_thumbnail_src = comment.author.image.url(:small, :host => SERVER_URL)
166       @changeset_comment = comment.changeset.tags["comment"].presence
167       @time = comment.created_at
168       @changeset_author = comment.changeset.user.display_name
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       attachments.inline['logo.png'] = File.read(Rails.root.join('app', 'assets', 'images', 'osm_logo_30.png'))
177       attachments.inline['avatar.png'] = File.read(user_avatar_file_path(comment.author))
178
179       mail :to => recipient.email, :subject => subject
180     end
181   end
182
183   private
184
185   def user_avatar_file_path(user)
186     image = user.image
187     if image.file?
188       return image.path.sub('/original/', '/small/')
189     else
190       return Rails.root.join('app', 'assets', 'images', 'users', 'images', 'small.png')
191     end
192   end
193
194   def with_recipient_locale(recipient)
195     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
196       yield
197     end
198   end
199
200   def from_address(name, type, id, digest, user_id = nil)
201     if Object.const_defined?(:MESSAGES_DOMAIN) && domain = MESSAGES_DOMAIN
202       if user_id
203         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
204       else
205         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"
206       end
207     else
208       EMAIL_FROM
209     end
210   end
211
212 end