2020-03-01 13:36:13 +00:00
|
|
|
class ConversationReplyMailer < ApplicationMailer
|
2021-11-19 06:22:27 +00:00
|
|
|
include ConversationReplyMailerHelper
|
2021-03-12 10:07:06 +00:00
|
|
|
default from: ENV.fetch('MAILER_SENDER_EMAIL', 'Chatwoot <accounts@chatwoot.com>')
|
2020-07-19 17:38:07 +00:00
|
|
|
layout :choose_layout
|
2020-01-23 17:29:07 +00:00
|
|
|
|
2021-10-14 07:25:46 +00:00
|
|
|
def reply_with_summary(conversation, last_queued_id)
|
2020-01-23 17:29:07 +00:00
|
|
|
return unless smtp_config_set_or_development?
|
|
|
|
|
2020-08-13 19:17:24 +00:00
|
|
|
init_conversation_attributes(conversation)
|
2020-09-10 13:49:15 +00:00
|
|
|
return if conversation_already_viewed?
|
2020-01-23 17:29:07 +00:00
|
|
|
|
2021-10-14 07:25:46 +00:00
|
|
|
recap_messages = @conversation.messages.chat.where('id < ?', last_queued_id).last(10)
|
|
|
|
new_messages = @conversation.messages.chat.where('id >= ?', last_queued_id)
|
2020-01-23 17:29:07 +00:00
|
|
|
@messages = recap_messages + new_messages
|
2021-08-23 16:30:47 +00:00
|
|
|
@messages = @messages.select(&:email_reply_summarizable?)
|
2021-11-19 06:22:27 +00:00
|
|
|
prepare_mail(true)
|
2020-07-15 11:03:52 +00:00
|
|
|
end
|
|
|
|
|
2021-10-14 07:25:46 +00:00
|
|
|
def reply_without_summary(conversation, last_queued_id)
|
2020-07-15 11:03:52 +00:00
|
|
|
return unless smtp_config_set_or_development?
|
|
|
|
|
2020-08-13 19:17:24 +00:00
|
|
|
init_conversation_attributes(conversation)
|
2020-09-10 13:49:15 +00:00
|
|
|
return if conversation_already_viewed?
|
2020-07-15 11:03:52 +00:00
|
|
|
|
2021-10-14 07:25:46 +00:00
|
|
|
@messages = @conversation.messages.chat.where(message_type: [:outgoing, :template]).where('id >= ?', last_queued_id)
|
2021-08-23 16:30:47 +00:00
|
|
|
@messages = @messages.reject { |m| m.template? && !m.input_csat? }
|
2020-07-20 07:17:02 +00:00
|
|
|
return false if @messages.count.zero?
|
2020-07-15 11:03:52 +00:00
|
|
|
|
2021-11-19 06:22:27 +00:00
|
|
|
prepare_mail(false)
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|
|
|
|
|
2021-10-14 07:25:46 +00:00
|
|
|
def email_reply(message)
|
|
|
|
return unless smtp_config_set_or_development?
|
|
|
|
|
|
|
|
init_conversation_attributes(message.conversation)
|
|
|
|
@message = message
|
2021-11-19 06:22:27 +00:00
|
|
|
reply_mail_object = prepare_mail(true)
|
2021-10-14 07:25:46 +00:00
|
|
|
|
|
|
|
message.update(source_id: reply_mail_object.message_id)
|
|
|
|
end
|
|
|
|
|
2020-08-17 05:55:13 +00:00
|
|
|
def conversation_transcript(conversation, to_email)
|
|
|
|
return unless smtp_config_set_or_development?
|
|
|
|
|
|
|
|
init_conversation_attributes(conversation)
|
|
|
|
|
2021-08-23 16:30:47 +00:00
|
|
|
@messages = @conversation.messages.chat.select(&:conversation_transcriptable?)
|
2020-08-17 05:55:13 +00:00
|
|
|
|
|
|
|
mail({
|
|
|
|
to: to_email,
|
2020-10-11 17:54:11 +00:00
|
|
|
from: from_email_with_name,
|
2020-08-17 05:55:13 +00:00
|
|
|
subject: "[##{@conversation.display_id}] #{I18n.t('conversations.reply.transcript_subject')}"
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2020-01-23 17:29:07 +00:00
|
|
|
private
|
|
|
|
|
2020-08-13 19:17:24 +00:00
|
|
|
def init_conversation_attributes(conversation)
|
|
|
|
@conversation = conversation
|
|
|
|
@account = @conversation.account
|
|
|
|
@contact = @conversation.contact
|
|
|
|
@agent = @conversation.assignee
|
2020-10-11 17:54:11 +00:00
|
|
|
@inbox = @conversation.inbox
|
2021-11-19 06:22:27 +00:00
|
|
|
@channel = @inbox.channel
|
2020-08-13 19:17:24 +00:00
|
|
|
end
|
|
|
|
|
2021-01-17 17:13:32 +00:00
|
|
|
def should_use_conversation_email_address?
|
|
|
|
@inbox.inbox_type == 'Email' || inbound_email_enabled?
|
|
|
|
end
|
|
|
|
|
2020-09-10 13:49:15 +00:00
|
|
|
def conversation_already_viewed?
|
|
|
|
# whether contact already saw the message on widget
|
|
|
|
return unless @conversation.contact_last_seen_at
|
|
|
|
return unless last_outgoing_message&.created_at
|
|
|
|
|
|
|
|
@conversation.contact_last_seen_at > last_outgoing_message&.created_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_outgoing_message
|
|
|
|
@conversation.messages.chat.where.not(message_type: :incoming)&.last
|
|
|
|
end
|
|
|
|
|
2020-07-31 10:27:19 +00:00
|
|
|
def assignee_name
|
|
|
|
@assignee_name ||= @agent&.available_name || 'Notifications'
|
|
|
|
end
|
|
|
|
|
2020-07-15 11:03:52 +00:00
|
|
|
def mail_subject
|
2021-10-27 07:39:29 +00:00
|
|
|
subject = @conversation.additional_attributes['mail_subject']
|
|
|
|
return "[##{@conversation.display_id}] #{I18n.t('conversations.reply.email_subject')}" if subject.nil?
|
|
|
|
|
|
|
|
chat_count = @conversation.messages.chat.count
|
|
|
|
if chat_count > 1
|
|
|
|
"Re: #{subject}"
|
|
|
|
else
|
|
|
|
subject
|
|
|
|
end
|
2021-01-17 17:13:32 +00:00
|
|
|
end
|
|
|
|
|
2020-04-30 14:50:26 +00:00
|
|
|
def reply_email
|
2021-01-17 17:13:32 +00:00
|
|
|
if should_use_conversation_email_address?
|
2021-11-27 04:16:12 +00:00
|
|
|
I18n.t('conversations.reply.email.header.reply_with_name', assignee_name: assignee_name, inbox_name: @inbox.name,
|
|
|
|
reply_email: "#{@conversation.uuid}@#{@account.inbound_email_domain}")
|
2020-04-30 14:50:26 +00:00
|
|
|
else
|
2020-10-11 17:54:11 +00:00
|
|
|
@inbox.email_address || @agent&.email
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-11 17:54:11 +00:00
|
|
|
def from_email_with_name
|
2021-01-17 17:13:32 +00:00
|
|
|
if should_use_conversation_email_address?
|
2021-11-27 04:16:12 +00:00
|
|
|
I18n.t('conversations.reply.email.header.from_with_name', assignee_name: assignee_name, inbox_name: @inbox.name,
|
|
|
|
from_email: parse_email(@account.support_email))
|
2020-04-30 14:50:26 +00:00
|
|
|
else
|
2021-11-27 04:16:12 +00:00
|
|
|
I18n.t('conversations.reply.email.header.from_with_name', assignee_name: assignee_name, inbox_name: @inbox.name,
|
|
|
|
from_email: parse_email(inbox_from_email_address))
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-12 10:07:06 +00:00
|
|
|
def parse_email(email_string)
|
|
|
|
Mail::Address.new(email_string).address
|
|
|
|
end
|
|
|
|
|
|
|
|
def inbox_from_email_address
|
2020-10-11 17:54:11 +00:00
|
|
|
return @inbox.email_address if @inbox.email_address
|
|
|
|
|
2021-03-12 10:07:06 +00:00
|
|
|
@account.support_email
|
2020-10-11 17:54:11 +00:00
|
|
|
end
|
|
|
|
|
2020-05-22 12:44:18 +00:00
|
|
|
def custom_message_id
|
2021-10-14 07:25:46 +00:00
|
|
|
last_message = @message || @messages&.last
|
|
|
|
|
|
|
|
"<conversation/#{@conversation.uuid}/messages/#{last_message&.id}@#{@account.inbound_email_domain}>"
|
2020-05-22 12:44:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def in_reply_to_email
|
2021-03-12 10:07:06 +00:00
|
|
|
conversation_reply_email_id || "<account/#{@account.id}/conversation/#{@conversation.uuid}@#{@account.inbound_email_domain}>"
|
2021-01-17 17:13:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def conversation_reply_email_id
|
|
|
|
content_attributes = @conversation.messages.incoming.last&.content_attributes
|
|
|
|
|
2021-01-18 06:13:31 +00:00
|
|
|
if content_attributes && content_attributes['email'] && content_attributes['email']['message_id']
|
|
|
|
return "<#{content_attributes['email']['message_id']}>"
|
2021-01-17 17:13:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
2020-05-22 12:44:18 +00:00
|
|
|
end
|
|
|
|
|
2021-10-11 07:30:48 +00:00
|
|
|
def cc_bcc_emails
|
|
|
|
content_attributes = @conversation.messages.outgoing.last&.content_attributes
|
|
|
|
|
|
|
|
return [] unless content_attributes
|
|
|
|
return [] unless content_attributes[:cc_emails] || content_attributes[:bcc_emails]
|
|
|
|
|
|
|
|
[content_attributes[:cc_emails], content_attributes[:bcc_emails]]
|
|
|
|
end
|
|
|
|
|
2020-07-19 17:38:07 +00:00
|
|
|
def inbound_email_enabled?
|
2021-03-12 10:07:06 +00:00
|
|
|
@inbound_email_enabled ||= @account.feature_enabled?('inbound_emails') && @account.inbound_email_domain
|
|
|
|
.present? && @account.support_email.present?
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
2020-07-19 17:38:07 +00:00
|
|
|
|
|
|
|
def choose_layout
|
2021-10-14 07:25:46 +00:00
|
|
|
return false if action_name == 'reply_without_summary' || action_name == 'email_reply'
|
2020-07-19 17:38:07 +00:00
|
|
|
|
2020-08-06 09:51:06 +00:00
|
|
|
'mailer/base'
|
2020-07-19 17:38:07 +00:00
|
|
|
end
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|