chore: Minor API cleanups (#3645)
- exception list updated - revert mail presenter changes
This commit is contained in:
parent
c57c975a0d
commit
13d6734977
3 changed files with 25 additions and 77 deletions
|
@ -8,48 +8,30 @@ class MailPresenter < SimpleDelegator
|
|||
end
|
||||
|
||||
def subject
|
||||
encode_to_unicode(@mail.subject)
|
||||
encode_to_unicode(@mail.subject || '')
|
||||
end
|
||||
|
||||
def text_content
|
||||
@decoded_text_content = select_body || ''
|
||||
encoding = @decoded_text_content.encoding
|
||||
|
||||
body = EmailReplyTrimmer.trim(@decoded_text_content)
|
||||
@decoded_text_content ||= encode_to_unicode(text_part&.decoded || decoded_message || '')
|
||||
|
||||
return {} if @decoded_text_content.blank?
|
||||
|
||||
@text_content ||= {
|
||||
full: select_body,
|
||||
reply: @decoded_text_content,
|
||||
quoted: body.force_encoding(encoding).encode('UTF-8')
|
||||
full: @decoded_text_content,
|
||||
reply: extract_reply(@decoded_text_content)[:reply],
|
||||
quoted: extract_reply(@decoded_text_content)[:quoted_text]
|
||||
}
|
||||
end
|
||||
|
||||
def select_body
|
||||
message = mail.text_part || mail.html_part || mail
|
||||
decoded = encode_to_unicode(message.decoded)
|
||||
# Certain trigger phrases that means we didn't parse correctly
|
||||
return '' if %r{(Content-Type: multipart/alternative|text/plain)}.match?(decoded)
|
||||
|
||||
if (mail.content_type || '').include? 'text/html'
|
||||
::HtmlParser.parse_reply(decoded)
|
||||
else
|
||||
decoded
|
||||
end
|
||||
end
|
||||
|
||||
def html_content
|
||||
@decoded_html_content = select_body || ''
|
||||
@decoded_html_content ||= encode_to_unicode(html_part&.decoded)
|
||||
|
||||
return {} if @decoded_html_content.blank?
|
||||
|
||||
body = EmailReplyTrimmer.trim(@decoded_html_content)
|
||||
|
||||
@html_content ||= {
|
||||
full: select_body,
|
||||
reply: @decoded_html_content,
|
||||
quoted: body
|
||||
full: @decoded_html_content,
|
||||
reply: extract_reply(@decoded_html_content)[:reply],
|
||||
quoted: extract_reply(@decoded_html_content)[:quoted_text]
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -65,6 +47,14 @@ class MailPresenter < SimpleDelegator
|
|||
end
|
||||
end
|
||||
|
||||
def decoded_message
|
||||
if mail.multipart?
|
||||
return mail.text_part ? mail.text_part.decoded : nil
|
||||
end
|
||||
|
||||
mail.decoded
|
||||
end
|
||||
|
||||
def number_of_attachments
|
||||
mail.attachments.count
|
||||
end
|
||||
|
@ -124,27 +114,13 @@ class MailPresenter < SimpleDelegator
|
|||
return str if current_encoding == 'UTF-8'
|
||||
|
||||
str.encode(current_encoding, 'UTF-8', invalid: :replace, undef: :replace, replace: '?')
|
||||
rescue StandardError
|
||||
''
|
||||
end
|
||||
|
||||
def quoted_text_regexes
|
||||
return sender_agnostic_regexes if @account.nil? || @account.support_email.blank?
|
||||
|
||||
[
|
||||
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)
|
||||
] + sender_agnostic_regexes
|
||||
end
|
||||
|
||||
def sender_agnostic_regexes
|
||||
@sender_agnostic_regexes ||= [
|
||||
Regexp.new("^.*On.*(\n)?wrote:$", Regexp::IGNORECASE),
|
||||
Regexp.new('^.*On(.*)(.*)wrote:$', Regexp::IGNORECASE),
|
||||
Regexp.new("-+original\s+message-+\s*$", Regexp::IGNORECASE),
|
||||
Regexp.new("from:\s*$", Regexp::IGNORECASE)
|
||||
]
|
||||
def extract_reply(content)
|
||||
# NOTE: implement the reply parser over here
|
||||
{
|
||||
reply: content.strip,
|
||||
quoted_text: content.strip
|
||||
}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,6 +2,8 @@ module ExceptionList
|
|||
REST_CLIENT_EXCEPTIONS = [RestClient::NotFound, RestClient::GatewayTimeout, RestClient::BadRequest,
|
||||
RestClient::MethodNotAllowed, RestClient::Forbidden, RestClient::InternalServerError,
|
||||
RestClient::Exceptions::OpenTimeout, RestClient::Exceptions::ReadTimeout,
|
||||
RestClient::TemporaryRedirect, RestClient::SSLCertificateNotVerified, RestClient::PaymentRequired,
|
||||
RestClient::BadGateway, RestClient::Unauthorized, RestClient::PayloadTooLarge,
|
||||
RestClient::MovedPermanently, RestClient::ServiceUnavailable, Errno::ECONNREFUSED, SocketError].freeze
|
||||
SMTP_EXCEPTIONS = [
|
||||
Net::SMTPSyntaxError
|
||||
|
|
|
@ -96,35 +96,5 @@ RSpec.describe ReplyMailbox, type: :mailbox do
|
|||
expect(conversation_1.messages.last.content).to eq("Let's talk about these images:")
|
||||
end
|
||||
end
|
||||
|
||||
context 'with quotes in email' do
|
||||
let(:described_subject) { described_class.receive mail_with_quote }
|
||||
|
||||
before do
|
||||
# this UUID is hardcoded in the reply.eml, that's why we are updating this
|
||||
conversation.uuid = '6bdc3f4d-0bec-4515-a284-5d916fdde489'
|
||||
conversation.save
|
||||
end
|
||||
|
||||
it 'add the mail content as new message on the conversation' do
|
||||
described_subject
|
||||
expect(conversation.messages.last.content).to eq(
|
||||
<<-BODY.strip_heredoc.chomp
|
||||
Yes, I am providing you step how to reproduce this issue
|
||||
|
||||
On Thu, Aug 19, 2021 at 2:07 PM Tejaswini from Email sender test < tejaswini@chatwoot.com> wrote:
|
||||
|
||||
> Any update on this?
|
||||
>
|
||||
>
|
||||
|
||||
--
|
||||
* Sony Mathew*
|
||||
Software developer
|
||||
*Mob:9999999999
|
||||
BODY
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue