2020-09-21 17:14:22 +00:00
|
|
|
class SupportMailbox < ApplicationMailbox
|
|
|
|
attr_accessor :channel, :account, :inbox, :conversation, :processed_mail
|
|
|
|
|
|
|
|
before_processing :find_channel,
|
|
|
|
:load_account,
|
|
|
|
:load_inbox,
|
|
|
|
:decorate_mail
|
|
|
|
|
|
|
|
def process
|
2021-07-15 17:20:32 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
find_or_create_contact
|
2021-10-12 15:16:00 +00:00
|
|
|
find_or_create_conversation
|
2021-07-15 17:20:32 +00:00
|
|
|
create_message
|
|
|
|
add_attachments_to_message
|
|
|
|
end
|
2020-09-21 17:14:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_channel
|
|
|
|
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
|
|
|
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
|
|
|
|
|
2021-10-12 15:16:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-09-21 17:14:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_create_contact
|
2021-08-24 08:48:08 +00:00
|
|
|
@contact = @inbox.contacts.find_by(email: @processed_mail.original_sender)
|
2020-09-21 17:14:22 +00:00
|
|
|
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,
|
2021-08-24 08:48:08 +00:00
|
|
|
email: @processed_mail.original_sender,
|
2020-09-21 17:14:22 +00:00
|
|
|
additional_attributes: {
|
|
|
|
source_id: "email:#{processed_mail.message_id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).perform
|
|
|
|
@contact = @contact_inbox.contact
|
|
|
|
end
|
|
|
|
|
|
|
|
def identify_contact_name
|
2021-09-16 07:56:52 +00:00
|
|
|
processed_mail.sender_name || processed_mail.from.first.split('@').first
|
2020-09-21 17:14:22 +00:00
|
|
|
end
|
|
|
|
end
|