diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 736dcb1730d..52bc4b6ce36 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -211,10 +211,9 @@ module Email message_ids.uniq! return if message_ids.empty? - IncomingEmail.where.not(post_id: nil) - .where(message_id: message_ids) - .first - .try(:post) + Post.where(id: IncomingEmail.where(message_id: message_ids).select(:post_id)) + .order(created_at: :desc) + .first end def extract_references diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index c1d1b0ec4de..665a46b63d7 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -236,6 +236,26 @@ describe Email::Receiver do expect(emails).to include("someone@else.com", "discourse@bar.com", "team@bar.com", "wat@bar.com") end + it "associates email replies using both 'In-Reply-To' and 'References' headers" do + expect { process(:email_reply_1) }.to change(Topic, :count) + + topic = Topic.last + + expect { process(:email_reply_2) }.to change { topic.posts.count } + expect { process(:email_reply_3) }.to change { topic.posts.count } + + # Why 6 when we only processed 3 emails? + # - 3 of them are indeed "regular" posts generated from the emails + # - The 3 others are "small action" posts automatically added because + # we invited 3 users (team@bar.com, two@foo.com and three@foo.com) + expect(topic.posts.count).to eq(6) + + # trash all but the 1st post + topic.ordered_posts[1..-1].each(&:trash!) + + expect { process(:email_reply_4) }.to change { topic.posts.count } + end + end context "new topic in a category" do diff --git a/spec/fixtures/emails/email_reply_1.eml b/spec/fixtures/emails/email_reply_1.eml new file mode 100644 index 00000000000..fdf4a5f2f89 --- /dev/null +++ b/spec/fixtures/emails/email_reply_1.eml @@ -0,0 +1,12 @@ +Return-Path: +From: One +To: team@bar.com +Cc: two@foo.com, three@foo.com +Subject: Testing email threading +Date: Fri, 15 Jan 2016 00:12:43 +0100 +Message-ID: <34@foo.bar.mail> +Mime-Version: 1.0 +Content-Type: text/plain +Content-Transfer-Encoding: 7bit + +This is email reply **1**. diff --git a/spec/fixtures/emails/email_reply_2.eml b/spec/fixtures/emails/email_reply_2.eml new file mode 100644 index 00000000000..fde2e03390d --- /dev/null +++ b/spec/fixtures/emails/email_reply_2.eml @@ -0,0 +1,13 @@ +Return-Path: +From: Two +To: one@foo.com +Cc: team@bar.com, three@foo.com +Subject: RE: Testing email threading +Date: Fri, 15 Jan 2016 00:12:43 +0100 +Message-ID: <35@foo.bar.mail> +In-Reply-To: <34@foo.bar.mail> +Mime-Version: 1.0 +Content-Type: text/plain +Content-Transfer-Encoding: 7bit + +This is email reply **2**. diff --git a/spec/fixtures/emails/email_reply_3.eml b/spec/fixtures/emails/email_reply_3.eml new file mode 100644 index 00000000000..3b4eb6d0c05 --- /dev/null +++ b/spec/fixtures/emails/email_reply_3.eml @@ -0,0 +1,14 @@ +Return-Path: +From: Three +To: one@foo.com +Cc: team@bar.com, two@foo.com +Subject: RE: Testing email threading +Date: Fri, 15 Jan 2016 00:12:43 +0100 +Message-ID: <36@foo.bar.mail> +In-Reply-To: <35@foo.bar.mail> +References: <34@foo.bar.mail> +Mime-Version: 1.0 +Content-Type: text/plain +Content-Transfer-Encoding: 7bit + +This is email reply **3**. diff --git a/spec/fixtures/emails/email_reply_4.eml b/spec/fixtures/emails/email_reply_4.eml new file mode 100644 index 00000000000..4c54d008497 --- /dev/null +++ b/spec/fixtures/emails/email_reply_4.eml @@ -0,0 +1,15 @@ +Return-Path: +From: One +To: two@foo.com +Cc: team@bar.com, three@foo.com +Subject: RE: Testing email threading +Date: Fri, 15 Jan 2016 00:12:43 +0100 +Message-ID: <37@foo.bar.mail> +In-Reply-To: <36@foo.bar.mail> +References: <34@foo.bar.mail> + <35@foo.bar.mail> +Mime-Version: 1.0 +Content-Type: text/plain +Content-Transfer-Encoding: 7bit + +This is email reply **4**.