Skip to content
Extraits de code Groupes Projets
Valider 8c15ca2a rédigé par Dan Hansen's avatar Dan Hansen
Parcourir les fichiers

PMs and comments contain author name in "From" header

refactor notifier
parent 8a5d76de
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 199 ajouts et 112 suppressions
......@@ -19,7 +19,7 @@ module NotifierHelper
# @return [String] The truncated and formatted comment.
def comment_message(comment, opts={})
opts[:length] ||= 600
text = truncate(@comment.text, :length => opts[:length])
text = truncate(comment.text, :length => opts[:length])
text = process_newlines(text) if opts[:process_newlines]
text
end
......
module NotificationMailers
class AlsoCommented < NotificationMailers::Base
include ActionView::Helpers::TextHelper
attr_accessor :comment
def set_headers(comment_id)
@comment = Comment.find_by_id(comment_id)
if mail?
@headers[:from] = "[#{@comment.author.name} (Diaspora)] <#{AppConfig[:smtp_sender_address]}>"
@headers[:subject] = truncate(@comment.parent.comment_email_subject, :length => TRUNCATION_LEN)
@headers[:subject] = "Re: #{@headers[:subject]}"
end
end
def mail?
@recipient && @sender && @comment
end
end
end
\ No newline at end of file
module NotificationMailers
TRUNCATION_LEN = 70
class Base
attr_accessor :recipient, :recipient_name, :sender
def initialize(recipient_id, sender_id=nil, *args)
@headers = {}
@recipient = User.find_by_id(recipient_id)
@recipient_name = @recipient.profile.first_name
@sender = Person.find_by_id(sender_id) if sender_id.present?
log_mail(recipient_id, sender_id, self.class.to_s.underscore)
with_locale do
set_headers(*args)
end
end
def headers
default_headers.merge(@headers)
end
private
def default_headers
headers = {
:host => "#{AppConfig[:pod_uri]}",
:to => "\"#{@recipient.name}\" <#{@recipient.email}>"
}
headers[:from] = "\"#{@sender.name} (Diaspora)\" <#{AppConfig[:smtp_sender_address]}>" if @sender.present?
headers
end
def with_locale(&block)
I18n.with_locale(@recipient.language, &block)
end
def log_mail(recipient_id, sender_id, type)
log_string = "event=mail mail_type=#{type} recipient_id=#{recipient_id} sender_id=#{sender_id}"
if @recipient && @sender
log_string << "models_found=true sender_handle=#{@sender.diaspora_handle} recipient_handle=#{@recipient.diaspora_handle}"
else
log_string << "models_found=false"
end
Rails.logger.info(log_string)
end
end
end
\ No newline at end of file
module NotificationMailers
class CommentOnPost < NotificationMailers::Base
include ActionView::Helpers::TextHelper
attr_accessor :comment
def set_headers(comment_id)
@comment = Comment.find(comment_id)
@headers[:from] = "[#{@comment.author.name} (Diaspora)] <#{AppConfig[:smtp_sender_address]}>"
@headers[:subject] = truncate(@comment.parent.comment_email_subject, :length => TRUNCATION_LEN)
@headers[:subject] = "Re: #{@headers[:subject]}"
end
end
end
\ No newline at end of file
module NotificationMailers
class ConfirmEmail < NotificationMailers::Base
def set_headers
@headers[:to] = "\"#{recipient_name}\" <#{@recipient.unconfirmed_email}>"
@headers[:subject] = I18n.t('notifier.confirm_email.subject', :unconfirmed_email => @recipient.unconfirmed_email)
end
end
end
\ No newline at end of file
module NotificationMailers
class Liked < NotificationMailers::Base
attr_accessor :like
def set_headers(like_id)
@like = Like.find(like_id)
@headers[:subject] = I18n.t('notifier.liked.liked', :name => @sender.name)
end
end
end
\ No newline at end of file
module NotificationMailers
class Mentioned < NotificationMailers::Base
attr_accessor :post
def set_headers(target_id)
@post = Mention.find_by_id(target_id).post
@headers[:subject] = I18n.t('notifier.mentioned.subject', :name => @sender.name)
end
end
end
\ No newline at end of file
module NotificationMailers
class PrivateMessage < NotificationMailers::Base
attr_accessor :message, :conversation, :participants
def set_headers(message_id)
@message = Message.find_by_id(message_id)
@conversation = @message.conversation
@participants = @conversation.participants
@headers[:from] = "[#{@message.author.name} (Diaspora)] <#{AppConfig[:smtp_sender_address]}>"
@headers[:subject] = @conversation.subject.strip
@headers[:subject] = "Re: #{@headers[:subject]}" if @conversation.messages.size > 1
end
end
end
\ No newline at end of file
module NotificationMailers
class Reshared < NotificationMailers::Base
attr_accessor :reshare
def set_headers(reshare_id)
@reshare = Reshare.find(reshare_id)
@headers[:subject] = I18n.t('notifier.reshared.reshared', :name => @sender.name)
end
end
end
\ No newline at end of file
module NotificationMailers
class StartedSharing < NotificationMailers::Base
def set_headers
@headers[:subject] = I18n.t('notifier.started_sharing.subject', :name => @sender.name)
end
end
end
\ No newline at end of file
......@@ -5,11 +5,8 @@ class Notifier < ActionMailer::Base
default :from => AppConfig[:smtp_sender_address]
include ActionView::Helpers::TextHelper
include NotifierHelper
TRUNCATION_LEN = 70
def self.admin(string, recipients, opts = {})
mails = []
recipients.each do |rec|
......@@ -27,133 +24,71 @@ class Notifier < ActionMailer::Base
end
def started_sharing(recipient_id, sender_id)
@receiver = User.find_by_id(recipient_id)
@sender = Person.find_by_id(sender_id)
log_mail(recipient_id, sender_id, 'started_sharing')
@notification = NotificationMailers::StartedSharing.new(recipient_id, sender_id)
I18n.with_locale(@receiver.language) do
mail(:to => "\"#{@receiver.name}\" <#{@receiver.email}>",
:subject => I18n.t('notifier.started_sharing.subject', :name => @sender.name), :host => AppConfig[:pod_uri].host)
with_recipient_locale do
mail(@notification.headers)
end
end
def liked(recipient_id, sender_id, like_id)
@receiver = User.find_by_id(recipient_id)
@sender = Person.find_by_id(sender_id)
@like = Like.find(like_id)
log_mail(recipient_id, sender_id, 'liked')
@notification = NotificationMailers::Liked.new(recipient_id, sender_id, like_id)
I18n.with_locale(@receiver.language) do
mail(:to => "\"#{@receiver.name}\" <#{@receiver.email}>",
:subject => I18n.t('notifier.liked.liked', :name => @sender.name), :host => AppConfig[:pod_uri].host)
with_recipient_locale do
mail(@notification.headers)
end
end
def reshared(recipient_id, sender_id, reshare_id)
@receiver = User.find_by_id(recipient_id)
@sender = Person.find_by_id(sender_id)
@reshare = Reshare.find(reshare_id)
@notification = NotificationMailers::Reshared.new(recipient_id, sender_id, reshare_id)
log_mail(recipient_id, sender_id, 'reshared')
I18n.with_locale(@receiver.language) do
mail(:to => "\"#{@receiver.name}\" <#{@receiver.email}>",
:subject => I18n.t('notifier.reshared.reshared', :name => @sender.name), :host => AppConfig[:pod_uri].host)
with_recipient_locale do
mail(@notification.headers)
end
end
def mentioned(recipient_id, sender_id, target_id)
@receiver = User.find_by_id(recipient_id)
@sender = Person.find_by_id(sender_id)
@post = Mention.find_by_id(target_id).post
log_mail(recipient_id, sender_id, 'mentioned')
@notification = NotificationMailers::Mentioned.new(recipient_id, sender_id, target_id)
I18n.with_locale(@receiver.language) do
mail(:to => "\"#{@receiver.name}\" <#{@receiver.email}>",
:subject => I18n.t('notifier.mentioned.subject', :name => @sender.name), :host => AppConfig[:pod_uri].host)
with_recipient_locale do
mail(@notification.headers)
end
end
def comment_on_post(recipient_id, sender_id, comment_id)
@receiver = User.find_by_id(recipient_id)
@sender = Person.find_by_id(sender_id)
@comment = Comment.find_by_id(comment_id)
@notification = NotificationMailers::CommentOnPost.new(recipient_id, sender_id, comment_id)
log_mail(recipient_id, sender_id, 'comment_on_post')
I18n.with_locale(@receiver.language) do
mail(:from => "\"#{@sender.name} (Diaspora)\" <#{AppConfig[:smtp_sender_address]}>",
:to => "\"#{@receiver.name}\" <#{@receiver.email}>",
:subject => "Re: #{comment_email_subject}")
with_recipient_locale do
mail(@notification.headers)
end
end
def also_commented(recipient_id, sender_id, comment_id)
@receiver = User.find_by_id(recipient_id)
@sender = Person.find_by_id(sender_id)
@comment = Comment.find_by_id(comment_id)
if @receiver && @sender && @comment
@post_author_name = @comment.post.author.name
log_mail(recipient_id, sender_id, 'comment_on_post')
@notification = NotificationMailers::AlsoCommented.new(recipient_id, sender_id, comment_id)
I18n.with_locale(@receiver.language) do
mail(:from => "\"#{@sender.name} (Diaspora)\" <#{AppConfig[:smtp_sender_address]}>",
:to => "\"#{@receiver.name}\" <#{@receiver.email}>",
:subject => "Re: #{comment_email_subject}")
end
with_recipient_locale do
mail(@notification.headers) if @notification.mail?
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)
@message = Message.find_by_id(message_id)
@conversation = @message.conversation
@participants = @conversation.participants
log_mail(recipient_id, sender_id, 'private_message')
@notification = NotificationMailers::PrivateMessage.new(recipient_id, sender_id, message_id)
subject = @conversation.subject.strip
subject = "Re: #{subject}" if @conversation.messages.size > 1
I18n.with_locale(@receiver.language) do
mail(:from => "\"#{@sender.name} (Diaspora)\" <#{AppConfig[:smtp_sender_address]}>",
:to => "\"#{@receiver.name}\" <#{@receiver.email}>",
:subject => subject)
with_recipient_locale do
mail(@notification.headers)
end
end
def confirm_email(receiver_id)
@receiver = User.find_by_id(receiver_id)
def confirm_email(recipient_id)
@notification = NotificationMailers::ConfirmEmail.new(recipient_id)
I18n.with_locale(@receiver.language) do
mail(:to => "\"#{@receiver.name}\" <#{@receiver.unconfirmed_email}>",
:subject => I18n.t('notifier.confirm_email.subject', :unconfirmed_email => @receiver.unconfirmed_email),
:host => AppConfig[:pod_uri].host)
with_recipient_locale do
mail(@notification.headers)
end
end
private
def log_mail recipient_id, sender_id, type
log_string = "event=mail mail_type=#{type} recipient_id=#{recipient_id} sender_id=#{sender_id}"
if @receiver && @sender
log_string << "models_found=true sender_handle=#{@sender.diaspora_handle} recipient_handle=#{@receiver.diaspora_handle}"
else
log_string << "models_found=false"
end
Rails.logger.info log_string
def with_recipient_locale(&block)
I18n.with_locale(@notification.recipient.language, &block)
end
end
%p
= comment_message(@comment.text, :process_newlines => true)
= comment_message(@notification.comment, :process_newlines => true)
%p
= link_to t('notifier.comment_on_post.reply', :name => @comment.post.author.first_name), post_url(@comment.post)
= link_to t('notifier.comment_on_post.reply', :name => @notification.comment.post.author.first_name), post_url(@notification.comment.post)
!= comment_message(@comment.text)
!= comment_message(@notification.comment)
%p
= comment_message(@comment.text, :process_newlines => true)
= comment_message(@notification.comment, :process_newlines => true)
%p
= link_to t('notifier.comment_on_post.reply', :name => @comment.post.author.name), post_url(@comment.post)
= link_to t('notifier.comment_on_post.reply', :name => @notification.comment.post.author.name), post_url(@notification.comment.post)
!= comment_message(@comment.text)
!= comment_message(@notification.comment)
%p
= t('notifier.hello', :name => @receiver.profile.first_name)
= t('notifier.hello', :name => @notification.recipient_name)
%p
!= t('notifier.confirm_email.click_link', :unconfirmed_email => @receiver.unconfirmed_email)
!= t('notifier.confirm_email.click_link', :unconfirmed_email => @notification.recipient.unconfirmed_email)
%br
= link_to confirm_email_url(:token => @receiver.confirm_email_token), confirm_email_url(:token => @receiver.confirm_email_token)
= link_to confirm_email_url(:token => @notification.recipient.confirm_email_token),
confirm_email_url(:token => @notification.recipient.confirm_email_token)
!= t('notifier.hello', :name => @receiver.profile.first_name)
!= t('notifier.hello', :name => @notification.recipient_name)
!= t('notifier.confirm_email.click_link', :unconfirmed_email => @receiver.unconfirmed_email)
!= confirm_email_url(:token => @receiver.confirm_email_token)
!= t('notifier.confirm_email.click_link', :unconfirmed_email => @notification.recipient.unconfirmed_email)
!= confirm_email_url(:token => @notification.recipient.confirm_email_token)
%p
= "#{t('.liked', :name => "#{@sender.name}")}:"
= "#{t('.liked', :name => "#{@notification.sender.name}")}:"
%p{:style => "font-style:italic;color:#666"}
= post_message(@like.target, :process_newlines => true)
= post_message(@notification.like.target, :process_newlines => true)
%p
= link_to t('.view_post'), post_url(@like.target)
= link_to t('.view_post'), post_url(@notification.like.target)
!= "#{t('notifier.liked.liked', :name => "#{@sender.name}")}:"
!=post_message(@like.target)
!= "#{t('notifier.liked.liked', :name => "#{@notification.sender.name}")}:"
!=post_message(@notification.like.target)
%p
= post_message(@post, :process_newlines => true, :length => 600)
= post_message(@notification.post, :process_newlines => true, :length => 600)
%p
= link_to t('notifier.comment_on_post.reply', :name => @post.author.name), post_url(@post)
= link_to t('notifier.comment_on_post.reply', :name => @notification.post.author.name), post_url(@notification.post)
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter