2020-04-30 14:50:26 +00:00
|
|
|
class ApplicationMailbox < ActionMailbox::Base
|
2021-10-12 15:16:00 +00:00
|
|
|
include MailboxHelper
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
# Last part is the regex for the UUID
|
2020-07-19 08:04:34 +00:00
|
|
|
# Eg: email should be something like : reply+6bdc3f4d-0bec-4515-a284-5d916fdde489@domain.com
|
2021-10-12 15:16:00 +00:00
|
|
|
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+)}
|
2020-04-30 14:50:26 +00:00
|
|
|
|
2020-09-21 17:14:22 +00:00
|
|
|
def self.reply_mail?
|
2020-04-30 14:50:26 +00:00
|
|
|
proc do |inbound_mail_obj|
|
|
|
|
is_a_reply_email = false
|
2021-03-27 06:57:48 +00:00
|
|
|
inbound_mail_obj.mail.to&.each do |email|
|
2021-10-12 15:16:00 +00:00
|
|
|
is_a_reply_email = true if reply_uuid_mail?(email)
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
2021-10-12 15:16:00 +00:00
|
|
|
is_a_reply_email = true if in_reply_to_mail?(inbound_mail_obj, is_a_reply_email)
|
2020-04-30 14:50:26 +00:00
|
|
|
is_a_reply_email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-21 17:14:22 +00:00
|
|
|
def self.support_mail?
|
|
|
|
proc do |inbound_mail_obj|
|
|
|
|
is_a_support_email = false
|
2021-03-27 06:57:48 +00:00
|
|
|
inbound_mail_obj.mail.to&.each do |email|
|
2021-09-17 16:44:39 +00:00
|
|
|
channel = Channel::Email.find_by('lower(email) = ? OR lower(forward_to_email) = ?', email.downcase, email.downcase)
|
2020-09-21 17:14:22 +00:00
|
|
|
if channel.present?
|
|
|
|
is_a_support_email = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
is_a_support_email
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.catch_all_mail?
|
2020-04-30 14:50:26 +00:00
|
|
|
proc { |_mail| true }
|
|
|
|
end
|
|
|
|
|
2021-10-12 15:16:00 +00:00
|
|
|
# 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
|
|
|
|
|
2021-10-14 07:25:46 +00:00
|
|
|
in_reply_to = inbound_mail_obj.mail.in_reply_to
|
2021-10-12 15:16:00 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
# routing should be defined below the referenced procs
|
2021-03-04 08:29:59 +00:00
|
|
|
|
|
|
|
# routes as a reply to existing conversations
|
2020-09-21 17:14:22 +00:00
|
|
|
routing(reply_mail? => :reply)
|
2021-03-04 08:29:59 +00:00
|
|
|
# routes as a new conversation in email channel
|
2020-09-21 17:14:22 +00:00
|
|
|
routing(support_mail? => :support)
|
|
|
|
routing(catch_all_mail? => :default)
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|