fix: Update email message_id parsing order (#3073)

Co-authored-by: Pranav Raj S <pranav@chatwoot.com>
This commit is contained in:
Tejaswini Chile
2021-10-12 20:46:00 +05:30
committed by GitHub
parent 6bfa551c85
commit 6998e9aa2d
9 changed files with 818 additions and 48 deletions

View File

@@ -1,6 +1,4 @@
class ReplyMailbox < ApplicationMailbox
include MailboxHelper
attr_accessor :conversation_uuid, :processed_mail
# Last part is the regex for the UUID
@@ -8,8 +6,8 @@ class ReplyMailbox < ApplicationMailbox
EMAIL_PART_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
before_processing :conversation_uuid_from_to_address,
:find_relative_conversation,
:verify_decoded_params,
:find_conversation,
:decorate_mail
def process
@@ -19,10 +17,18 @@ class ReplyMailbox < ApplicationMailbox
private
def find_relative_conversation
if @conversation_uuid
find_conversation_with_uuid
elsif mail['In-Reply-To'].try(:value).present?
find_conversation_with_in_reply_to
end
end
def conversation_uuid_from_to_address
mail.to.each do |email|
username = email.split('@')[0]
match_result = username.match(ApplicationMailbox::REPLY_EMAIL_USERNAME_PATTERN)
match_result = username.match(ApplicationMailbox::REPLY_EMAIL_UUID_PATTERN)
if match_result
@conversation_uuid = match_result.captures
break
@@ -35,11 +41,38 @@ class ReplyMailbox < ApplicationMailbox
raise 'Conversation uuid not found' if conversation_uuid.nil?
end
def find_conversation
# find conversation uuid from below pattern
# reply+<conversation-uuid>@<mailer-domain.com>
def find_conversation_with_uuid
@conversation = Conversation.find_by(uuid: conversation_uuid)
validate_resource @conversation
end
def find_conversation_by_uuid(match_result)
@conversation_uuid = match_result.captures[0]
find_conversation_with_uuid
end
def find_conversation_by_message_id(in_reply_to)
@message = Message.find_by(source_id: in_reply_to)
@conversation = @message.conversation if @message.present?
@conversation_uuid = @conversation.uuid if @conversation.present?
end
# find conversation uuid from below pattern
# <conversation/#{@conversation.uuid}/messages/#{@messages&.last&.id}@#{@account.inbound_email_domain}>
def find_conversation_with_in_reply_to
in_reply_to = mail['In-Reply-To'].try(:value)
match_result = in_reply_to.match(ApplicationMailbox::CONVERSATION_MESSAGE_ID_PATTERN) if in_reply_to.present?
if match_result
find_conversation_by_uuid(match_result)
else
find_conversation_by_message_id(in_reply_to)
end
end
def validate_resource(resource)
raise "#{resource.class.name} not found" if resource.nil?