f9b0427751
* 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
84 lines
2.3 KiB
Ruby
84 lines
2.3 KiB
Ruby
class SupportMailbox < ApplicationMailbox
|
|
include MailboxHelper
|
|
|
|
attr_accessor :channel, :account, :inbox, :conversation, :processed_mail
|
|
|
|
before_processing :find_channel,
|
|
:load_account,
|
|
:load_inbox,
|
|
:decorate_mail
|
|
|
|
def process
|
|
find_or_create_contact
|
|
create_conversation
|
|
create_message
|
|
add_attachments_to_message
|
|
end
|
|
|
|
private
|
|
|
|
def find_channel
|
|
mail.to.each do |email|
|
|
@channel = Channel::Email.find_by(email: email)
|
|
break if @channel.present?
|
|
end
|
|
raise 'Email channel/inbox not found' if @channel.nil?
|
|
|
|
@channel
|
|
end
|
|
|
|
def load_account
|
|
@account = @channel.account
|
|
end
|
|
|
|
def load_inbox
|
|
@inbox = @channel.inbox
|
|
end
|
|
|
|
def decorate_mail
|
|
@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',
|
|
initiated_at: {
|
|
timestamp: Time.now.utc
|
|
}
|
|
}
|
|
})
|
|
end
|
|
|
|
def find_or_create_contact
|
|
@contact = @inbox.contacts.find_by(email: processed_mail.from.first)
|
|
if @contact.present?
|
|
@contact_inbox = ContactInbox.find_by(inbox: @inbox, contact: @contact)
|
|
else
|
|
create_contact
|
|
end
|
|
end
|
|
|
|
def create_contact
|
|
@contact_inbox = ::ContactBuilder.new(
|
|
source_id: "email:#{processed_mail.message_id}",
|
|
inbox: @inbox,
|
|
contact_attributes: {
|
|
name: identify_contact_name,
|
|
email: processed_mail.from.first,
|
|
additional_attributes: {
|
|
source_id: "email:#{processed_mail.message_id}"
|
|
}
|
|
}
|
|
).perform
|
|
@contact = @contact_inbox.contact
|
|
end
|
|
|
|
def identify_contact_name
|
|
processed_mail.from.first.split('@').first
|
|
end
|
|
end
|