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
|
||||
|
||||
@@ -11,7 +11,9 @@ module MailboxHelper
|
||||
content_type: 'incoming_email',
|
||||
source_id: processed_mail.message_id,
|
||||
content_attributes: {
|
||||
email: processed_mail.serialized_data
|
||||
email: processed_mail.serialized_data,
|
||||
cc_email: processed_mail.cc,
|
||||
bcc_email: processed_mail.bcc
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
@@ -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?
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
class SupportMailbox < ApplicationMailbox
|
||||
include MailboxHelper
|
||||
|
||||
attr_accessor :channel, :account, :inbox, :conversation, :processed_mail
|
||||
|
||||
before_processing :find_channel,
|
||||
@@ -11,7 +9,7 @@ class SupportMailbox < ApplicationMailbox
|
||||
def process
|
||||
ActiveRecord::Base.transaction do
|
||||
find_or_create_contact
|
||||
create_conversation
|
||||
find_or_create_conversation
|
||||
create_message
|
||||
add_attachments_to_message
|
||||
end
|
||||
@@ -41,20 +39,31 @@ class SupportMailbox < ApplicationMailbox
|
||||
@processed_mail = MailPresenter.new(mail, @account)
|
||||
end
|
||||
|
||||
def create_conversation
|
||||
@conversation = ::Conversation.create!({
|
||||
account_id: @account.id,
|
||||
inbox_id: @inbox.id,
|
||||
contact_id: @contact.id,
|
||||
contact_inbox_id: @contact_inbox.id,
|
||||
additional_attributes: {
|
||||
source: 'email',
|
||||
mail_subject: @processed_mail.subject,
|
||||
initiated_at: {
|
||||
timestamp: Time.now.utc
|
||||
}
|
||||
}
|
||||
})
|
||||
def find_conversation_by_in_reply_to
|
||||
return if in_reply_to.blank?
|
||||
|
||||
@account.conversations.where("additional_attributes->>'in_reply_to' = ?", in_reply_to).first
|
||||
end
|
||||
|
||||
def in_reply_to
|
||||
mail['In-Reply-To'].try(:value)
|
||||
end
|
||||
|
||||
def find_or_create_conversation
|
||||
@conversation = find_conversation_by_in_reply_to || ::Conversation.create!({
|
||||
account_id: @account.id,
|
||||
inbox_id: @inbox.id,
|
||||
contact_id: @contact.id,
|
||||
contact_inbox_id: @contact_inbox.id,
|
||||
additional_attributes: {
|
||||
in_reply_to: in_reply_to,
|
||||
source: 'email',
|
||||
mail_subject: @processed_mail.subject,
|
||||
initiated_at: {
|
||||
timestamp: Time.now.utc
|
||||
}
|
||||
}
|
||||
})
|
||||
end
|
||||
|
||||
def find_or_create_contact
|
||||
|
||||
Reference in New Issue
Block a user