2020-03-01 13:36:13 +00:00
|
|
|
class ConversationReplyMailer < ApplicationMailer
|
2020-01-23 17:29:07 +00:00
|
|
|
default from: ENV.fetch('MAILER_SENDER_EMAIL', '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
|
|
|
|
@messages = @messages.select(&:reportable?)
|
|
|
|
|
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
|
|
|
|
2020-07-20 07:17:02 +00:00
|
|
|
@messages = @conversation.messages.chat.outgoing.where('created_at >= ?', message_queued_time)
|
|
|
|
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)
|
|
|
|
|
|
|
|
@messages = @conversation.messages.chat.select(&:reportable?)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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
|
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
|
|
|
|
|
|
|
def reply_email
|
2020-07-19 17:38:07 +00:00
|
|
|
if inbound_email_enabled?
|
2020-07-31 10:27:19 +00:00
|
|
|
"#{assignee_name} <reply+#{@conversation.uuid}@#{current_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
|
2020-07-19 17:38:07 +00:00
|
|
|
if inbound_email_enabled?
|
2020-07-31 10:27:19 +00:00
|
|
|
"#{assignee_name} <#{account_support_email}>"
|
2020-04-30 14:50:26 +00:00
|
|
|
else
|
2020-10-11 17:54:11 +00:00
|
|
|
"#{assignee_name} <#{from_email_address}>"
|
2020-04-30 14:50:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-11 17:54:11 +00:00
|
|
|
def from_email_address
|
|
|
|
return @inbox.email_address if @inbox.email_address
|
|
|
|
|
|
|
|
ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
|
|
|
|
end
|
|
|
|
|
2020-05-22 12:44:18 +00:00
|
|
|
def custom_message_id
|
2020-07-15 11:03:52 +00:00
|
|
|
"<conversation/#{@conversation.uuid}/messages/#{@messages&.last&.id}@#{current_domain}>"
|
2020-05-22 12:44:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def in_reply_to_email
|
|
|
|
"<account/#{@account.id}/conversation/#{@conversation.uuid}@#{current_domain}>"
|
|
|
|
end
|
|
|
|
|
2020-07-19 17:38:07 +00:00
|
|
|
def inbound_email_enabled?
|
|
|
|
@inbound_email_enabled ||= @account.feature_enabled?('inbound_emails') && current_domain.present? && account_support_email.present?
|
2020-05-22 12:44:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def current_domain
|
2020-07-19 10:05:55 +00:00
|
|
|
@current_domain ||= begin
|
|
|
|
@account.domain ||
|
|
|
|
ENV.fetch('MAILER_INBOUND_EMAIL_DOMAIN', false) ||
|
|
|
|
GlobalConfig.get('MAILER_INBOUND_EMAIL_DOMAIN')['MAILER_INBOUND_EMAIL_DOMAIN']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_support_email
|
|
|
|
@account_support_email ||= begin
|
|
|
|
@account.support_email ||
|
|
|
|
GlobalConfig.get('MAILER_SUPPORT_EMAIL')['MAILER_SUPPORT_EMAIL'] ||
|
|
|
|
ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
|
2020-05-22 12:44:18 +00:00
|
|
|
end
|
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
|