2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-11 05:00:27 +08:00
|
|
|
class IncomingEmailDetailsSerializer < ApplicationSerializer
|
|
|
|
|
|
|
|
attributes :error,
|
|
|
|
:error_description,
|
2016-03-07 23:56:17 +08:00
|
|
|
:rejection_message,
|
2016-03-31 01:05:42 +08:00
|
|
|
:headers,
|
2016-02-11 05:00:27 +08:00
|
|
|
:subject,
|
|
|
|
:body
|
|
|
|
|
|
|
|
def initialize(incoming_email, opts)
|
|
|
|
super
|
|
|
|
@error_string = incoming_email.error
|
|
|
|
@mail = Mail.new(incoming_email.raw)
|
|
|
|
end
|
|
|
|
|
2020-04-30 14:48:34 +08:00
|
|
|
EMAIL_RECEIVER_ERROR_PREFIX = "Email::Receiver::"
|
2016-02-11 05:00:27 +08:00
|
|
|
|
|
|
|
def error
|
2017-08-05 02:04:26 +08:00
|
|
|
@error_string.presence || I18n.t("emails.incoming.unrecognized_error")
|
2016-02-11 05:00:27 +08: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-27 00:59:56 +08:00
|
|
|
@error_string && @error_string[EMAIL_RECEIVER_ERROR_PREFIX]
|
2016-02-11 05:00:27 +08:00
|
|
|
end
|
|
|
|
|
2016-03-31 01:05:42 +08:00
|
|
|
def headers
|
|
|
|
@mail.header.to_s
|
2016-02-11 05:00:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
2018-01-31 06:47:58 +08:00
|
|
|
@mail.subject.presence || I18n.t("emails.incoming.no_subject")
|
2016-02-11 05:00:27 +08: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-31 06:47:58 +08: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-11 05:00:27 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|