FIX: replace 'discourse_email_parser' with 'email_reply_trimmer' to better trim replies from plain text emails

FIX: undefined method `number_to_human_size' when email contains attachments
This commit is contained in:
Régis Hanol 2016-01-30 01:29:31 +01:00
parent 1d27b33100
commit 46b6c55197
8 changed files with 33 additions and 27 deletions

View File

@ -64,7 +64,7 @@ gem 'aws-sdk', require: false
gem 'excon', require: false
gem 'unf', require: false
gem 'discourse_email_parser'
gem 'email_reply_trimmer', '0.0.3'
# note: for image_optim to correctly work you need to follow
# https://github.com/toy/image_optim

View File

@ -73,10 +73,10 @@ GEM
diff-lcs (1.2.5)
discourse-qunit-rails (0.0.8)
railties
discourse_email_parser (0.6.1)
docile (1.1.5)
domain_name (0.5.25)
unf (>= 0.0.5, < 1.0.0)
email_reply_trimmer (0.0.3)
ember-data-source (1.0.0.beta.16.1)
ember-source (~> 1.8)
ember-handlebars-template (0.1.5)
@ -411,7 +411,7 @@ DEPENDENCIES
byebug
certified
discourse-qunit-rails
discourse_email_parser
email_reply_trimmer (= 0.0.3)
ember-rails
ember-source (= 1.12.2)
excon

View File

@ -5,6 +5,7 @@ require_dependency "email/html_cleaner"
module Email
class Receiver
include ActionView::Helpers::NumberHelper
class ProcessingError < StandardError; end
class EmptyEmailError < ProcessingError; end
@ -110,29 +111,23 @@ module Email
end
# prefer text over html
if text.present?
text_encoding = text.encoding
text = DiscourseEmailParser.parse_reply(text)
text = try_to_encode(text, text_encoding)
return text if text.present?
end
text = trim_discourse_markers(text) if text.present?
text = EmailReplyTrimmer.trim(text) if text.present?
return text if text.present?
# clean the html if that's all we've got
if html.present?
html_encoding = html.encoding
html = Email::HtmlCleaner.new(html).output_html
html = DiscourseEmailParser.parse_reply(html)
html = try_to_encode(html, html_encoding)
return html if html.present?
end
html = Email::HtmlCleaner.new(html).output_html if html.present?
html = trim_discourse_markers(html) if html.present?
html = EmailReplyTrimmer.trim(html) if html.present?
return html if html.present?
end
def fix_charset(mail_part)
return nil if mail_part.blank? || mail_part.body.blank?
string = mail_part.body.to_s
string = mail_part.body.decoded rescue nil
# TODO (use charlock_holmes to properly detect encoding)
return nil if string.blank?
# 1) use the charset provided
if mail_part.charset.present?
@ -150,6 +145,14 @@ module Email
nil
end
def previous_replies_regex
@previous_replies_regex ||= /^---\n\*#{I18n.t("user_notifications.previous_discussion")}\*\n/im
end
def trim_discourse_markers(reply)
reply.split(previous_replies_regex)[0]
end
def from
@from ||= @mail[:from].address_list.addresses.first
end

View File

@ -172,20 +172,17 @@ describe Email::Receiver do
it "handles inline reply" do
expect { process(:inline_reply) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to eq("On Tue, Jan 15, 2016 at 11:12 AM, Bar Foo <info@unconfigured.discourse.org> wrote:\n\n> WAT <https://bar.com/users/wat> November 28\n>\n> This is the previous post.\n\nAnd this is *my* reply :+1:")
expect(topic.posts.last.raw).to eq("> WAT <https://bar.com/users/wat> November 28\n>\n> This is the previous post.\n\nAnd this is *my* reply :+1:")
end
it "retrieves the first part of multiple replies" do
expect { process(:inline_mixed_replies) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to eq("On Tue, Jan 15, 2016 at 11:12 AM, Bar Foo <info@unconfigured.discourse.org> wrote:\n\n> WAT <https://bar.com/users/wat> November 28\n>\n> This is the previous post.\n\nAnd this is *my* reply :+1:\n\n> This is another post.\n\nAnd this is **another** reply.")
expect(topic.posts.last.raw).to eq("> WAT <https://bar.com/users/wat> November 28\n>\n> This is the previous post.\n\nAnd this is *my* reply :+1:\n\n> This is another post.\n\nAnd this is **another** reply.")
end
it "strips signatures" do
it "strips mobile/webmail signatures" do
expect { process(:iphone_signature) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to eq("This is not the signature you're looking for.")
expect { process(:signature) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to eq("You shall not sign!")
end
it "strips 'original message' context" do
@ -193,14 +190,20 @@ describe Email::Receiver do
expect(topic.posts.last.raw).to eq("This is a reply :)")
end
it "supports attachments" do
expect { process(:no_body_with_attachments) }.to change { topic.posts.count }
it "supports attached images" do
expect { process(:no_body_with_image) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to match(/<img/)
expect { process(:inline_attachment) }.to change { topic.posts.count }
expect { process(:inline_image) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to match(/Before\s+<img.+\s+After/m)
end
it "supports attachments" do
SiteSetting.authorized_extensions = "txt"
expect { process(:attached_txt_file) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to match(/text\.txt/)
end
it "supports liking via email" do
expect { process(:like) }.to change(PostAction, :count)
end

Binary file not shown.

Binary file not shown.