diaspora/app/mailers/notification_mailers/base.rb
Jonne Haß 8280556a47 Introduce message renderer
This new class replaces all existing server side message
rendering helpers and is the new global entry point for such
needs. All models with relevant fields now expose an instance
of MessageRenderer for those. MessageRenderer acts as
gateway between the existing processing solutions for markdown,
mentions and tags and provides a very flexible interface for
all output needs. This makes the API to obtain a message
in a certain format clear. As a result of centralizing the
processing a lot of duplication is eliminated. Centralizing
the message processing also makes it clear where to change
its behaviour, add new representations and what options
are already available.
2014-03-15 17:16:17 +01:00

59 lines
1.7 KiB
Ruby

module NotificationMailers
class Base
attr_accessor :recipient, :sender
delegate :unconfirmed_email, :confirm_email_token,
:first_name, to: :recipient, prefix: true
delegate :first_name, :name, :sender, to: :sender, prefix: true
def initialize(recipient_id, sender_id=nil, *args)
@headers = {}
@recipient = User.find_by_id(recipient_id)
@sender = Person.find_by_id(sender_id) if sender_id.present?
log_mail(recipient_id, sender_id, self.class.to_s.underscore)
with_recipient_locale do
set_headers(*args)
end
end
def headers
default_headers.merge(@headers)
end
def name_and_address(name, email)
address = Mail::Address.new email
address.display_name = name
address.format
end
private
def default_headers
headers = {
:from => AppConfig.mail.sender_address.get,
:host => "#{AppConfig.pod_uri.host}",
:to => name_and_address(@recipient.name, @recipient.email)
}
headers[:from] = "\"#{@sender.name} (diaspora*)\" <#{AppConfig.mail.sender_address}>" if @sender.present?
headers
end
def with_recipient_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