2020-03-01 13:36:13 +00:00
|
|
|
class ConversationReplyMailer < ApplicationMailer
|
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
|
|
|
|
2020-03-01 13:36:13 +00:00
|
|
|
def reply_with_summary(conversation, message_queued_time)
|
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
|
|
|
|
2020-05-10 07:44:57 +00:00
|
|
|
recap_messages = @conversation.messages.chat.where('created_at < ?', message_queued_time).last(10)
|
|
|
|
new_messages = @conversation.messages.chat.where('created_at >= ?', message_queued_time)
|
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?)
|
2020-01-23 17:29:07 +00:00
|
|
|
|
2020-05-22 12:44:18 +00:00
|
|
|
mail({
|
|
|
|
to: @contact&.email,
|
2020-10-11 17:54:11 +00:00
|
|
|
from: from_email_with_name,
|
2020-05-22 12:44:18 +00:00
|
|
|
reply_to: reply_email,
|
2020-07-15 11:03:52 +00:00
|
|
|
subject: mail_subject,
|
|
|
|
message_id: custom_message_id,
|
|
|
|
in_reply_to: in_reply_to_email
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_without_summary(conversation, message_queued_time)
|
|
|
|
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-08-23 16:30:47 +00:00
|
|
|
@messages = @conversation.messages.chat.where(message_type: [:outgoing, :template]).where('created_at >= ?', message_queued_time)
|
|
|
|
@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
|
|
|
|
|
|
|
mail({
|
|
|
|
to: @contact&.email,
|
2020-10-11 17:54:11 +00:00
|
|
|
from: from_email_with_name,
|
2020-07-15 11:03:52 +00:00
|
|
|
reply_to: reply_email,
|
|
|
|
subject: mail_subject,
|
2020-05-22 12:44:18 +00:00
|
|
|
message_id: custom_message_id,
|
|
|
|
in_reply_to: in_reply_to_email
|
|
|
|
})
|
2020-01-23 17:29:07 +00:00
|
|
|
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
|
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-01-17 17:13:32 +00:00
|
|
|
return "Re: #{incoming_mail_subject}" if incoming_mail_subject
|
|
|
|
|
2020-05-22 12:44:18 +00:00
|
|
|
subject_line = I18n.t('conversations.reply.email_subject')
|
2020-03-30 10:37:01 +00:00
|
|
|
"[##{@conversation.display_id}] #{subject_line}"
|
2020-01-23 17:29:07 +00:00
|
|
|
end
|
2020-04-30 14:50:26 +00:00
|
|
|
|
2021-01-17 17:13:32 +00:00
|
|
|
def incoming_mail_subject
|
|
|
|
@incoming_mail_subject ||= @conversation.additional_attributes['mail_subject']
|
|
|
|
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-03-12 10:07:06 +00:00
|
|
|
"#{assignee_name} <reply+#{@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-03-12 10:07:06 +00:00
|
|
|
"#{assignee_name} from #{@inbox.name} <#{parse_email(@account.support_email)}>"
|
2020-04-30 14:50:26 +00:00
|
|
|
else
|
2021-03-12 10:07:06 +00:00
|
|
|
"#{assignee_name} from #{@inbox.name} <#{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-03-12 10:07:06 +00:00
|
|
|
"<conversation/#{@conversation.uuid}/messages/#{@messages&.last&.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
|
|
|
|
|
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
|
|
|
|
return false if action_name == 'reply_without_summary'
|
|
|
|
|
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
|