refactor: strategy pattern for mailbox conversation finding (#12766)
Co-authored-by: Pranav <pranav@chatwoot.com> Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -3,6 +3,8 @@ class Imap::ImapMailbox
|
||||
include IncomingEmailValidityHelper
|
||||
attr_accessor :channel, :account, :inbox, :conversation, :processed_mail
|
||||
|
||||
FALLBACK_CONVERSATION_PATTERN = %r{account/(\d+)/conversation/([a-zA-Z0-9-]+)@}
|
||||
|
||||
def process(mail, channel)
|
||||
@inbound_mail = mail
|
||||
@channel = channel
|
||||
@@ -49,19 +51,32 @@ class Imap::ImapMailbox
|
||||
end
|
||||
|
||||
def find_conversation_by_reference_ids
|
||||
return if @inbound_mail.references.blank? && in_reply_to.present?
|
||||
return if @inbound_mail.references.blank?
|
||||
|
||||
message = find_message_by_references
|
||||
if message.present?
|
||||
conversation = @inbox.conversations.find_by(id: message.conversation_id)
|
||||
return conversation if conversation.present?
|
||||
end
|
||||
|
||||
return if message.nil?
|
||||
|
||||
@inbox.conversations.find(message.conversation_id)
|
||||
# FALLBACK_PATTERN use to find a conversation that is started by an agent (no incoming message yet)
|
||||
conversation_id = find_conversation_by_references
|
||||
@inbox.conversations.find_by(uuid: conversation_id) if conversation_id.present?
|
||||
end
|
||||
|
||||
def in_reply_to
|
||||
@processed_mail.in_reply_to
|
||||
end
|
||||
|
||||
def find_conversation_by_references
|
||||
references = Array.wrap(@inbound_mail.references)
|
||||
references.each do |message_id|
|
||||
match = FALLBACK_CONVERSATION_PATTERN.match(message_id)
|
||||
|
||||
return match[2] if match.present?
|
||||
end
|
||||
end
|
||||
|
||||
def find_message_by_references
|
||||
message_to_return = nil
|
||||
|
||||
|
||||
Reference in New Issue
Block a user