mirror of
https://github.com/discourse/discourse.git
synced 2025-03-24 16:25:32 +08:00
FIX: respect RFCs when setting 'In-Reply-To' and 'References' email headers
This commit is contained in:
parent
af4f2d000a
commit
74b6fe8739
@ -72,25 +72,37 @@ module Email
|
|||||||
reply_key = header_value('X-Discourse-Reply-Key')
|
reply_key = header_value('X-Discourse-Reply-Key')
|
||||||
|
|
||||||
# always set a default Message ID from the host
|
# always set a default Message ID from the host
|
||||||
uuid = SecureRandom.uuid
|
@message.header['Message-ID'] = "<#{SecureRandom.uuid}@#{host}>"
|
||||||
@message.header['Message-ID'] = "<#{uuid}@#{host}>"
|
|
||||||
|
|
||||||
if topic_id.present?
|
if topic_id.present?
|
||||||
email_log.topic_id = topic_id
|
email_log.topic_id = topic_id
|
||||||
|
|
||||||
|
post = Post.find_by(id: post_id)
|
||||||
|
topic = Topic.find_by(id: topic_id)
|
||||||
|
|
||||||
|
topic_message_id = "<topic/#{topic_id}@#{host}>"
|
||||||
|
post_message_id = "<topic/#{topic_id}/#{post_id}@#{host}>"
|
||||||
|
|
||||||
incoming_email = IncomingEmail.find_by(post_id: post_id, topic_id: topic_id)
|
incoming_email = IncomingEmail.find_by(post_id: post_id, topic_id: topic_id)
|
||||||
|
incoming_message_id = "<#{incoming_email.message_id}>" if incoming_email&.message_id.present?
|
||||||
|
|
||||||
incoming_message_id = nil
|
referenced_posts = Post.includes(:incoming_email)
|
||||||
incoming_message_id = "<#{incoming_email.message_id}>" if incoming_email.try(:message_id).present?
|
.where(id: PostReply.where(reply_id: post_id).select(:post_id))
|
||||||
|
.order(id: :desc)
|
||||||
|
|
||||||
topic_identifier = "<topic/#{topic_id}@#{host}>"
|
referenced_post_message_ids = referenced_posts.map do |post|
|
||||||
post_identifier = "<topic/#{topic_id}/#{post_id}@#{host}>"
|
if post&.incoming_email&.message_id.present?
|
||||||
|
"<#{post.incoming_email.message_id}>"
|
||||||
|
else
|
||||||
|
"<topic/#{topic_id}/#{post.id}@#{host}>"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@message.header['Message-ID'] = post_identifier
|
@message.header['Message-ID'] = incoming_message_id || post_message_id
|
||||||
@message.header['In-Reply-To'] = incoming_message_id || topic_identifier
|
if post && post.post_number > 1
|
||||||
@message.header['References'] = topic_identifier
|
@message.header['In-Reply-To'] = referenced_post_message_ids.first
|
||||||
|
@message.header['References'] = [topic_message_id, referenced_post_message_ids].flatten
|
||||||
topic = Topic.where(id: topic_id).first
|
end
|
||||||
|
|
||||||
# http://www.ietf.org/rfc/rfc2919.txt
|
# http://www.ietf.org/rfc/rfc2919.txt
|
||||||
if topic && topic.category && !topic.category.uncategorized?
|
if topic && topic.category && !topic.category.uncategorized?
|
||||||
|
@ -180,9 +180,6 @@ describe Email::Sender do
|
|||||||
When { email_sender.send }
|
When { email_sender.send }
|
||||||
Then { expect(email_log.post_id).to eq(3344) }
|
Then { expect(email_log.post_id).to eq(3344) }
|
||||||
Then { expect(email_log.topic_id).to eq(5577) }
|
Then { expect(email_log.topic_id).to eq(5577) }
|
||||||
Then { expect(message.header['In-Reply-To']).to be_present }
|
|
||||||
Then { expect(message.header['References']).to be_present }
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "email log with a reply key" do
|
context "email log with a reply key" do
|
||||||
|
Loading…
x
Reference in New Issue
Block a user