2020-09-21 17:14:22 +00:00
|
|
|
module MailboxHelper
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_message
|
2022-04-08 05:50:19 +00:00
|
|
|
return if @conversation.messages.find_by(source_id: processed_mail.message_id).present?
|
|
|
|
|
2022-09-13 12:10:06 +00:00
|
|
|
@message = @conversation.messages.create!(
|
2020-09-21 17:14:22 +00:00
|
|
|
account_id: @conversation.account_id,
|
|
|
|
sender: @conversation.contact,
|
2022-03-22 06:44:17 +00:00
|
|
|
content: mail_content&.truncate(150_000),
|
2020-09-21 17:14:22 +00:00
|
|
|
inbox_id: @conversation.inbox_id,
|
|
|
|
message_type: 'incoming',
|
|
|
|
content_type: 'incoming_email',
|
|
|
|
source_id: processed_mail.message_id,
|
|
|
|
content_attributes: {
|
2021-10-12 15:16:00 +00:00
|
|
|
email: processed_mail.serialized_data,
|
|
|
|
cc_email: processed_mail.cc,
|
|
|
|
bcc_email: processed_mail.bcc
|
2020-09-21 17:14:22 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_attachments_to_message
|
2022-06-15 10:50:19 +00:00
|
|
|
return if @message.blank?
|
|
|
|
|
2020-09-21 17:14:22 +00:00
|
|
|
processed_mail.attachments.each do |mail_attachment|
|
|
|
|
attachment = @message.attachments.new(
|
|
|
|
account_id: @conversation.account_id,
|
|
|
|
file_type: 'file'
|
|
|
|
)
|
|
|
|
attachment.file.attach(mail_attachment[:blob])
|
|
|
|
end
|
|
|
|
@message.save!
|
|
|
|
end
|
2021-11-15 13:45:51 +00:00
|
|
|
|
2021-11-19 06:22:27 +00:00
|
|
|
def create_contact
|
2022-10-13 22:12:04 +00:00
|
|
|
@contact_inbox = ::ContactInboxWithContactBuilder.new(
|
2022-07-08 09:39:06 +00:00
|
|
|
source_id: processed_mail.original_sender,
|
2021-11-19 06:22:27 +00:00
|
|
|
inbox: @inbox,
|
|
|
|
contact_attributes: {
|
|
|
|
name: identify_contact_name,
|
|
|
|
email: processed_mail.original_sender,
|
|
|
|
additional_attributes: {
|
|
|
|
source_id: "email:#{processed_mail.message_id}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).perform
|
|
|
|
@contact = @contact_inbox.contact
|
|
|
|
end
|
|
|
|
|
2021-11-15 13:45:51 +00:00
|
|
|
def notification_email_from_chatwoot?
|
|
|
|
# notification emails are send via mailer sender email address. so it should match
|
|
|
|
@processed_mail.original_sender == Mail::Address.new(ENV.fetch('MAILER_SENDER_EMAIL', 'Chatwoot <accounts@chatwoot.com>')).address
|
|
|
|
end
|
2022-01-27 23:45:26 +00:00
|
|
|
|
|
|
|
def mail_content
|
|
|
|
if processed_mail.text_content.present?
|
|
|
|
processed_mail.text_content[:reply]
|
|
|
|
elsif processed_mail.html_content.present?
|
|
|
|
processed_mail.html_content[:reply]
|
|
|
|
end
|
|
|
|
end
|
2020-09-21 17:14:22 +00:00
|
|
|
end
|