2021-12-05 19:34:39 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Email
|
|
|
|
##
|
|
|
|
# Email Message-IDs are used in both our outbound and inbound email
|
|
|
|
# flow. For the outbound flow via Email::Sender, we assign a unique
|
|
|
|
# Message-ID for any emails sent out from the application.
|
2023-01-11 22:54:15 -05:00
|
|
|
# If we are sending an email related to a post, such as through the
|
2021-12-05 19:34:39 -05:00
|
|
|
# PostAlerter class, then the Message-ID will contain references to
|
2023-01-11 22:54:15 -05:00
|
|
|
# the post ID. The host must also be included on the Message-IDs.
|
|
|
|
# The format looks like this:
|
|
|
|
#
|
|
|
|
# discourse/post/POST_ID@HOST
|
|
|
|
#
|
|
|
|
# We previously had the following formats, but support for these
|
|
|
|
# will be removed in 2023:
|
|
|
|
#
|
|
|
|
# topic/TOPIC_ID/POST_ID@HOST
|
|
|
|
# topic/TOPIC_ID@HOST
|
2021-12-05 19:34:39 -05:00
|
|
|
#
|
|
|
|
# For the inbound email flow via Email::Receiver, we use Message-IDs
|
2023-01-11 22:54:15 -05:00
|
|
|
# to discern which topic and post the inbound email reply should be
|
2021-12-05 19:34:39 -05:00
|
|
|
# in response to. In this case, the Message-ID is extracted from the
|
|
|
|
# References and/or In-Reply-To headers, and compared with either
|
|
|
|
# the IncomingEmail table, the Post table, or the IncomingEmail to
|
|
|
|
# determine where to send the reply.
|
|
|
|
#
|
|
|
|
# See https://datatracker.ietf.org/doc/html/rfc2822#section-3.6.4 for
|
|
|
|
# more specific information around Message-IDs in email.
|
|
|
|
#
|
|
|
|
# See https://tools.ietf.org/html/rfc850#section-2.1.7 for the
|
|
|
|
# Message-ID format specification.
|
|
|
|
class MessageIdService
|
|
|
|
class << self
|
|
|
|
def generate_default
|
|
|
|
"<#{SecureRandom.uuid}@#{host}>"
|
|
|
|
end
|
|
|
|
|
2022-09-25 19:14:24 -04:00
|
|
|
##
|
|
|
|
# The outbound_message_id may be present because either:
|
|
|
|
#
|
|
|
|
# * The post was created via incoming email and Email::Receiver, and
|
|
|
|
# references a Message-ID generated by an external email client or service.
|
|
|
|
# * At least one email has been sent because of the post being created
|
|
|
|
# to inform interested parties via email.
|
|
|
|
#
|
|
|
|
# If it is blank then we should assume Discourse was the originator
|
|
|
|
# of the post, and generate a Message-ID to be used from now on using
|
|
|
|
# our discourse/post/POST_ID@HOST format.
|
|
|
|
def generate_or_use_existing(post_ids)
|
|
|
|
post_ids = Array.wrap(post_ids)
|
|
|
|
return [] if post_ids.empty?
|
|
|
|
|
|
|
|
DB.exec(<<~SQL, host: host)
|
|
|
|
UPDATE posts
|
|
|
|
SET outbound_message_id = 'discourse/post/' || posts.id || '@' || :host
|
|
|
|
WHERE outbound_message_id IS NULL AND posts.id IN (#{post_ids.join(",")});
|
|
|
|
SQL
|
|
|
|
|
|
|
|
DB.query_single(<<~SQL)
|
|
|
|
SELECT '<' || posts.outbound_message_id || '>'
|
|
|
|
FROM posts
|
|
|
|
WHERE posts.id IN (#{post_ids.join(",")})
|
|
|
|
ORDER BY posts.created_at ASC;
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Uses extracted Message-IDs from both the In-Reply-To and References
|
|
|
|
# headers from an incoming email.
|
2021-12-05 19:34:39 -05:00
|
|
|
def find_post_from_message_ids(message_ids)
|
|
|
|
message_ids = message_ids.map { |message_id| message_id_clean(message_id) }
|
2022-09-25 19:14:24 -04:00
|
|
|
|
2023-01-11 22:54:15 -05:00
|
|
|
# TODO (martin) 2023-04-01 We should remove these backwards-compatible
|
2022-09-25 19:14:24 -04:00
|
|
|
# formats for the Message-ID and solely use the discourse/post/999@host
|
|
|
|
# format.
|
|
|
|
topic_ids =
|
|
|
|
message_ids
|
|
|
|
.map { |message_id| message_id[message_id_topic_id_regexp, 1] }
|
|
|
|
.compact
|
|
|
|
.map(&:to_i)
|
|
|
|
post_ids =
|
|
|
|
message_ids
|
|
|
|
.map { |message_id| message_id[message_id_post_id_regexp, 1] }
|
|
|
|
.compact
|
|
|
|
.map(&:to_i)
|
2023-01-09 07:10:19 -05:00
|
|
|
|
2022-09-25 19:14:24 -04:00
|
|
|
post_ids << message_ids
|
|
|
|
.map { |message_id| message_id[message_id_discourse_regexp, 1] }
|
2023-01-09 07:10:19 -05:00
|
|
|
.compact
|
2022-09-25 19:14:24 -04:00
|
|
|
.map(&:to_i)
|
2023-01-09 07:10:19 -05:00
|
|
|
|
2022-09-25 19:14:24 -04:00
|
|
|
post_ids << Post
|
|
|
|
.where(outbound_message_id: message_ids)
|
|
|
|
.or(Post.where(topic_id: topic_ids, post_number: 1))
|
|
|
|
.pluck(:id)
|
2021-12-05 19:34:39 -05:00
|
|
|
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
|
|
|
|
|
2023-01-11 22:54:15 -05:00
|
|
|
# TODO (martin) 2023-04-01 We should remove these backwards-compatible
|
2022-09-25 19:14:24 -04:00
|
|
|
# formats for the Message-ID and solely use the discourse/post/999@host
|
|
|
|
# format.
|
2021-12-05 19:34:39 -05:00
|
|
|
def discourse_generated_message_id?(message_id)
|
|
|
|
!!(message_id =~ message_id_post_id_regexp) ||
|
2022-09-25 19:14:24 -04:00
|
|
|
!!(message_id =~ message_id_topic_id_regexp) ||
|
|
|
|
!!(message_id =~ message_id_discourse_regexp)
|
2021-12-05 19:34:39 -05:00
|
|
|
end
|
|
|
|
|
2023-01-11 22:54:15 -05:00
|
|
|
# TODO (martin) 2023-04-01 We should remove these backwards-compatible
|
2022-09-25 19:14:24 -04:00
|
|
|
# formats for the Message-ID and solely use the discourse/post/999@host
|
|
|
|
# format.
|
2021-12-05 19:34:39 -05:00
|
|
|
def message_id_post_id_regexp
|
2021-12-08 06:17:20 -05:00
|
|
|
Regexp.new "topic/\\d+/(\\d+|\\d+\.\\w+)@#{Regexp.escape(host)}"
|
2021-12-05 19:34:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def message_id_topic_id_regexp
|
2021-12-08 06:17:20 -05:00
|
|
|
Regexp.new "topic/(\\d+|\\d+\.\\w+)@#{Regexp.escape(host)}"
|
2021-12-05 19:34:39 -05:00
|
|
|
end
|
|
|
|
|
2022-09-25 19:14:24 -04:00
|
|
|
def message_id_discourse_regexp
|
|
|
|
Regexp.new "discourse/post/(\\d+)@#{Regexp.escape(host)}"
|
|
|
|
end
|
|
|
|
|
2021-12-05 19:34:39 -05:00
|
|
|
def message_id_rfc_format(message_id)
|
|
|
|
message_id.present? && !is_message_id_rfc?(message_id) ? "<#{message_id}>" : message_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def message_id_clean(message_id)
|
|
|
|
if message_id.present? && is_message_id_rfc?(message_id)
|
2023-01-20 13:52:49 -05:00
|
|
|
message_id.gsub(/\A<|>\z/, "")
|
2023-01-09 07:10:19 -05:00
|
|
|
else
|
2021-12-05 19:34:39 -05:00
|
|
|
message_id
|
2023-01-09 07:10:19 -05:00
|
|
|
end
|
2021-12-05 19:34:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_message_id_rfc?(message_id)
|
|
|
|
message_id.start_with?("<") && message_id.include?("@") && message_id.end_with?(">")
|
|
|
|
end
|
|
|
|
|
|
|
|
def host
|
2021-12-08 06:17:20 -05:00
|
|
|
Email::Sender.host_for(Discourse.base_url)
|
2021-12-05 19:34:39 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|