fix: Use mail.decoded as fallback for text content to fix char-rendering

This commit is contained in:
Pranav Raj S 2021-07-15 20:06:43 +05:30
parent e0535168a0
commit 72069e45ef

View file

@ -12,7 +12,10 @@ class MailPresenter < SimpleDelegator
end end
def text_content def text_content
@decoded_text_content ||= encode_to_unicode(text_part&.decoded || fallback_content) @decoded_text_content ||= encode_to_unicode(text_part&.decoded || mail.decoded || '')
return {} if @decoded_text_content.blank?
@text_content ||= { @text_content ||= {
full: @decoded_text_content, full: @decoded_text_content,
reply: extract_reply(@decoded_text_content)[:reply], reply: extract_reply(@decoded_text_content)[:reply],
@ -21,7 +24,10 @@ class MailPresenter < SimpleDelegator
end end
def html_content def html_content
@decoded_html_content ||= encode_to_unicode(html_part&.decoded || fallback_content) @decoded_html_content ||= encode_to_unicode(html_part&.decoded)
return {} if @decoded_html_content.blank?
@html_content ||= { @html_content ||= {
full: @decoded_html_content, full: @decoded_html_content,
reply: extract_reply(@decoded_html_content)[:reply], reply: extract_reply(@decoded_html_content)[:reply],
@ -29,10 +35,6 @@ class MailPresenter < SimpleDelegator
} }
end end
def fallback_content
body&.decoded || ''
end
def attachments def attachments
# ref : https://github.com/gorails-screencasts/action-mailbox-action-text/blob/master/app/mailboxes/posts_mailbox.rb # ref : https://github.com/gorails-screencasts/action-mailbox-action-text/blob/master/app/mailboxes/posts_mailbox.rb
mail.attachments.map do |attachment| mail.attachments.map do |attachment|
@ -69,6 +71,8 @@ class MailPresenter < SimpleDelegator
# forcing the encoding of the content to UTF-8 so as to be compatible with database and serializers # forcing the encoding of the content to UTF-8 so as to be compatible with database and serializers
def encode_to_unicode(str) def encode_to_unicode(str)
return '' if str.blank?
current_encoding = str.encoding.name current_encoding = str.encoding.name
return str if current_encoding == 'UTF-8' return str if current_encoding == 'UTF-8'