Work in Progress: Reply By Email:

- Add support classes and settings to enable reply by email
- Split out Email builder to be more OO, add many specs
This commit is contained in:
Robin Ward
2013-06-10 16:46:08 -04:00
parent e338e97fa3
commit e29f4a3496
25 changed files with 400 additions and 96 deletions

View File

@ -1,32 +0,0 @@
# Help us build an email
module Email
module Builder
def build_email(to, email_key, params={})
params[:site_name] = SiteSetting.title
params[:base_url] = Discourse.base_url
params[:user_preferences_url] = "#{Discourse.base_url}/user_preferences"
body = I18n.t("#{email_key}.text_body_template", params)
# Are we appending an unsubscribe link?
if params[:add_unsubscribe_link]
body << "\n"
body << I18n.t("unsubscribe_link", params)
headers 'List-Unsubscribe' => "<#{params[:user_preferences_url]}>"
end
mail_args = {
to: to,
subject: I18n.t("#{email_key}.subject_template", params),
body: body
}
mail_args[:from] = params[:from] || SiteSetting.notification_email
mail_args[:charset] = 'UTF-8'
mail(mail_args)
end
end
end

View File

@ -0,0 +1,19 @@
module Email
class IncomingMessage
attr_reader :reply_key,
:body_plain
def initialize(reply_key, body)
@reply_key = reply_key
@body = body
end
def reply
@reply ||= EmailReplyParser.read(@body).visible_text
end
end
end

View File

@ -0,0 +1,68 @@
# Builds a Mail::Mesage we can use for sending. Optionally supports using a template
# for the body and subject
module Email
module BuildEmailHelper
def build_email(*builder_args)
builder = Email::MessageBuilder.new(*builder_args)
headers(builder.header_args) if builder.header_args.present?
mail(builder.build_args)
end
end
class MessageBuilder
def initialize(to, opts=nil)
@to = to
@opts = opts || {}
end
def subject
subject = @opts[:subject]
subject = I18n.t("#{@opts[:template]}.subject_template", template_args) if @opts[:template]
subject
end
def body
body = @opts[:body]
body = I18n.t("#{@opts[:template]}.text_body_template", template_args) if @opts[:template]
if @opts[:add_unsubscribe_link]
body << "\n"
body << I18n.t('unsubscribe_link', template_args)
end
body
end
def template_args
@template_args ||= { site_name: SiteSetting.title,
base_url: Discourse.base_url,
user_preferences_url: "#{Discourse.base_url}/user_preferences" }.merge!(@opts)
end
def build_args
mail_args = { to: @to,
subject: subject,
body: body,
charset: 'UTF-8' }
mail_args[:from] = @opts[:from] || SiteSetting.notification_email
if @opts[:from_alias]
mail_args[:from] = "#{@opts[:from_alias]} <#{mail_args[:from]}>"
end
mail_args
end
def header_args
result = {}
if @opts[:add_unsubscribe_link]
result['List-Unsubscribe'] = "<#{template_args[:user_preferences_url]}>" if @opts[:add_unsubscribe_link]
end
result
end
end
end

30
lib/email/receiver.rb Normal file
View File

@ -0,0 +1,30 @@
#
# Handles an incoming message
#
require_dependency 'email/incoming_message'
module Email
class Receiver
def self.results
@results ||= Enum.new(:unprocessable)
end
def initialize(incoming_message)
@incoming_message = incoming_message
end
def process
if @incoming_message.blank? || @incoming_message.reply_key.blank?
return Email::Receiver.results[:unprocessable]
end
log = EmailLog.where(reply_key: @incoming_message.reply_key).first
return Email::Receiver.results[:unprocessable] if log.blank?
nil
end
end
end