From a2043671ce41096c0a80ad655cdc13ee0cc75cf5 Mon Sep 17 00:00:00 2001
From: Raphael Sofaer <raphael@joindiaspora.com>
Date: Thu, 11 Aug 2011 10:45:28 -0700
Subject: [PATCH] Move comment email subject line code into models

---
 app/mailers/notifier.rb              | 9 ++++++---
 app/models/activity_streams/photo.rb | 4 ++++
 app/models/post.rb                   | 4 ++++
 app/models/reshare.rb                | 3 +++
 app/models/status_message.rb         | 4 ++++
 config/locales/diaspora/en.yml       | 2 ++
 6 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb
index 35249586ee..76387c56dc 100644
--- a/app/mailers/notifier.rb
+++ b/app/mailers/notifier.rb
@@ -74,7 +74,7 @@ class Notifier < ActionMailer::Base
     I18n.with_locale(@receiver.language) do
       mail(:from => "\"#{@sender.name} (Diaspora)\" <#{AppConfig[:smtp_sender_address]}>",
            :to => "\"#{@receiver.name}\" <#{@receiver.email}>",
-           :subject => "Re: #{post_message(@comment.parent, :length => TRUNCATION_LEN)}")
+           :subject => "Re: #{comment_email_subject}")
     end
   end
 
@@ -89,13 +89,16 @@ class Notifier < ActionMailer::Base
     log_mail(recipient_id, sender_id, 'comment_on_post')
 
     I18n.with_locale(@receiver.language) do
-      subject_message = post_message(@comment.parent, :length => TRUNCATION_LEN)
       mail(:from => "\"#{@sender.name} (Diaspora)\" <#{AppConfig[:smtp_sender_address]}>",
            :to => "\"#{@receiver.name}\" <#{@receiver.email}>",
-           :subject => "Re: #{subject_message}")
+           :subject => "Re: #{comment_email_subject}")
     end
   end
 
+  def comment_email_subject
+    truncate(@comment.parent.comment_email_subject, :length => TRUNCATION_LEN)
+  end
+
   def private_message(recipient_id, sender_id, message_id)
     @receiver = User.find_by_id(recipient_id)
     @sender   = Person.find_by_id(sender_id)
diff --git a/app/models/activity_streams/photo.rb b/app/models/activity_streams/photo.rb
index c45cbbfdba..1d67dc9575 100644
--- a/app/models/activity_streams/photo.rb
+++ b/app/models/activity_streams/photo.rb
@@ -53,5 +53,9 @@ class ActivityStreams::Photo < Post
   # A better solution is needed.
   # @return [Boolean] true
   def activity_streams?; true; end
+
+  def comment_email_subject
+    I18n.t("photos.comment_email_subject", :name => author.name)
+  end
 end
 
diff --git a/app/models/post.rb b/app/models/post.rb
index 1141f2fa01..b6d8b08cba 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -117,6 +117,10 @@ class Post < ActiveRecord::Base
     false
   end
 
+  def comment_email_subject
+    I18n.t('notifier.a_post_you_shared')
+  end
+
   # @return [Array<Comment>]
   def last_three_comments
     self.comments.order('created_at DESC').limit(3).includes(:author => :profile).reverse
diff --git a/app/models/reshare.rb b/app/models/reshare.rb
index f235697911..f82769a3c2 100644
--- a/app/models/reshare.rb
+++ b/app/models/reshare.rb
@@ -31,6 +31,9 @@ class Reshare < Post
     end
   end
 
+  def comment_email_subject
+    I18n.t('reshares.comment_email_subject', :resharer => author.name, :author => root.author.name)
+  end
   private
 
   def after_parse
diff --git a/app/models/status_message.rb b/app/models/status_message.rb
index 58ed733bb6..c083862342 100644
--- a/app/models/status_message.rb
+++ b/app/models/status_message.rb
@@ -132,6 +132,10 @@ class StatusMessage < Post
     end
   end
 
+  def comment_email_subject
+    formatted_message(:plain_text => true)
+  end
+
   protected
 
   def message_or_photos_present?
diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml
index 453c80f759..d934e2d939 100644
--- a/config/locales/diaspora/en.yml
+++ b/config/locales/diaspora/en.yml
@@ -533,6 +533,7 @@ en:
     new_profile_photo:
       upload: "Upload a new profile photo!"
       or_select_one: "or select one from your already existing"
+    comment_email_subject: "%{name}'s photo"
 
   posts:
     show:
@@ -621,6 +622,7 @@ en:
       deleted: "Original post deleted by author."
     create:
       failure: "There was an error resharing this post."
+    comment_email_subject: "%{resharer}'s reshare of %{author}'s post"
   services:
     index:
       logged_in_as: "logged in as"
-- 
GitLab