FIX: associates email replies using both 'In-Reply-To' and 'References' headers

This commit is contained in:
Régis Hanol 2016-01-20 22:52:08 +01:00
parent f5c0972210
commit f145310cd5
6 changed files with 77 additions and 4 deletions

View file

@ -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

View file

@ -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

12
spec/fixtures/emails/email_reply_1.eml vendored Normal file
View file

@ -0,0 +1,12 @@
Return-Path: <one@foo.com>
From: One <one@foo.com>
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**.

13
spec/fixtures/emails/email_reply_2.eml vendored Normal file
View file

@ -0,0 +1,13 @@
Return-Path: <two@foo.com>
From: Two <two@foo.com>
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**.

14
spec/fixtures/emails/email_reply_3.eml vendored Normal file
View file

@ -0,0 +1,14 @@
Return-Path: <three@foo.com>
From: Three <three@foo.com>
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**.

15
spec/fixtures/emails/email_reply_4.eml vendored Normal file
View file

@ -0,0 +1,15 @@
Return-Path: <one@foo.com>
From: One <one@foo.com>
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**.