fix: Update email message_id parsing order (#3073)
Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
@@ -1,19 +1,18 @@
|
||||
class ApplicationMailbox < ActionMailbox::Base
|
||||
include MailboxHelper
|
||||
|
||||
# Last part is the regex for the UUID
|
||||
# Eg: email should be something like : reply+6bdc3f4d-0bec-4515-a284-5d916fdde489@domain.com
|
||||
REPLY_EMAIL_USERNAME_PATTERN = /^reply\+([0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12})$/i
|
||||
REPLY_EMAIL_UUID_PATTERN = /^reply\+([0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12})$/i
|
||||
CONVERSATION_MESSAGE_ID_PATTERN = %r{conversation/([a-zA-Z0-9\-]*?)/messages/(\d+?)@(\w+\.\w+)}
|
||||
|
||||
def self.reply_mail?
|
||||
proc do |inbound_mail_obj|
|
||||
is_a_reply_email = false
|
||||
inbound_mail_obj.mail.to&.each do |email|
|
||||
username = email.split('@')[0]
|
||||
match_result = username.match(REPLY_EMAIL_USERNAME_PATTERN)
|
||||
if match_result
|
||||
is_a_reply_email = true
|
||||
break
|
||||
end
|
||||
is_a_reply_email = true if reply_uuid_mail?(email)
|
||||
end
|
||||
is_a_reply_email = true if in_reply_to_mail?(inbound_mail_obj, is_a_reply_email)
|
||||
is_a_reply_email
|
||||
end
|
||||
end
|
||||
@@ -36,6 +35,30 @@ class ApplicationMailbox < ActionMailbox::Base
|
||||
proc { |_mail| true }
|
||||
end
|
||||
|
||||
# checks if follow this pattern then send it to reply_mailbox
|
||||
# <account/#{@account.id}/conversation/#{@conversation.uuid}@#{@account.inbound_email_domain}>
|
||||
def self.in_reply_to_mail?(inbound_mail_obj, is_a_reply_email)
|
||||
return if is_a_reply_email
|
||||
|
||||
in_reply_to = inbound_mail_obj.mail['In-Reply-To'].value
|
||||
|
||||
return false if in_reply_to.blank?
|
||||
|
||||
return true if in_reply_to.match(CONVERSATION_MESSAGE_ID_PATTERN)
|
||||
|
||||
message = Message.find_by(source_id: in_reply_to)
|
||||
return true if message.present?
|
||||
|
||||
false
|
||||
end
|
||||
|
||||
# checks if follow this pattern send it to reply_mailbox
|
||||
# reply+<conversation-uuid>@<mailer-domain.com>
|
||||
def self.reply_uuid_mail?(email)
|
||||
conversation_uuid = email.split('@')[0]
|
||||
conversation_uuid.match(REPLY_EMAIL_UUID_PATTERN)
|
||||
end
|
||||
|
||||
# routing should be defined below the referenced procs
|
||||
|
||||
# routes as a reply to existing conversations
|
||||
|
||||
Reference in New Issue
Block a user