Chatwoot/app/mailboxes/reply_mailbox.rb
Sony Mathew f9b0427751
feat: email channel backend (#140) (#1255)
* feat: added support mailbox to handle email channel (#140)

Added a new mailbox called 'SupportMailbox' to handle all the
incoming emails other than reply emails.

An email channel will have a support email and forward email
associated with it. So we filter for the right email inbox based on
the support email of that inbox and route this to this mailbox.

This mailbox finds the account, inbox, contact (create a new one
if it does not exist) and creates a conversation and adds the
email content as the first message in the conversation.

Other minor things handled in this commit:

* renamed the procs for routing emails in application mailbox
* renamed ConversationMailbox to ReplyMailbox
* Added a fallback content in MailPresenter
* Added a record saving (bang) versions of enabling and disabling
features in Featurable module
* added new factory for the email channel

refs: #140
2020-09-21 22:44:22 +05:30

52 lines
1.4 KiB
Ruby

class ReplyMailbox < ApplicationMailbox
include MailboxHelper
attr_accessor :conversation_uuid, :processed_mail
# Last part is the regex for the UUID
# Eg: email should be something like : reply+6bdc3f4d-0bec-4515-a284-5d916fdde489@domain.com
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.freeze
before_processing :conversation_uuid_from_to_address,
:verify_decoded_params,
:find_conversation,
:decorate_mail
def process
create_message
add_attachments_to_message
end
private
def conversation_uuid_from_to_address
mail.to.each do |email|
username = email.split('@')[0]
match_result = username.match(ApplicationMailbox::REPLY_EMAIL_USERNAME_PATTERN)
if match_result
@conversation_uuid = match_result.captures
break
end
end
@conversation_uuid
end
def verify_decoded_params
raise 'Conversation uuid not found' if conversation_uuid.nil?
end
def find_conversation
@conversation = Conversation.find_by(uuid: conversation_uuid)
validate_resource @conversation
end
def validate_resource(resource)
raise "#{resource.class.name} not found" if resource.nil?
resource
end
def decorate_mail
@processed_mail = MailPresenter.new(mail, @conversation.account)
end
end