1 # frozen_string_literal: true
 
   3 class UserMailer < ApplicationMailer
 
   4   include ActionView::Helpers::AssetUrlHelper
 
   6   self.delivery_job = ActionMailer::MailDeliveryJob
 
   8   default :from => Settings.email_from,
 
   9           :return_path => Settings.email_return_path,
 
  10           :auto_submitted => "auto-generated"
 
  12   before_action :set_shared_template_vars
 
  13   before_action :attach_project_logo
 
  15   def signup_confirm(user, token, referer = nil)
 
  16     with_recipient_locale user do
 
  17       @url = url_for(:controller => "confirmations", :action => "confirm",
 
  18                      :display_name => user.display_name,
 
  19                      :confirm_string => token,
 
  22       mail :to => user.email,
 
  23            :subject => t(".subject")
 
  27   def email_confirm(user, token)
 
  28     with_recipient_locale user do
 
  29       @address = user.new_email
 
  30       @url = url_for(:controller => "confirmations", :action => "confirm_email",
 
  31                      :confirm_string => token)
 
  33       mail :to => user.new_email,
 
  34            :subject => t(".subject")
 
  38   def lost_password(user, token)
 
  39     with_recipient_locale user do
 
  40       @url = user_reset_password_url(:token => token)
 
  42       mail :to => user.email,
 
  43            :subject => t(".subject")
 
  47   def gpx_success(trace, possible_points)
 
  48     with_recipient_locale trace.user do
 
  49       @to_user = trace.user.display_name
 
  50       @trace_url = show_trace_url(trace.user, trace)
 
  51       @trace_name = trace.name
 
  52       @trace_points = trace.size
 
  53       @trace_description = trace.description
 
  54       @trace_tags = trace.tags
 
  55       @possible_points = possible_points
 
  56       @my_traces_url = url_for(:controller => "traces", :action => "mine")
 
  58       mail :to => trace.user.email,
 
  59            :subject => t(".subject")
 
  63   def gpx_failure(trace, error)
 
  64     with_recipient_locale trace.user do
 
  65       @to_user = trace.user.display_name
 
  66       @trace_name = trace.name
 
  67       @trace_description = trace.description
 
  68       @trace_tags = trace.tags
 
  71       mail :to => trace.user.email,
 
  72            :subject => t(".subject")
 
  76   def message_notification(message)
 
  77     with_recipient_locale message.recipient do
 
  78       @to_user = message.recipient.display_name
 
  79       @from_user = message.sender.display_name
 
  81       @title = message.title
 
  82       @readurl = message_url(message)
 
  83       @replyurl = new_message_reply_url(message)
 
  86       attach_user_avatar(message.sender)
 
  88       mail :from => from_address(message.sender.display_name, "m", message.id, message.notification_token),
 
  89            :to => message.recipient.email,
 
  90            :subject => t(".subject", :message_title => message.title)
 
  94   def diary_comment_notification(comment, recipient)
 
  95     with_recipient_locale recipient do
 
  96       @to_user = recipient.display_name
 
  97       @from_user = comment.user.display_name
 
  99       @title = comment.diary_entry.title
 
 100       @readurl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "comment#{comment.id}")
 
 101       @commenturl = diary_entry_url(comment.diary_entry.user, comment.diary_entry, :anchor => "newcomment")
 
 102       @replyurl = new_message_url(comment.user, :message => { :title => "Re: #{comment.diary_entry.title}" })
 
 103       @unsubscribeurl = diary_entry_unsubscribe_url(comment.diary_entry.user, comment.diary_entry)
 
 106       attach_user_avatar(comment.user)
 
 108       set_references("diary", comment.diary_entry)
 
 111         "#{comment.diary_entry.id}.diary.www.openstreetmap.org",
 
 112         t(".description", :id => comment.diary_entry.id),
 
 113         :archive => @readurl,
 
 114         :subscribe => diary_entry_subscribe_url(comment.diary_entry.user, comment.diary_entry),
 
 115         :unsubscribe => @unsubscribeurl
 
 118       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.notification_token(recipient.id), recipient.id),
 
 119            :to => recipient.email,
 
 120            :subject => t(".subject", :user => comment.user.display_name)
 
 124   def follow_notification(follow)
 
 125     with_recipient_locale follow.following do
 
 127       @viewurl = user_url(@follow.follower)
 
 128       @followurl = follow_url(@follow.follower)
 
 129       @author = @follow.follower.display_name
 
 131       attach_user_avatar(@follow.follower)
 
 132       mail :to => follow.following.email,
 
 133            :subject => t(".subject", :user => follow.follower.display_name)
 
 137   def note_comment_notification(comment, recipient)
 
 138     with_recipient_locale recipient do
 
 139       @noteurl = note_url(comment.note)
 
 140       @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, I18n.locale)
 
 141       @comment = comment.body
 
 142       @owner = recipient == comment.note.author
 
 143       @event = comment.event
 
 145       @commenter = if comment.author
 
 146                      comment.author.display_name
 
 152       attach_user_avatar(comment.author)
 
 154       set_references("note", comment.note)
 
 157         "#{comment.note.id}.note.www.openstreetmap.org",
 
 158         t(".description", :id => comment.note.id),
 
 163                   t(".#{@event}.subject_own", :commenter => @commenter)
 
 165                   t(".#{@event}.subject_other", :commenter => @commenter)
 
 168       mail :to => recipient.email, :subject => subject
 
 172   def changeset_comment_notification(comment, recipient)
 
 173     with_recipient_locale recipient do
 
 174       @to_user = recipient.display_name
 
 175       @changeset_url = changeset_url(comment.changeset)
 
 176       @comment = comment.body
 
 177       @owner = recipient == comment.changeset.user
 
 178       @commenter = comment.author.display_name
 
 179       @changeset_comment = comment.changeset.tags["comment"].presence
 
 180       @time = comment.created_at
 
 181       @changeset_author = comment.changeset.user.display_name
 
 182       @changeset_subscription_url = changeset_subscription_url(comment.changeset)
 
 186                   t(".commented.subject_own", :commenter => @commenter)
 
 188                   t(".commented.subject_other", :commenter => @commenter)
 
 191       attach_user_avatar(comment.author)
 
 193       set_references("changeset", comment.changeset)
 
 196         "#{comment.changeset.id}.changeset.www.openstreetmap.org",
 
 197         t(".description", :id => comment.changeset.id),
 
 198         :subscribe => @changeset_subscription_url,
 
 199         :unsubscribe => @changeset_subscription_url,
 
 200         :archive => @changeset_url
 
 203       mail :to => recipient.email, :subject => subject
 
 209   def set_shared_template_vars
 
 213   def attach_project_logo
 
 214     attachments.inline["logo.png"] = Rails.root.join("app/assets/images/osm_logo_30.png").read
 
 217   def attach_user_avatar(user)
 
 218     @avatar = user_avatar_filename(user)
 
 219     attachments.inline[@avatar] = user_avatar_file(user)
 
 222   def user_avatar_filename(user)
 
 223     avatar = user&.avatar
 
 225       case avatar.content_type
 
 226       when "image/png" then "avatar.png"
 
 227       when "image/jpeg" then "avatar.jpg"
 
 228       when "image/gif" then "avatar.gif"
 
 229       when "image/bmp" then "avatar.bmp"
 
 230       when "image/tiff" then "avatar.tif"
 
 231       when "image/svg+xml" then "avatar.svg"
 
 239   def user_avatar_file(user)
 
 240     avatar = user&.avatar
 
 243         avatar.variant(:resize_to_limit => [50, 50]).download
 
 248       Rails.root.join("app/assets/images/avatar_small.png").read
 
 252   def with_recipient_locale(recipient, &)
 
 253     I18n.with_locale(Locale.available.preferred(recipient.preferred_languages), &)
 
 256   def from_address(name, type, id, token, user_id = nil)
 
 257     if Settings.key?(:messages_domain) && domain = Settings.messages_domain
 
 259         "#{name} <#{type}-#{id}-#{user_id}-#{token}@#{domain}>"
 
 261         "#{name} <#{type}-#{id}-#{token}@#{domain}>"
 
 268   def set_references(scope, reference_object)
 
 269     ref = "osm-#{scope}-#{reference_object.id}@#{Settings.server_url}"
 
 271     headers["X-Entity-Ref-ID"] = ref
 
 272     headers["In-Reply-To"] = ref
 
 273     headers["References"] = ref
 
 276   def set_list_headers(id, description, options = {})
 
 277     headers["List-ID"] = "#{description} <#{id}>"
 
 278     headers["List-Archive"] = "<#{options[:archive]}>" if options[:archive]
 
 279     headers["List-Subscribe"] = "<#{options[:subscribe]}>" if options[:subscribe]
 
 280     headers["List-Unsubscribe"] = "<#{options[:unsubscribe]}>" if options[:unsubscribe]