Chatwoot/app/mailers/conversation_reply_mailer.rb

168 lines
5.5 KiB
Ruby
Raw Normal View History

class ConversationReplyMailer < ApplicationMailer
include ConversationReplyMailerHelper
default from: ENV.fetch('MAILER_SENDER_EMAIL', 'Chatwoot <accounts@chatwoot.com>')
layout :choose_layout
def reply_with_summary(conversation, last_queued_id)
return unless smtp_config_set_or_development?
init_conversation_attributes(conversation)
return if conversation_already_viewed?
recap_messages = @conversation.messages.chat.where('id < ?', last_queued_id).last(10)
new_messages = @conversation.messages.chat.where('id >= ?', last_queued_id)
@messages = recap_messages + new_messages
2021-08-23 16:30:47 +00:00
@messages = @messages.select(&:email_reply_summarizable?)
prepare_mail(true)
end
def reply_without_summary(conversation, last_queued_id)
return unless smtp_config_set_or_development?
init_conversation_attributes(conversation)
return if conversation_already_viewed?
@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? }
return false if @messages.count.zero?
prepare_mail(false)
end
def email_reply(message)
return unless smtp_config_set_or_development?
init_conversation_attributes(message.conversation)
@message = message
reply_mail_object = prepare_mail(true)
message.update(source_id: reply_mail_object.message_id)
end
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?)
mail({
to: to_email,
from: from_email_with_name,
subject: "[##{@conversation.display_id}] #{I18n.t('conversations.reply.transcript_subject')}"
})
end
private
def init_conversation_attributes(conversation)
@conversation = conversation
@account = @conversation.account
@contact = @conversation.contact
@agent = @conversation.assignee
@inbox = @conversation.inbox
@channel = @inbox.channel
end
def should_use_conversation_email_address?
@inbox.inbox_type == 'Email' || inbound_email_enabled?
end
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
def assignee_name
@assignee_name ||= @agent&.available_name || 'Notifications'
end
def mail_subject
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
end
Feature: Conversation Continuity with Email (#770) * Added POC for mail inbox reply email * created mailbox and migratuion for the same * cleaned up sidekiq queues and added the queues for action mailbox and active storage * created conversation mailbox and functionlaity to create a message on the conversation when it's replied * Added UUID to conversation to be used in email replies * added migration to add uuid for conversation * changed parsing and resource fetching to reflect matching uuid and loading conversation alone * cleaned up conversation mailbox.rb * Added content type & attribute for message * Added the new reply email to outgoing emails * Added migration to accounts for adding domain and settings * Modified seeds to reflect this changes * Added the flag based column on account for boolean settings * Added the new reply to email in outgoing conversation emails based on conditions * Added dynamic email routing in application mailbox * Added dynamic email routing in application mailbox * Added a catch all deafult empty mailbox * Added annotation for account * Added the complete email details & attachments to the message * Added the complete email details to the message in content_attributes, like subject, to, cc, bcc etc * Modified the mail extractor to give a serilaized version of email * Handled storing attachments of email on the message * Added incoming email settings, env variables * [#138] Added documentation regarding different email settings and variables * Fixed the mail attachments blob issue (#138) * Decoided attachments were strings and had to construct blobs out fo them to work with active storage * Fixed the content encoding issue with mail body * Fixed issue with Proc used in apllication mailbox routing * Fixed couple of typos and silly mistakes * Set appropriate from email for conversation reply mails (#138) * From email was taken from a env variable, changed it to take from account settings if enabled * Set the reply to email correctly based on conversation UUID * Added commented config ind development.rb for mailbox ingress * Added account settings for domain and support email (#138) * Added the new attributes in accounts controller params whitelisting, api responses * Added options for the the new fields in account settings * Fixed typos in email continuity docs and warnings * Added specs for conversation reply mailer changes (#138) * Added specs for * conversation reply mailer * Accounts controller * Account and Conversation models * Added tests for email presenter (#138) * Specs for inbound email routing and mailboxes (#138)
2020-04-30 14:50:26 +00:00
def reply_email
if should_use_conversation_email_address?
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}")
Feature: Conversation Continuity with Email (#770) * Added POC for mail inbox reply email * created mailbox and migratuion for the same * cleaned up sidekiq queues and added the queues for action mailbox and active storage * created conversation mailbox and functionlaity to create a message on the conversation when it's replied * Added UUID to conversation to be used in email replies * added migration to add uuid for conversation * changed parsing and resource fetching to reflect matching uuid and loading conversation alone * cleaned up conversation mailbox.rb * Added content type & attribute for message * Added the new reply email to outgoing emails * Added migration to accounts for adding domain and settings * Modified seeds to reflect this changes * Added the flag based column on account for boolean settings * Added the new reply to email in outgoing conversation emails based on conditions * Added dynamic email routing in application mailbox * Added dynamic email routing in application mailbox * Added a catch all deafult empty mailbox * Added annotation for account * Added the complete email details & attachments to the message * Added the complete email details to the message in content_attributes, like subject, to, cc, bcc etc * Modified the mail extractor to give a serilaized version of email * Handled storing attachments of email on the message * Added incoming email settings, env variables * [#138] Added documentation regarding different email settings and variables * Fixed the mail attachments blob issue (#138) * Decoided attachments were strings and had to construct blobs out fo them to work with active storage * Fixed the content encoding issue with mail body * Fixed issue with Proc used in apllication mailbox routing * Fixed couple of typos and silly mistakes * Set appropriate from email for conversation reply mails (#138) * From email was taken from a env variable, changed it to take from account settings if enabled * Set the reply to email correctly based on conversation UUID * Added commented config ind development.rb for mailbox ingress * Added account settings for domain and support email (#138) * Added the new attributes in accounts controller params whitelisting, api responses * Added options for the the new fields in account settings * Fixed typos in email continuity docs and warnings * Added specs for conversation reply mailer changes (#138) * Added specs for * conversation reply mailer * Accounts controller * Account and Conversation models * Added tests for email presenter (#138) * Specs for inbound email routing and mailboxes (#138)
2020-04-30 14:50:26 +00:00
else
@inbox.email_address || @agent&.email
Feature: Conversation Continuity with Email (#770) * Added POC for mail inbox reply email * created mailbox and migratuion for the same * cleaned up sidekiq queues and added the queues for action mailbox and active storage * created conversation mailbox and functionlaity to create a message on the conversation when it's replied * Added UUID to conversation to be used in email replies * added migration to add uuid for conversation * changed parsing and resource fetching to reflect matching uuid and loading conversation alone * cleaned up conversation mailbox.rb * Added content type & attribute for message * Added the new reply email to outgoing emails * Added migration to accounts for adding domain and settings * Modified seeds to reflect this changes * Added the flag based column on account for boolean settings * Added the new reply to email in outgoing conversation emails based on conditions * Added dynamic email routing in application mailbox * Added dynamic email routing in application mailbox * Added a catch all deafult empty mailbox * Added annotation for account * Added the complete email details & attachments to the message * Added the complete email details to the message in content_attributes, like subject, to, cc, bcc etc * Modified the mail extractor to give a serilaized version of email * Handled storing attachments of email on the message * Added incoming email settings, env variables * [#138] Added documentation regarding different email settings and variables * Fixed the mail attachments blob issue (#138) * Decoided attachments were strings and had to construct blobs out fo them to work with active storage * Fixed the content encoding issue with mail body * Fixed issue with Proc used in apllication mailbox routing * Fixed couple of typos and silly mistakes * Set appropriate from email for conversation reply mails (#138) * From email was taken from a env variable, changed it to take from account settings if enabled * Set the reply to email correctly based on conversation UUID * Added commented config ind development.rb for mailbox ingress * Added account settings for domain and support email (#138) * Added the new attributes in accounts controller params whitelisting, api responses * Added options for the the new fields in account settings * Fixed typos in email continuity docs and warnings * Added specs for conversation reply mailer changes (#138) * Added specs for * conversation reply mailer * Accounts controller * Account and Conversation models * Added tests for email presenter (#138) * Specs for inbound email routing and mailboxes (#138)
2020-04-30 14:50:26 +00:00
end
end
def from_email_with_name
if should_use_conversation_email_address?
I18n.t('conversations.reply.email.header.from_with_name', assignee_name: assignee_name, inbox_name: @inbox.name,
from_email: parse_email(@account.support_email))
Feature: Conversation Continuity with Email (#770) * Added POC for mail inbox reply email * created mailbox and migratuion for the same * cleaned up sidekiq queues and added the queues for action mailbox and active storage * created conversation mailbox and functionlaity to create a message on the conversation when it's replied * Added UUID to conversation to be used in email replies * added migration to add uuid for conversation * changed parsing and resource fetching to reflect matching uuid and loading conversation alone * cleaned up conversation mailbox.rb * Added content type & attribute for message * Added the new reply email to outgoing emails * Added migration to accounts for adding domain and settings * Modified seeds to reflect this changes * Added the flag based column on account for boolean settings * Added the new reply to email in outgoing conversation emails based on conditions * Added dynamic email routing in application mailbox * Added dynamic email routing in application mailbox * Added a catch all deafult empty mailbox * Added annotation for account * Added the complete email details & attachments to the message * Added the complete email details to the message in content_attributes, like subject, to, cc, bcc etc * Modified the mail extractor to give a serilaized version of email * Handled storing attachments of email on the message * Added incoming email settings, env variables * [#138] Added documentation regarding different email settings and variables * Fixed the mail attachments blob issue (#138) * Decoided attachments were strings and had to construct blobs out fo them to work with active storage * Fixed the content encoding issue with mail body * Fixed issue with Proc used in apllication mailbox routing * Fixed couple of typos and silly mistakes * Set appropriate from email for conversation reply mails (#138) * From email was taken from a env variable, changed it to take from account settings if enabled * Set the reply to email correctly based on conversation UUID * Added commented config ind development.rb for mailbox ingress * Added account settings for domain and support email (#138) * Added the new attributes in accounts controller params whitelisting, api responses * Added options for the the new fields in account settings * Fixed typos in email continuity docs and warnings * Added specs for conversation reply mailer changes (#138) * Added specs for * conversation reply mailer * Accounts controller * Account and Conversation models * Added tests for email presenter (#138) * Specs for inbound email routing and mailboxes (#138)
2020-04-30 14:50:26 +00:00
else
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))
Feature: Conversation Continuity with Email (#770) * Added POC for mail inbox reply email * created mailbox and migratuion for the same * cleaned up sidekiq queues and added the queues for action mailbox and active storage * created conversation mailbox and functionlaity to create a message on the conversation when it's replied * Added UUID to conversation to be used in email replies * added migration to add uuid for conversation * changed parsing and resource fetching to reflect matching uuid and loading conversation alone * cleaned up conversation mailbox.rb * Added content type & attribute for message * Added the new reply email to outgoing emails * Added migration to accounts for adding domain and settings * Modified seeds to reflect this changes * Added the flag based column on account for boolean settings * Added the new reply to email in outgoing conversation emails based on conditions * Added dynamic email routing in application mailbox * Added dynamic email routing in application mailbox * Added a catch all deafult empty mailbox * Added annotation for account * Added the complete email details & attachments to the message * Added the complete email details to the message in content_attributes, like subject, to, cc, bcc etc * Modified the mail extractor to give a serilaized version of email * Handled storing attachments of email on the message * Added incoming email settings, env variables * [#138] Added documentation regarding different email settings and variables * Fixed the mail attachments blob issue (#138) * Decoided attachments were strings and had to construct blobs out fo them to work with active storage * Fixed the content encoding issue with mail body * Fixed issue with Proc used in apllication mailbox routing * Fixed couple of typos and silly mistakes * Set appropriate from email for conversation reply mails (#138) * From email was taken from a env variable, changed it to take from account settings if enabled * Set the reply to email correctly based on conversation UUID * Added commented config ind development.rb for mailbox ingress * Added account settings for domain and support email (#138) * Added the new attributes in accounts controller params whitelisting, api responses * Added options for the the new fields in account settings * Fixed typos in email continuity docs and warnings * Added specs for conversation reply mailer changes (#138) * Added specs for * conversation reply mailer * Accounts controller * Account and Conversation models * Added tests for email presenter (#138) * Specs for inbound email routing and mailboxes (#138)
2020-04-30 14:50:26 +00:00
end
end
def parse_email(email_string)
Mail::Address.new(email_string).address
end
def inbox_from_email_address
return @inbox.email_address if @inbox.email_address
@account.support_email
end
def custom_message_id
last_message = @message || @messages&.last
"<conversation/#{@conversation.uuid}/messages/#{last_message&.id}@#{@account.inbound_email_domain}>"
end
def in_reply_to_email
conversation_reply_email_id || "<account/#{@account.id}/conversation/#{@conversation.uuid}@#{@account.inbound_email_domain}>"
end
def conversation_reply_email_id
content_attributes = @conversation.messages.incoming.last&.content_attributes
if content_attributes && content_attributes['email'] && content_attributes['email']['message_id']
return "<#{content_attributes['email']['message_id']}>"
end
nil
end
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
def inbound_email_enabled?
@inbound_email_enabled ||= @account.feature_enabled?('inbound_emails') && @account.inbound_email_domain
.present? && @account.support_email.present?
Feature: Conversation Continuity with Email (#770) * Added POC for mail inbox reply email * created mailbox and migratuion for the same * cleaned up sidekiq queues and added the queues for action mailbox and active storage * created conversation mailbox and functionlaity to create a message on the conversation when it's replied * Added UUID to conversation to be used in email replies * added migration to add uuid for conversation * changed parsing and resource fetching to reflect matching uuid and loading conversation alone * cleaned up conversation mailbox.rb * Added content type & attribute for message * Added the new reply email to outgoing emails * Added migration to accounts for adding domain and settings * Modified seeds to reflect this changes * Added the flag based column on account for boolean settings * Added the new reply to email in outgoing conversation emails based on conditions * Added dynamic email routing in application mailbox * Added dynamic email routing in application mailbox * Added a catch all deafult empty mailbox * Added annotation for account * Added the complete email details & attachments to the message * Added the complete email details to the message in content_attributes, like subject, to, cc, bcc etc * Modified the mail extractor to give a serilaized version of email * Handled storing attachments of email on the message * Added incoming email settings, env variables * [#138] Added documentation regarding different email settings and variables * Fixed the mail attachments blob issue (#138) * Decoided attachments were strings and had to construct blobs out fo them to work with active storage * Fixed the content encoding issue with mail body * Fixed issue with Proc used in apllication mailbox routing * Fixed couple of typos and silly mistakes * Set appropriate from email for conversation reply mails (#138) * From email was taken from a env variable, changed it to take from account settings if enabled * Set the reply to email correctly based on conversation UUID * Added commented config ind development.rb for mailbox ingress * Added account settings for domain and support email (#138) * Added the new attributes in accounts controller params whitelisting, api responses * Added options for the the new fields in account settings * Fixed typos in email continuity docs and warnings * Added specs for conversation reply mailer changes (#138) * Added specs for * conversation reply mailer * Accounts controller * Account and Conversation models * Added tests for email presenter (#138) * Specs for inbound email routing and mailboxes (#138)
2020-04-30 14:50:26 +00:00
end
def choose_layout
return false if action_name == 'reply_without_summary' || action_name == 'email_reply'
'mailer/base'
end
end