FEATURE: hide elided part of incoming emails behind a [details] tag

This commit is contained in:
Régis Hanol 2016-03-09 18:51:54 +01:00
parent 1009dc9be1
commit 2747e14b4c
3 changed files with 13 additions and 8 deletions

View file

@ -54,7 +54,11 @@ module Email
def process_internal def process_internal
user = find_or_create_user(@from_email, @from_display_name) user = find_or_create_user(@from_email, @from_display_name)
@incoming_email.update_columns(user_id: user.id) @incoming_email.update_columns(user_id: user.id)
body = select_body || ""
body, elided = select_body
body ||= ""
body << "\n\n[details=...]\n#{elided}\n[/details]" if elided.present?
raise AutoGeneratedEmailError if is_auto_generated? raise AutoGeneratedEmailError if is_auto_generated?
raise NoBodyDetectedError if body.blank? && !@mail.has_attachments? raise NoBodyDetectedError if body.blank? && !@mail.has_attachments?
@ -115,6 +119,7 @@ module Email
def select_body def select_body
text = nil text = nil
html = nil html = nil
elided = nil
if @mail.multipart? if @mail.multipart?
text = fix_charset(@mail.text_part) text = fix_charset(@mail.text_part)
@ -127,14 +132,14 @@ module Email
# prefer text over html # prefer text over html
text = trim_discourse_markers(text) if text.present? text = trim_discourse_markers(text) if text.present?
text = EmailReplyTrimmer.trim(text) if text.present? text, elided = EmailReplyTrimmer.trim(text, true) if text.present?
return text if text.present? return [text, elided] if text.present?
# clean the html if that's all we've got # clean the html if that's all we've got
html = Email::HtmlCleaner.new(html).output_html if html.present? html = Email::HtmlCleaner.new(html).output_html if html.present?
html = trim_discourse_markers(html) if html.present? html = trim_discourse_markers(html) if html.present?
html = EmailReplyTrimmer.trim(html) if html.present? html, elided = EmailReplyTrimmer.trim(html, true) if html.present?
return html if html.present? return [html, elided] if html.present?
end end
def fix_charset(mail_part) def fix_charset(mail_part)

View file

@ -118,7 +118,7 @@ describe Email::Receiver do
it "removes the 'on <date>, <contact> wrote' quoting line" do it "removes the 'on <date>, <contact> wrote' quoting line" do
expect { process(:on_date_contact_wrote) }.to change { topic.posts.count } expect { process(:on_date_contact_wrote) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to eq("This is the actual reply.") expect(topic.posts.last.raw).to eq("This is the actual reply.\n\n[details=...]\nOn Tue, Jan 14, 2016 at 0:42 AM, Bar Foo <wat@discourse.org> wrote:\n\n> This is the previous email.\n> And it had\n>\n> a lot\n>\n>\n> of lines ;)\n[/details]")
end end
it "removes the 'Previous Replies' marker" do it "removes the 'Previous Replies' marker" do
@ -193,7 +193,7 @@ describe Email::Receiver do
it "strips 'original message' context" do it "strips 'original message' context" do
expect { process(:original_message) }.to change { topic.posts.count } expect { process(:original_message) }.to change { topic.posts.count }
expect(topic.posts.last.raw).to eq("This is a reply :)") expect(topic.posts.last.raw).to eq("This is a reply :)\n\n[details=...]\n---Original Message---\nThis part should not be included\n[/details]")
end end
it "supports attached images" do it "supports attached images" do

View file

@ -258,7 +258,7 @@ describe PostAlerter do
post.revise(admin, { raw: "Mention @eviltrout in this edit." }) post.revise(admin, { raw: "Mention @eviltrout in this edit." })
}.to change(evil_trout.notifications, :count) }.to change(evil_trout.notifications, :count)
n = evil_trout.notifications.last n = evil_trout.notifications.last
n.data_hash["original_username"].should == admin.username expect(n.data_hash["original_username"]).to eq(admin.username)
end end
end end