1 class Notifier < ActionMailer::Base
 
   2   default :from => EMAIL_FROM,
 
   3           :return_path => EMAIL_RETURN_PATH,
 
   4           :auto_submitted => "auto-generated"
 
   6   before_action :set_shared_template_vars
 
   7   before_action :attach_project_logo
 
   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)
 
  16       mail :to => user.email,
 
  17            :subject => I18n.t("notifier.signup_confirm.subject")
 
  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)
 
  28       mail :to => user.new_email,
 
  29            :subject => I18n.t("notifier.email_confirm.subject")
 
  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)
 
  39       mail :to => user.email,
 
  40            :subject => I18n.t("notifier.lost_password.subject")
 
  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
 
  52       mail :to => trace.user.email,
 
  53            :subject => I18n.t("notifier.gpx_notification.success.subject")
 
  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
 
  64       mail :to => trace.user.email,
 
  65            :subject => I18n.t("notifier.gpx_notification.failure.subject")
 
  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
 
  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)
 
  83       attach_user_avatar(message.sender)
 
  85       mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
 
  86            :to => message.recipient.email,
 
  87            :subject => I18n.t("notifier.message_notification.subject_header", :subject => message.title)
 
  91   def diary_comment_notification(comment, recipient)
 
  92     with_recipient_locale recipient do
 
  93       @to_user = recipient.display_name
 
  94       @from_user = comment.user.display_name
 
  96       @title = comment.diary_entry.title
 
  97       @readurl = url_for(:host => SERVER_URL,
 
  98                          :controller => "diary_entry",
 
 100                          :display_name => comment.diary_entry.user.display_name,
 
 101                          :id => comment.diary_entry.id,
 
 102                          :anchor => "comment#{comment.id}")
 
 103       @commenturl = url_for(:host => SERVER_URL,
 
 104                             :controller => "diary_entry",
 
 106                             :display_name => comment.diary_entry.user.display_name,
 
 107                             :id => comment.diary_entry.id,
 
 108                             :anchor => "newcomment")
 
 109       @replyurl = url_for(:host => SERVER_URL,
 
 110                           :controller => "message",
 
 112                           :display_name => comment.user.display_name,
 
 113                           :title => "Re: #{comment.diary_entry.title}")
 
 116       attach_user_avatar(comment.user)
 
 118       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
 
 119            :to => recipient.email,
 
 120            :subject => I18n.t("notifier.diary_comment_notification.subject", :user => comment.user.display_name)
 
 124   def friend_notification(friend)
 
 125     with_recipient_locale friend.befriendee do
 
 127       @viewurl = url_for(:host => SERVER_URL,
 
 128                          :controller => "user", :action => "view",
 
 129                          :display_name => @friend.befriender.display_name)
 
 130       @friendurl = url_for(:host => SERVER_URL,
 
 131                            :controller => "user", :action => "make_friend",
 
 132                            :display_name => @friend.befriender.display_name)
 
 133       @author = @friend.befriender.display_name
 
 135       attach_user_avatar(@friend.befriender)
 
 136       mail :to => friend.befriendee.email,
 
 137            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
 
 141   def note_comment_notification(comment, recipient)
 
 142     with_recipient_locale recipient do
 
 143       @noteurl = browse_note_url(comment.note, :host => SERVER_URL)
 
 144       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
 
 145       @comment = comment.body
 
 146       @owner = recipient == comment.note.author
 
 147       @event = comment.event
 
 149       @commenter = if comment.author
 
 150                      comment.author.display_name
 
 152                      I18n.t("notifier.note_comment_notification.anonymous")
 
 156       attach_user_avatar(comment.author)
 
 159                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
 
 161                   I18n.t("notifier.note_comment_notification.#{@event}.subject_other", :commenter => @commenter)
 
 164       mail :to => recipient.email, :subject => subject
 
 168   def changeset_comment_notification(comment, recipient)
 
 169     with_recipient_locale recipient do
 
 170       @to_user = recipient.display_name
 
 171       @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
 
 172       @comment = comment.body
 
 173       @owner = recipient == comment.changeset.user
 
 174       @commenter = comment.author.display_name
 
 175       @changeset_comment = comment.changeset.tags["comment"].presence
 
 176       @time = comment.created_at
 
 177       @changeset_author = comment.changeset.user.display_name
 
 181                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
 
 183                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
 
 186       attach_user_avatar(comment.author)
 
 188       mail :to => recipient.email, :subject => subject
 
 194   def set_shared_template_vars
 
 195     @root_url = root_url(:host => SERVER_URL)
 
 198   def attach_project_logo
 
 199     attachments.inline["logo.png"] = File.read(Rails.root.join("app", "assets", "images", "osm_logo_30.png"))
 
 202   def attach_user_avatar(user)
 
 203     attachments.inline["avatar.png"] = File.read(user_avatar_file_path(user))
 
 206   def user_avatar_file_path(user)
 
 207     image = user && user.image
 
 208     if image && image.file?
 
 209       return image.path(:small)
 
 211       return Rails.root.join("app", "assets", "images", "users", "images", "small.png")
 
 215   def with_recipient_locale(recipient)
 
 216     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
 
 221   def from_address(name, type, id, digest, user_id = nil)
 
 222     if Object.const_defined?(:MESSAGES_DOMAIN) && domain = MESSAGES_DOMAIN
 
 224         "#{name} <#{type}-#{id}-#{user_id}-#{digest[0, 6]}@#{domain}>"
 
 226         "#{name} <#{type}-#{id}-#{digest[0, 6]}@#{domain}>"