mirror of
https://github.com/discourse/discourse.git
synced 2025-09-10 12:04:21 +08:00
FIX: Add random suffix to outbound Message-ID for email (#15179)
Currently the Message-IDs we send out for outbound email are not unique; for a post they look like: topic/TOPIC_ID/POST_ID@HOST And for a topic they look like: topic/TOPIC_ID@HOST This commit changes the outbound Message-IDs to also have a random suffix before the host, so the new format is like this: topic/TOPIC_ID/POST_ID.RANDOM_SUFFIX@HOST Or: topic/TOPIC_ID.RANDOM_SUFFIX@HOST This should help with email deliverability. This change is backwards-compatible, the old Message-ID format will still be recognized in the mail receiver flow, so people will still be able to reply using Message-IDs, In-Reply-To, and References headers that have already been sent. This commit also refactors Message-ID related logic to a central location, and adds judicious amounts of tests and documentation.
This commit is contained in:
parent
11d1c520ff
commit
3b13f1146b
12 changed files with 304 additions and 104 deletions
|
@ -107,7 +107,7 @@ module Email
|
|||
# server (e.g. a message_id generated by Gmail) and does not need to
|
||||
# be updated, because message_ids from the IMAP server are not guaranteed
|
||||
# to be unique.
|
||||
return unless discourse_generated_message_id?(@message_id)
|
||||
return unless Email::MessageIdService.discourse_generated_message_id?(@message_id)
|
||||
|
||||
incoming_email.update(
|
||||
imap_uid_validity: @opts[:imap_uid_validity],
|
||||
|
@ -801,7 +801,7 @@ module Email
|
|||
# if the user is directly replying to an email send to them from discourse,
|
||||
# there will be a corresponding EmailLog record, so we can use that as the
|
||||
# reply post if it exists
|
||||
if discourse_generated_message_id?(mail.in_reply_to)
|
||||
if Email::MessageIdService.discourse_generated_message_id?(mail.in_reply_to)
|
||||
post_id_from_email_log = EmailLog.where(message_id: mail.in_reply_to)
|
||||
.addressed_to_user(user)
|
||||
.order(created_at: :desc)
|
||||
|
@ -1056,35 +1056,7 @@ module Email
|
|||
message_ids = Email::Receiver.extract_reply_message_ids(@mail, max_message_id_count: 5)
|
||||
return if message_ids.empty?
|
||||
|
||||
post_ids = message_ids.map { |message_id| message_id[message_id_post_id_regexp, 1] }.compact.map(&:to_i)
|
||||
post_ids << Post.where(topic_id: message_ids.map { |message_id| message_id[message_id_topic_id_regexp, 1] }.compact, post_number: 1).pluck(:id)
|
||||
post_ids << EmailLog.where(message_id: message_ids).pluck(:post_id)
|
||||
post_ids << IncomingEmail.where(message_id: message_ids).pluck(:post_id)
|
||||
|
||||
post_ids.flatten!
|
||||
post_ids.compact!
|
||||
post_ids.uniq!
|
||||
|
||||
return if post_ids.empty?
|
||||
|
||||
Post.where(id: post_ids).order(:created_at).last
|
||||
end
|
||||
|
||||
def host
|
||||
@host ||= Email::Sender.host_for(Discourse.base_url)
|
||||
end
|
||||
|
||||
def discourse_generated_message_id?(message_id)
|
||||
!!(message_id =~ message_id_post_id_regexp) ||
|
||||
!!(message_id =~ message_id_topic_id_regexp)
|
||||
end
|
||||
|
||||
def message_id_post_id_regexp
|
||||
@message_id_post_id_regexp ||= Regexp.new "topic/\\d+/(\\d+)@#{Regexp.escape(host)}"
|
||||
end
|
||||
|
||||
def message_id_topic_id_regexp
|
||||
@message_id_topic_id_regexp ||= Regexp.new "topic/(\\d+)@#{Regexp.escape(host)}"
|
||||
Email::MessageIdService.find_post_from_message_ids(message_ids)
|
||||
end
|
||||
|
||||
def self.extract_reply_message_ids(mail, max_message_id_count:)
|
||||
|
@ -1100,7 +1072,7 @@ module Email
|
|||
references
|
||||
elsif references.present?
|
||||
references.split(/[\s,]/).map do |r|
|
||||
Email.message_id_clean(r)
|
||||
Email::MessageIdService.message_id_clean(r)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue