FIX: 'In-Reply-To' header should default to topic_message_id

This commit is contained in:
Régis Hanol 2016-11-28 14:18:02 +01:00
parent 6768c7fc0b
commit a03287f2ee
2 changed files with 71 additions and 3 deletions

View File

@ -100,8 +100,8 @@ module Email
@message.header['Message-ID'] = incoming_message_id || post_message_id
if post && post.post_number > 1
@message.header['In-Reply-To'] = referenced_post_message_ids.first
@message.header['References'] = [topic_message_id, referenced_post_message_ids].flatten
@message.header['In-Reply-To'] = referenced_post_message_ids.first || topic_message_id
@message.header['References'] = [topic_message_id, referenced_post_message_ids].flatten.compact.uniq
end
# http://www.ietf.org/rfc/rfc2919.txt

View File

@ -139,6 +139,75 @@ describe Email::Sender do
Then { expect(message.header['X-Discourse-Reply-Key']).not_to be_present }
end
context "email threading" do
let(:topic) { Fabricate(:topic) }
let(:post_1) { Fabricate(:post, topic: topic, post_number: 1) }
let(:post_2) { Fabricate(:post, topic: topic, post_number: 2) }
let(:post_3) { Fabricate(:post, topic: topic, post_number: 3) }
let(:post_4) { Fabricate(:post, topic: topic, post_number: 4) }
let!(:incoming_email) { IncomingEmail.create(topic: topic, post: post_4, message_id: "foobar") }
let!(:post_reply_1_3) { PostReply.create(post: post_1, reply: post_3) }
let!(:post_reply_2_3) { PostReply.create(post: post_2, reply: post_3) }
before do
message.header['X-Discourse-Topic-Id'] = topic.id
end
it "doesn't set the 'In-Reply-To' and 'References' headers on the first post" do
message.header['X-Discourse-Post-Id'] = post_1.id
email_sender.send
expect(message.header['Message-Id'].to_s).to eq("<topic/#{topic.id}/#{post_1.id}@test.localhost>")
expect(message.header['In-Reply-To'].to_s).to be_blank
expect(message.header['References'].to_s).to be_blank
end
it "sets the 'In-Reply-To' header to the topic by default" do
message.header['X-Discourse-Post-Id'] = post_2.id
email_sender.send
expect(message.header['Message-Id'].to_s).to eq("<topic/#{topic.id}/#{post_2.id}@test.localhost>")
expect(message.header['In-Reply-To'].to_s).to eq("<topic/#{topic.id}@test.localhost>")
end
it "sets the 'In-Reply-To' header to the newest replied post" do
message.header['X-Discourse-Post-Id'] = post_3.id
email_sender.send
expect(message.header['Message-Id'].to_s).to eq("<topic/#{topic.id}/#{post_3.id}@test.localhost>")
expect(message.header['In-Reply-To'].to_s).to eq("<topic/#{topic.id}/#{post_2.id}@test.localhost>")
end
it "sets the 'References' header to the topic and all replied posts" do
message.header['X-Discourse-Post-Id'] = post_3.id
email_sender.send
references = [
"<topic/#{topic.id}@test.localhost>",
"<topic/#{topic.id}/#{post_2.id}@test.localhost>",
"<topic/#{topic.id}/#{post_1.id}@test.localhost>",
]
expect(message.header['References'].to_s).to eq(references.join(" "))
end
it "uses the incoming_email message_id when available" do
message.header['X-Discourse-Post-Id'] = post_4.id
email_sender.send
expect(message.header['Message-Id'].to_s).to eq("<#{incoming_email.message_id}>")
end
end
context "merges custom mandrill header" do
before do
ActionMailer::Base.smtp_settings[:address] = "smtp.mandrillapp.com"
@ -221,7 +290,6 @@ describe Email::Sender do
expect(@email_log.user_id).to eq(user.id)
end
end
end