2020-04-30 14:50:26 +00:00
|
|
|
class ApplicationMailbox < ActionMailbox::Base
|
|
|
|
# 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
|
|
|
|
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.freeze
|
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|
|
2020-04-30 14:50:26 +00:00
|
|
|
username = email.split('@')[0]
|
|
|
|
match_result = username.match(REPLY_EMAIL_USERNAME_PATTERN)
|
|
|
|
if match_result
|
|
|
|
is_a_reply_email = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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-03-04 08:29:59 +00:00
|
|
|
channel = Channel::Email.find_by('email = ? OR forward_to_email = ?', email, email)
|
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
|
|
|
|
|
|
|
|
# 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
|