mirror of
https://github.com/discourse/discourse.git
synced 2024-11-28 08:23:43 +08:00
e3d495850d
See https://meta.discourse.org/t/discourse-email-messages-are-incorrectly-threaded/233499 for thorough reasoning. This commit changes how we generate Message-IDs and do email threading for emails sent from Discourse. The main changes are as follows: * Introduce an outbound_message_id column on Post that is either a) filled with a Discourse-generated Message-ID the first time that post is used for an outbound email or b) filled with an original Message-ID from an external mail client or service if the post was created from an incoming email. * Change Discourse-generated Message-IDs to be more consistent and static, in the format `discourse/post/:post_id@:host` * Do not send References or In-Reply-To headers for emails sent for the OP of topics. * Make sure that In-Reply-To is filled with either a) the OP's Message-ID if the post is not a direct reply or b) the parent post's Message-ID * Make sure that In-Reply-To has all referenced post's Message-IDs * Make sure that References is filled with a chain of Message-IDs from the OP down to the parent post of the new post. We also are keeping X-Discourse-Post-Id and X-Discourse-Topic-Id, headers that we previously removed, for easier visual debugging of outbound emails. Finally, we backfill the `outbound_message_id` for posts that have a linked `IncomingEmail` record, using the `message_id` of that record. We do not need to do that for posts that don't have an incoming email since they are backfilled at runtime if `outbound_message_id` is missing.
26 lines
762 B
Ruby
26 lines
762 B
Ruby
# frozen_string_literal: true
|
|
|
|
class BackfillOutboundMessageId < ActiveRecord::Migration[7.0]
|
|
def up
|
|
# best effort backfill, we don't care about years worth of message_id
|
|
# preservation
|
|
#
|
|
# we also don't need to backfill outbound_message_id for posts that
|
|
# do _not_ have an incoming email linked, since that will be backfilled
|
|
# at runtime if it is missing
|
|
sql_query = <<~SQL
|
|
UPDATE posts
|
|
SET outbound_message_id = ie.message_id
|
|
FROM incoming_emails AS ie
|
|
WHERE ie.post_id = posts.id
|
|
AND posts.created_at >= :one_year_ago
|
|
AND posts.outbound_message_id IS NULL
|
|
SQL
|
|
DB.exec(sql_query, one_year_ago: 1.year.ago)
|
|
end
|
|
|
|
def down
|
|
raise ActiveRecord::IrreversibleMigration
|
|
end
|
|
end
|