Feature: Conversation creation email notifications (#576)

* Clean up the mailers

* Disable assignment mailer if setting is turned off

* Email notifications on conversation create

* Specs
This commit is contained in:
Sojan Jose
2020-03-01 19:06:13 +05:30
committed by GitHub
parent d6237dfc59
commit cda65ea339
17 changed files with 160 additions and 31 deletions

View File

@@ -5,7 +5,7 @@ class AsyncDispatcher < BaseDispatcher
end
def listeners
listeners = [ReportingListener.instance, WebhookListener.instance]
listeners = [EmailNotificationListener.instance, ReportingListener.instance, WebhookListener.instance]
listeners << SubscriptionListener.instance if ENV['BILLING_ENABLED']
listeners
end

View File

@@ -0,0 +1,10 @@
class EmailNotificationListener < BaseListener
def conversation_created(event)
conversation, _account, _timestamp = extract_conversation_and_account(event)
conversation.inbox.members.each do |agent|
next unless agent.notification_settings.find_by(account_id: conversation.account_id).conversation_creation?
AgentNotifications::ConversationNotificationsMailer.conversation_created(conversation, agent).deliver_later
end
end
end

View File

@@ -0,0 +1,21 @@
class AgentNotifications::ConversationNotificationsMailer < ApplicationMailer
default from: ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
layout 'mailer'
def conversation_created(conversation, agent)
return unless smtp_config_set_or_development?
@agent = agent
@conversation = conversation
subject = "#{@agent.name}, A new conversation [ID - #{@conversation.display_id}] has been created in #{@conversation.inbox&.name}."
mail(to: @agent.email, subject: subject)
end
def conversation_assigned(conversation, agent)
return unless smtp_config_set_or_development?
@agent = agent
@conversation = conversation
mail(to: @agent.email, subject: "#{@agent.name}, A new conversation [ID - #{@conversation.display_id}] has been assigned to you.")
end
end

View File

@@ -1,6 +1,7 @@
class ApplicationMailer < ActionMailer::Base
default from: ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
layout 'mailer'
append_view_path Rails.root.join('app/views/mailers')
# helpers
helper :frontend_urls

View File

@@ -1,12 +0,0 @@
class AssignmentMailer < ApplicationMailer
default from: ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
layout 'mailer'
def conversation_assigned(conversation, agent)
return unless smtp_config_set_or_development?
@agent = agent
@conversation = conversation
mail(to: @agent.email, subject: "#{@agent.name}, A new conversation [ID - #{@conversation.display_id}] has been assigned to you.")
end
end

View File

@@ -1,8 +1,8 @@
class ConversationMailer < ApplicationMailer
class ConversationReplyMailer < ApplicationMailer
default from: ENV.fetch('MAILER_SENDER_EMAIL', 'accounts@chatwoot.com')
layout 'mailer'
def new_message(conversation, message_queued_time)
def reply_with_summary(conversation, message_queued_time)
return unless smtp_config_set_or_development?
@conversation = conversation

View File

@@ -112,8 +112,11 @@ class Conversation < ApplicationRecord
def send_email_notification_to_assignee
return if self_assign?(assignee_id)
return unless saved_change_to_assignee_id?
return if assignee_id.blank?
return if assignee.notification_settings.find_by(account_id: account_id).not_conversation_assignment?
AssignmentMailer.conversation_assigned(self, assignee).deliver_later if saved_change_to_assignee_id? && assignee_id.present?
AgentNotifications::ConversationNotificationsMailer.conversation_assigned(self, assignee).deliver_later
end
def self_assign?(assignee_id)
@@ -156,8 +159,6 @@ class Conversation < ApplicationRecord
end
def run_round_robin
# return unless conversation.account.has_feature?(round_robin)
# return unless conversation.account.round_robin_enabled?
return unless inbox.enable_auto_assignment
return if assignee

View File

@@ -133,7 +133,7 @@ class Message < ApplicationRecord
# Since this is live chat, send the email after few minutes so the only one email with
# last few messages coupled together is sent rather than email for each message
ConversationEmailWorker.perform_in(2.minutes, conversation.id, Time.zone.now)
ConversationReplyEmailWorker.perform_in(2.minutes, conversation.id, Time.zone.now)
end
end
end

View File

@@ -0,0 +1,5 @@
<p>Hi <%= @agent.name %>,</p>
<p>Time to save the world. A new conversation has been created in <%= @conversation.inbox.name %></p>
<p>Click <%= link_to 'here', app_conversation_url(id: @conversation.display_id) %> to get cracking. </p>

View File

@@ -1,4 +1,4 @@
class ConversationEmailWorker
class ConversationReplyEmailWorker
include Sidekiq::Worker
sidekiq_options queue: :mailers
@@ -6,7 +6,7 @@ class ConversationEmailWorker
@conversation = Conversation.find(conversation_id)
# send the email
ConversationMailer.new_message(@conversation, queued_time).deliver_later
ConversationReplyMailer.reply_with_summary(@conversation, queued_time).deliver_later
# delete the redis set from the first new message on the conversation
conversation_mail_key = Redis::Alfred::CONVERSATION_MAILER_KEY % @conversation.id