2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-10 15:00:27 -06:00
|
|
|
class IncomingEmailDetailsSerializer < ApplicationSerializer
|
|
|
|
|
|
|
|
attributes :error,
|
|
|
|
:error_description,
|
2016-03-07 09:56:17 -06:00
|
|
|
:rejection_message,
|
2016-03-30 12:05:42 -05:00
|
|
|
:headers,
|
2016-02-10 15:00:27 -06:00
|
|
|
:subject,
|
|
|
|
:body
|
|
|
|
|
|
|
|
def initialize(incoming_email, opts)
|
|
|
|
super
|
|
|
|
@error_string = incoming_email.error
|
|
|
|
@mail = Mail.new(incoming_email.raw)
|
|
|
|
end
|
|
|
|
|
2020-04-30 01:48:34 -05:00
|
|
|
EMAIL_RECEIVER_ERROR_PREFIX = "Email::Receiver::"
|
2016-02-10 15:00:27 -06:00
|
|
|
|
|
|
|
def error
|
2017-08-04 13:04:26 -05:00
|
|
|
@error_string.presence || I18n.t("emails.incoming.unrecognized_error")
|
2016-02-10 15:00:27 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_description
|
|
|
|
error_name = @error_string.sub(EMAIL_RECEIVER_ERROR_PREFIX, "").underscore
|
|
|
|
I18n.t("emails.incoming.errors.#{error_name}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_error_description?
|
2019-03-26 11:59:56 -05:00
|
|
|
@error_string && @error_string[EMAIL_RECEIVER_ERROR_PREFIX]
|
2016-02-10 15:00:27 -06:00
|
|
|
end
|
|
|
|
|
2016-03-30 12:05:42 -05:00
|
|
|
def headers
|
|
|
|
@mail.header.to_s
|
2016-02-10 15:00:27 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
2018-01-30 16:47:58 -06:00
|
|
|
@mail.subject.presence || I18n.t("emails.incoming.no_subject")
|
2016-02-10 15:00:27 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
body = @mail.text_part.decoded rescue nil
|
|
|
|
body ||= @mail.html_part.decoded rescue nil
|
|
|
|
body ||= @mail.body.decoded rescue nil
|
2018-01-30 16:47:58 -06:00
|
|
|
|
|
|
|
return I18n.t("emails.incoming.no_body") if body.blank?
|
|
|
|
|
|
|
|
body.encode("utf-8", invalid: :replace, undef: :replace, replace: "")
|
|
|
|
.strip
|
|
|
|
.truncate_words(100, escape: false)
|
2016-02-10 15:00:27 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|