Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: automatically elides gmail quotes

Régis Hanolfd3309064638
diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb
index bb5576a5ad8..b29baf9a555 100644
--- a/lib/email/receiver.rb
+++ b/lib/email/receiver.rb
@@ -261,9 +261,16 @@ module Email
end
markdown, elided_markdown = if html.present?
- markdown = HtmlToMarkdown.new(html, keep_img_tags: true, keep_cid_imgs: true).to_markdown
- markdown = trim_discourse_markers(markdown)
- trim_reply_and_extract_elided(markdown)
+ if html[%{<div class="gmail_quote">}]
+ html, elided_html = extract_gmail_quote(html)
+ markdown = HtmlToMarkdown.new(html, keep_img_tags: true, keep_cid_imgs: true).to_markdown
+ elided_markdown = HtmlToMarkdown.new(elided_html).to_markdown
+ [markdown, elided_markdown]
+ else
+ markdown = HtmlToMarkdown.new(html, keep_img_tags: true, keep_cid_imgs: true).to_markdown
+ markdown = trim_discourse_markers(markdown)
+ trim_reply_and_extract_elided(markdown)
+ end
end
if text.blank? || (SiteSetting.incoming_email_prefer_html && markdown.present?)
@@ -273,6 +280,12 @@ module Email
end
end
+ def extract_gmail_quote(html)
+ doc = Nokogiri::HTML.fragment(html)
+ elided = doc.css("div.gmail_quote")[0].remove
+ [doc.to_html, elided.to_html]
+ end
+
def trim_reply_and_extract_elided(text)
return [text, ""] if @opts[:skip_trimming]
EmailReplyTrimmer.trim(text, true)
diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb
index 838e3bb9b4e..4c3a562f928 100644
--- a/spec/components/email/receiver_spec.rb
+++ b/spec/components/email/receiver_spec.rb
@@ -191,6 +191,12 @@ describe Email::Receiver do
expect(topic.posts.last.raw).to eq("This is a **HTML** reply ;)")
end
+ it "automatically elides gmail quotes" do
+ SiteSetting.always_show_trimmed_content = true
+ expect { process(:gmail_html_reply) }.to change { topic.posts.count }
+ expect(topic.posts.last.raw).to eq("This is a **GMAIL** reply ;)\n\n<details class='elided'>\n<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>\n\nThis is the *elided* part!\n\n</details>")
+ end
+
it "doesn't process email with same message-id more than once" do
expect do
process(:text_reply)
diff --git a/spec/fixtures/emails/gmail_html_reply.eml b/spec/fixtures/emails/gmail_html_reply.eml
new file mode 100644
index 00000000000..6a69a3f35b1
--- /dev/null
+++ b/spec/fixtures/emails/gmail_html_reply.eml
@@ -0,0 +1,17 @@
+Return-Path: <[email protected]>
+From: Foo Bar <[email protected]>
+Date: Fri, 15 Jan 2016 00:12:43 +0100
+Message-ID: <[email protected]>
+Mime-Version: 1.0
+Content-Type: text/html; charset=UTF-8
+Content-Transfer-Encoding: quoted-printable
+
+<div>
+ <div dir="auto">
+ <p>This is a <b>GMAIL</b> reply ;)</p>
+ </div>
+ <div class="gmail_quote">
+ <p>This is the <i>elided</i> part!</p>
+ </div>
+</div>