]> git.openstreetmap.org Git - rails.git/blobdiff - app/models/notifier.rb
Merge remote-tracking branch 'openstreetmap/pull/1437'
[rails.git] / app / models / notifier.rb
index b53210c30e96df1c017fe1b9f62ba07584984f3b..8f9e3e2954814e45151632c0ba88ca0db8fa6dfd 100644 (file)
@@ -1,10 +1,10 @@
-require "base64"
-
 class Notifier < ActionMailer::Base
   default :from => EMAIL_FROM,
           :return_path => EMAIL_RETURN_PATH,
           :auto_submitted => "auto-generated"
   helper :application
 class Notifier < ActionMailer::Base
   default :from => EMAIL_FROM,
           :return_path => EMAIL_RETURN_PATH,
           :auto_submitted => "auto-generated"
   helper :application
+  before_action :set_shared_template_vars
+  before_action :attach_project_logo
 
   def signup_confirm(user, token)
     with_recipient_locale user do
 
   def signup_confirm(user, token)
     with_recipient_locale user do
@@ -78,6 +78,9 @@ class Notifier < ActionMailer::Base
       @replyurl = url_for(:host => SERVER_URL,
                           :controller => "message", :action => "reply",
                           :message_id => message.id)
       @replyurl = url_for(:host => SERVER_URL,
                           :controller => "message", :action => "reply",
                           :message_id => message.id)
+      @author = @from_user
+
+      attach_user_avatar(message.sender)
 
       mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
            :to => message.recipient.email,
 
       mail :from => from_address(message.sender.display_name, "m", message.id, message.digest),
            :to => message.recipient.email,
@@ -108,6 +111,9 @@ class Notifier < ActionMailer::Base
                           :action => "new",
                           :display_name => comment.user.display_name,
                           :title => "Re: #{comment.diary_entry.title}")
                           :action => "new",
                           :display_name => comment.user.display_name,
                           :title => "Re: #{comment.diary_entry.title}")
+      @author = @from_user
+
+      attach_user_avatar(comment.user)
 
       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
            :to => recipient.email,
 
       mail :from => from_address(comment.user.display_name, "c", comment.id, comment.digest, recipient.id),
            :to => recipient.email,
@@ -124,7 +130,9 @@ class Notifier < ActionMailer::Base
       @friendurl = url_for(:host => SERVER_URL,
                            :controller => "user", :action => "make_friend",
                            :display_name => @friend.befriender.display_name)
       @friendurl = url_for(:host => SERVER_URL,
                            :controller => "user", :action => "make_friend",
                            :display_name => @friend.befriender.display_name)
+      @author = @friend.befriender.display_name
 
 
+      attach_user_avatar(@friend.befriender)
       mail :to => friend.befriendee.email,
            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
     end
       mail :to => friend.befriendee.email,
            :subject => I18n.t("notifier.friend_notification.subject", :user => friend.befriender.display_name)
     end
@@ -144,6 +152,9 @@ class Notifier < ActionMailer::Base
                      I18n.t("notifier.note_comment_notification.anonymous")
                    end
 
                      I18n.t("notifier.note_comment_notification.anonymous")
                    end
 
+      @author = @commenter
+      attach_user_avatar(comment.author)
+
       subject = if @owner
                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
                 else
       subject = if @owner
                   I18n.t("notifier.note_comment_notification.#{@event}.subject_own", :commenter => @commenter)
                 else
@@ -156,16 +167,15 @@ class Notifier < ActionMailer::Base
 
   def changeset_comment_notification(comment, recipient)
     with_recipient_locale recipient do
 
   def changeset_comment_notification(comment, recipient)
     with_recipient_locale recipient do
-      @root_url = root_url(:host => SERVER_URL)
+      @to_user = recipient.display_name
       @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
       @comment = comment.body
       @owner = recipient == comment.changeset.user
       @commenter = comment.author.display_name
       @changeset_url = changeset_url(comment.changeset, :host => SERVER_URL)
       @comment = comment.body
       @owner = recipient == comment.changeset.user
       @commenter = comment.author.display_name
-      @commenter_url = user_url(comment.author.display_name, :host => SERVER_URL)
-      @commenter_thumbnail_src = comment.author.image.url(:small, :host => SERVER_URL)
       @changeset_comment = comment.changeset.tags["comment"].presence
       @time = comment.created_at
       @changeset_author = comment.changeset.user.display_name
       @changeset_comment = comment.changeset.tags["comment"].presence
       @time = comment.created_at
       @changeset_author = comment.changeset.user.display_name
+      @author = @commenter
 
       subject = if @owner
                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
 
       subject = if @owner
                   I18n.t("notifier.changeset_comment_notification.commented.subject_own", :commenter => @commenter)
@@ -173,11 +183,7 @@ class Notifier < ActionMailer::Base
                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
                 end
 
                   I18n.t("notifier.changeset_comment_notification.commented.subject_other", :commenter => @commenter)
                 end
 
-      attachments.inline['osm_logo_30x30.png'] = {
-        mime_type: 'image/png',
-        encoding: 'base64',
-        content: @@osm_logo_png_bytes,
-      }
+      attach_user_avatar(comment.author)
 
       mail :to => recipient.email, :subject => subject
     end
 
       mail :to => recipient.email, :subject => subject
     end
@@ -185,6 +191,27 @@ class Notifier < ActionMailer::Base
 
   private
 
 
   private
 
+  def set_shared_template_vars
+    @root_url = root_url(:host => SERVER_URL)
+  end
+
+  def attach_project_logo
+    attachments.inline["logo.png"] = File.read(Rails.root.join("app", "assets", "images", "osm_logo_30.png"))
+  end
+
+  def attach_user_avatar(user)
+    attachments.inline["avatar.png"] = File.read(user_avatar_file_path(user))
+  end
+
+  def user_avatar_file_path(user)
+    image = user && user.image
+    if image && image.file?
+      return image.path(:small)
+    else
+      return Rails.root.join("app", "assets", "images", "users", "images", "small.png")
+    end
+  end
+
   def with_recipient_locale(recipient)
     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
       yield
   def with_recipient_locale(recipient)
     I18n.with_locale Locale.available.preferred(recipient.preferred_languages) do
       yield
@@ -202,60 +229,4 @@ class Notifier < ActionMailer::Base
       EMAIL_FROM
     end
   end
       EMAIL_FROM
     end
   end
-
-  # FIXME - 2016-12-28 - saintamh - someone please show me a better way to
-  # include this blob in here. If I save it under app/assets, how do I get a
-  # path to it in a portable way?
-  @@osm_logo_png_bytes = <<EOS
-iVBORw0KGgoAAAANSUhEUgAAAB4AAAAeCAYAAAA7MK6iAAAABGdBTUEAALGPC/xhBQAAAAFzUkdC
-AK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAZiS0dE
-AP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