chore: Add event for conversations transferred to Bot (#2167)

* chore:  Event for conversation transferred to Bot

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
Co-authored-by: Nithin David <webofnithin@gmail.com>
This commit is contained in:
Sojan Jose 2021-04-28 20:53:23 +05:30 committed by GitHub
parent aa47bad4c1
commit 4b49d21543
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 23 additions and 80 deletions

View file

@ -10,8 +10,7 @@ class ActionCableConnector extends BaseActionCableConnector {
'message.created': this.onMessageCreated,
'message.updated': this.onMessageUpdated,
'conversation.created': this.onConversationCreated,
'conversation.opened': this.onStatusChange,
'conversation.resolved': this.onStatusChange,
'conversation.status_changed': this.onStatusChange,
'user:logout': this.onLogout,
'page:reload': this.onReload,
'assignee.changed': this.onAssigneeChanged,

View file

@ -8,8 +8,7 @@ class ActionCableConnector extends BaseActionCableConnector {
'message.updated': this.onMessageUpdated,
'conversation.typing_on': this.onTypingOn,
'conversation.typing_off': this.onTypingOff,
'conversation.resolved': this.onStatusChange,
'conversation.opened': this.onStatusChange,
'conversation.status_changed': this.onStatusChange,
'presence.update': this.onPresenceUpdate,
};
}

View file

@ -32,25 +32,11 @@ class ActionCableListener < BaseListener
broadcast(account, tokens, MESSAGE_UPDATED, message.push_event_data)
end
def conversation_resolved(event)
def conversation_status_changed(event)
conversation, account = extract_conversation_and_account(event)
tokens = user_tokens(account, conversation.inbox.members) + [conversation.contact&.pubsub_token]
broadcast(account, tokens, CONVERSATION_RESOLVED, conversation.push_event_data)
end
def conversation_opened(event)
conversation, account = extract_conversation_and_account(event)
tokens = user_tokens(account, conversation.inbox.members) + [conversation.contact&.pubsub_token]
broadcast(account, tokens, CONVERSATION_OPENED, conversation.push_event_data)
end
def conversation_lock_toggle(event)
conversation, account = extract_conversation_and_account(event)
tokens = user_tokens(account, conversation.inbox.members)
broadcast(account, tokens, CONVERSATION_LOCK_TOGGLE, conversation.lock_event_data)
broadcast(account, tokens, CONVERSATION_STATUS_CHANGED, conversation.push_event_data)
end
def conversation_typing_on(event)

View file

@ -1,4 +1,5 @@
class WebhookListener < BaseListener
# FIXME: deprecate the opened and resolved events in future in favor of status changed event.
def conversation_resolved(event)
conversation = extract_conversation_and_account(event)[0]
inbox = conversation.inbox
@ -6,6 +7,7 @@ class WebhookListener < BaseListener
deliver_webhook_payloads(payload, inbox)
end
# FIXME: deprecate the opened and resolved events in future in favor of status changed event.
def conversation_opened(event)
conversation = extract_conversation_and_account(event)[0]
inbox = conversation.inbox
@ -13,6 +15,13 @@ class WebhookListener < BaseListener
deliver_webhook_payloads(payload, inbox)
end
def conversation_status_changed(event)
conversation = extract_conversation_and_account(event)[0]
inbox = conversation.inbox
payload = conversation.webhook_data.merge(event: __method__.to_s)
deliver_webhook_payloads(payload, inbox)
end
def message_created(event)
message = extract_message_and_account(event)[0]
inbox = message.inbox

View file

@ -8,7 +8,6 @@
# contact_last_seen_at :datetime
# identifier :string
# last_activity_at :datetime not null
# locked :boolean default(FALSE)
# status :integer default("open"), not null
# uuid :uuid not null
# created_at :datetime not null
@ -104,14 +103,6 @@ class Conversation < ApplicationRecord
Redis::Alfred.get(mute_key).present?
end
def lock!
update!(locked: true)
end
def unlock!
update!(locked: false)
end
def unread_messages
messages.unread_since(agent_last_seen_at)
end
@ -187,8 +178,8 @@ class Conversation < ApplicationRecord
{
CONVERSATION_OPENED => -> { saved_change_to_status? && open? },
CONVERSATION_RESOLVED => -> { saved_change_to_status? && resolved? },
CONVERSATION_STATUS_CHANGED => -> { saved_change_to_status? },
CONVERSATION_READ => -> { saved_change_to_contact_last_seen_at? },
CONVERSATION_LOCK_TOGGLE => -> { saved_change_to_locked? },
CONVERSATION_CONTACT_CHANGED => -> { saved_change_to_contact_id? }
}.each do |event, condition|
condition.call && dispatcher_dispatch(event)

View file

@ -1,8 +1,4 @@
class Conversations::EventDataPresenter < SimpleDelegator
def lock_data
{ id: display_id, locked: locked? }
end
def push_data
{
additional_attributes: additional_attributes,

View file

@ -0,0 +1,5 @@
class MigrationRemoveLockedFromConversation < ActiveRecord::Migration[6.0]
def change
remove_column :conversations, :locked, :boolean
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_03_15_101919) do
ActiveRecord::Schema.define(version: 2021_04_26_191914) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
@ -229,7 +229,6 @@ ActiveRecord::Schema.define(version: 2021_03_15_101919) do
t.integer "display_id", null: false
t.datetime "contact_last_seen_at"
t.datetime "agent_last_seen_at"
t.boolean "locked", default: false
t.jsonb "additional_attributes", default: {}
t.bigint "contact_inbox_id"
t.uuid "uuid", default: -> { "gen_random_uuid()" }, null: false

View file

@ -31,7 +31,7 @@ unless Rails.env.production?
inbox = Inbox.create!(channel: web_widget, account: account, name: 'Acme Support')
InboxMember.create!(user: user, inbox: inbox)
contact = Contact.create!(name: 'jane', email: 'jane@example.com', phone_number: '0000', account: account)
contact = Contact.create!(name: 'jane', email: 'jane@example.com', phone_number: '+2320000', account: account)
contact_inbox = ContactInbox.create!(inbox: inbox, contact: contact, source_id: user.id, hmac_verified: true)
conversation = Conversation.create!(
account: account,

View file

@ -12,9 +12,10 @@ module Events::Types
# conversation events
CONVERSATION_CREATED = 'conversation.created'
CONVERSATION_READ = 'conversation.read'
# FIXME: deprecate the opened and resolved events in future in favor of status changed event.
CONVERSATION_OPENED = 'conversation.opened'
CONVERSATION_RESOLVED = 'conversation.resolved'
CONVERSATION_LOCK_TOGGLE = 'conversation.lock_toggle'
CONVERSATION_STATUS_CHANGED = 'conversation.status_changed'
CONVERSATION_CONTACT_CHANGED = 'conversation.contact_changed'
ASSIGNEE_CHANGED = 'assignee.changed'
TEAM_CHANGED = 'team.changed'

View file

@ -4,7 +4,6 @@ FactoryBot.define do
factory :conversation do
status { 'open' }
agent_last_seen_at { Time.current }
locked { false }
identifier { SecureRandom.hex }
after(:build) do |conversation|

View file

@ -93,7 +93,6 @@ RSpec.describe Conversation, type: :model do
conversation.update(
status: :resolved,
locked: true,
contact_last_seen_at: Time.now,
assignee: new_assignee,
label_list: [label.title]
@ -106,8 +105,6 @@ RSpec.describe Conversation, type: :model do
.with(described_class::CONVERSATION_RESOLVED, kind_of(Time), conversation: conversation)
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
.with(described_class::CONVERSATION_READ, kind_of(Time), conversation: conversation)
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
.with(described_class::CONVERSATION_LOCK_TOGGLE, kind_of(Time), conversation: conversation)
expect(Rails.configuration.dispatcher).to have_received(:dispatch)
.with(described_class::ASSIGNEE_CHANGED, kind_of(Time), conversation: conversation)
end
@ -193,28 +190,6 @@ RSpec.describe Conversation, type: :model do
end
end
describe '#lock!' do
subject(:lock!) { conversation.lock! }
let(:conversation) { create(:conversation) }
it 'assigns locks the conversation' do
expect(lock!).to eq(true)
expect(conversation.reload.locked).to eq(true)
end
end
describe '#unlock!' do
subject(:unlock!) { conversation.unlock! }
let(:conversation) { create(:conversation) }
it 'unlocks the conversation' do
expect(unlock!).to eq(true)
expect(conversation.reload.locked).to eq(false)
end
end
describe '#mute!' do
subject(:mute!) { conversation.mute! }
@ -365,18 +340,6 @@ RSpec.describe Conversation, type: :model do
end
end
describe '#lock_event_data' do
subject(:lock_event_data) { conversation.lock_event_data }
let(:conversation) do
build(:conversation, display_id: 505, locked: false)
end
it 'returns lock event payload' do
expect(lock_event_data).to eq(id: 505, locked: false)
end
end
describe '#botinbox: when conversation created inside inbox with agent bot' do
let!(:bot_inbox) { create(:agent_bot_inbox) }
let(:conversation) { create(:conversation, inbox: bot_inbox.inbox) }

View file

@ -6,10 +6,6 @@ RSpec.describe Conversations::EventDataPresenter do
let(:presenter) { described_class.new(conversation) }
let(:conversation) { create(:conversation) }
describe '#lock_data' do
it { expect(presenter.lock_data).to eq(id: conversation.display_id, locked: false) }
end
describe '#push_data' do
let(:expected_data) do
{