FIX: Canonical Message-ID was incorrect for some cases (#15701)

When creating a direct message to a group with group SMTP
set up, and adding another person to that message in the OP,
we send an email to the second person in the OP via the group_smtp
job. This in turn creates an IncomingEmail record to guard against
IMAP double sync.

The issue with this was that this IncomingEmail (which is essentialy
a placeholder/dummy one) was having its Message-ID used as the canonical
References Message-ID for subsequent emails sent out to user_private_message
recipients (such as members of the group), causing threading issues in
the mail client. The canonical <topic/ID@HOST> format should be used
instead for these cases.

This commit fixes the issue by only using the IncomingEmail for the
OP's Message-ID if the OP was created via our handle_mail email receiver
pipeline. It does not make sense to use it in other cases.
This commit is contained in:
Martin Brennan
2022-02-03 10:36:32 +10:00
committed by GitHub
parent febe997bee
commit 82cb67e67b
3 changed files with 36 additions and 6 deletions

View File

@ -39,8 +39,15 @@ module Email
def generate_for_topic(topic, use_incoming_email_if_present: false, canonical: false)
first_post = topic.ordered_posts.first
incoming_email = first_post.incoming_email
if use_incoming_email_if_present && first_post.incoming_email&.message_id.present?
# If the incoming email was created by handle_mail, then it was an
# inbound email sent to Discourse and handled by Email::Receiver,
# this is the only case where we want to use the original Message-ID
# because we want to maintain threading in the original mail client.
if use_incoming_email_if_present &&
incoming_email&.message_id.present? &&
incoming_email&.created_via == IncomingEmail.created_via_types[:handle_mail]
return "<#{first_post.incoming_email.message_id}>"
end