2020-04-30 14:50:26 +00:00
|
|
|
class MailPresenter < SimpleDelegator
|
|
|
|
attr_accessor :mail
|
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
def initialize(mail, account = nil)
|
2020-04-30 14:50:26 +00:00
|
|
|
super(mail)
|
|
|
|
@mail = mail
|
2020-05-22 12:37:06 +00:00
|
|
|
@account = account
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
2021-04-27 17:51:04 +00:00
|
|
|
encode_to_unicode(@mail.subject || '')
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
def text_content
|
2021-07-15 17:20:32 +00:00
|
|
|
@decoded_text_content ||= encode_to_unicode(text_part&.decoded || decoded_message || '')
|
2021-07-15 14:36:43 +00:00
|
|
|
|
|
|
|
return {} if @decoded_text_content.blank?
|
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
@text_content ||= {
|
|
|
|
full: @decoded_text_content,
|
|
|
|
reply: extract_reply(@decoded_text_content)[:reply],
|
|
|
|
quoted: extract_reply(@decoded_text_content)[:quoted_text]
|
|
|
|
}
|
|
|
|
end
|
2020-04-30 14:50:26 +00:00
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
def html_content
|
2021-07-15 14:36:43 +00:00
|
|
|
@decoded_html_content ||= encode_to_unicode(html_part&.decoded)
|
|
|
|
|
|
|
|
return {} if @decoded_html_content.blank?
|
|
|
|
|
2020-05-22 12:37:06 +00:00
|
|
|
@html_content ||= {
|
|
|
|
full: @decoded_html_content,
|
|
|
|
reply: extract_reply(@decoded_html_content)[:reply],
|
|
|
|
quoted: extract_reply(@decoded_html_content)[:quoted_text]
|
|
|
|
}
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def attachments
|
|
|
|
# ref : https://github.com/gorails-screencasts/action-mailbox-action-text/blob/master/app/mailboxes/posts_mailbox.rb
|
|
|
|
mail.attachments.map do |attachment|
|
2021-08-03 14:41:52 +00:00
|
|
|
blob = ActiveStorage::Blob.create_and_upload!(
|
2020-04-30 14:50:26 +00:00
|
|
|
io: StringIO.new(attachment.body.to_s),
|
|
|
|
filename: attachment.filename,
|
|
|
|
content_type: attachment.content_type
|
|
|
|
)
|
|
|
|
{ original: attachment, blob: blob }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-15 17:20:32 +00:00
|
|
|
def decoded_message
|
|
|
|
if mail.multipart?
|
|
|
|
return mail.text_part ? mail.text_part.decoded : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
mail.decoded
|
|
|
|
end
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
def number_of_attachments
|
|
|
|
mail.attachments.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialized_data
|
|
|
|
{
|
2021-08-09 12:34:34 +00:00
|
|
|
bcc: bcc,
|
|
|
|
cc: cc,
|
|
|
|
content_type: content_type,
|
2020-04-30 14:50:26 +00:00
|
|
|
date: date,
|
|
|
|
from: from,
|
2021-08-09 12:34:34 +00:00
|
|
|
html_content: html_content,
|
2020-04-30 14:50:26 +00:00
|
|
|
in_reply_to: in_reply_to,
|
2021-08-09 12:34:34 +00:00
|
|
|
message_id: message_id,
|
|
|
|
multipart: multipart?,
|
|
|
|
number_of_attachments: number_of_attachments,
|
|
|
|
subject: subject,
|
|
|
|
text_content: text_content,
|
|
|
|
to: to
|
2020-04-30 14:50:26 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-08-11 19:58:07 +00:00
|
|
|
def from
|
|
|
|
# changing to downcase to avoid case mismatch while finding contact
|
|
|
|
@mail.from.map(&:downcase)
|
|
|
|
end
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
# forcing the encoding of the content to UTF-8 so as to be compatible with database and serializers
|
|
|
|
def encode_to_unicode(str)
|
2021-07-15 14:36:43 +00:00
|
|
|
return '' if str.blank?
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
current_encoding = str.encoding.name
|
2020-09-22 05:56:41 +00:00
|
|
|
return str if current_encoding == 'UTF-8'
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
str.encode(current_encoding, 'UTF-8', invalid: :replace, undef: :replace, replace: '?')
|
|
|
|
end
|
2020-05-22 12:37:06 +00:00
|
|
|
|
|
|
|
def extract_reply(content)
|
|
|
|
@regex_arr ||= quoted_text_regexes
|
|
|
|
|
|
|
|
content_length = content.length
|
|
|
|
# calculates the matching regex closest to top of page
|
|
|
|
index = @regex_arr.inject(content_length) do |min, regex|
|
|
|
|
[(content.index(regex) || content_length), min].min
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
reply: content[0..(index - 1)].strip,
|
2020-06-18 09:47:45 +00:00
|
|
|
quoted_text: content[index..].strip
|
2020-05-22 12:37:06 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def quoted_text_regexes
|
2021-03-04 08:29:59 +00:00
|
|
|
return sender_agnostic_regexes if @account.nil? || @account.support_email.blank?
|
2020-07-21 06:50:46 +00:00
|
|
|
|
|
|
|
[
|
2021-03-04 08:29:59 +00:00
|
|
|
Regexp.new("From:\s* #{Regexp.escape(@account.support_email)}", Regexp::IGNORECASE),
|
|
|
|
Regexp.new("<#{Regexp.escape(@account.support_email)}>", Regexp::IGNORECASE),
|
|
|
|
Regexp.new("#{Regexp.escape(@account.support_email)}\s+wrote:", Regexp::IGNORECASE),
|
|
|
|
Regexp.new("On(.*)#{Regexp.escape(@account.support_email)}(.*)wrote:", Regexp::IGNORECASE)
|
2020-07-21 06:50:46 +00:00
|
|
|
] + sender_agnostic_regexes
|
|
|
|
end
|
|
|
|
|
|
|
|
def sender_agnostic_regexes
|
|
|
|
@sender_agnostic_regexes ||= [
|
2020-05-22 12:37:06 +00:00
|
|
|
Regexp.new("^.*On.*(\n)?wrote:$", Regexp::IGNORECASE),
|
2020-07-21 06:50:46 +00:00
|
|
|
Regexp.new('^.*On(.*)(.*)wrote:$', Regexp::IGNORECASE),
|
2020-05-22 12:37:06 +00:00
|
|
|
Regexp.new("-+original\s+message-+\s*$", Regexp::IGNORECASE),
|
|
|
|
Regexp.new("from:\s*$", Regexp::IGNORECASE)
|
|
|
|
]
|
2020-07-21 06:50:46 +00:00
|
|
|
end
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|