From 4b49d21543f4fa22b0cc0c686ea2da7d4d3299bb Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Wed, 28 Apr 2021 20:53:23 +0530 Subject: [PATCH] chore: Add event for conversations transferred to Bot (#2167) * chore: Event for conversation transferred to Bot Co-authored-by: Muhsin Keloth Co-authored-by: Nithin David --- .../dashboard/helper/actionCable.js | 3 +- app/javascript/widget/helpers/actionCable.js | 3 +- app/listeners/action_cable_listener.rb | 18 +-------- app/listeners/webhook_listener.rb | 9 +++++ app/models/conversation.rb | 11 +----- .../conversations/event_data_presenter.rb | 4 -- ...gration_remove_locked_from_conversation.rb | 5 +++ db/schema.rb | 3 +- db/seeds.rb | 2 +- lib/events/types.rb | 3 +- spec/factories/conversations.rb | 1 - spec/models/conversation_spec.rb | 37 ------------------- .../event_data_presenter_spec.rb | 4 -- 13 files changed, 23 insertions(+), 80 deletions(-) create mode 100644 db/migrate/20210426191914_migration_remove_locked_from_conversation.rb diff --git a/app/javascript/dashboard/helper/actionCable.js b/app/javascript/dashboard/helper/actionCable.js index 73b3e7213..6103ee915 100644 --- a/app/javascript/dashboard/helper/actionCable.js +++ b/app/javascript/dashboard/helper/actionCable.js @@ -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, diff --git a/app/javascript/widget/helpers/actionCable.js b/app/javascript/widget/helpers/actionCable.js index 6cb760b15..8ac4919d3 100644 --- a/app/javascript/widget/helpers/actionCable.js +++ b/app/javascript/widget/helpers/actionCable.js @@ -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, }; } diff --git a/app/listeners/action_cable_listener.rb b/app/listeners/action_cable_listener.rb index 8ce2461a8..3efae0b30 100644 --- a/app/listeners/action_cable_listener.rb +++ b/app/listeners/action_cable_listener.rb @@ -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) diff --git a/app/listeners/webhook_listener.rb b/app/listeners/webhook_listener.rb index a463fea00..dd270eb32 100644 --- a/app/listeners/webhook_listener.rb +++ b/app/listeners/webhook_listener.rb @@ -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 diff --git a/app/models/conversation.rb b/app/models/conversation.rb index bb210f6bc..40272846c 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -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) diff --git a/app/presenters/conversations/event_data_presenter.rb b/app/presenters/conversations/event_data_presenter.rb index 1e3ff66ae..57c96558e 100644 --- a/app/presenters/conversations/event_data_presenter.rb +++ b/app/presenters/conversations/event_data_presenter.rb @@ -1,8 +1,4 @@ class Conversations::EventDataPresenter < SimpleDelegator - def lock_data - { id: display_id, locked: locked? } - end - def push_data { additional_attributes: additional_attributes, diff --git a/db/migrate/20210426191914_migration_remove_locked_from_conversation.rb b/db/migrate/20210426191914_migration_remove_locked_from_conversation.rb new file mode 100644 index 000000000..402935f28 --- /dev/null +++ b/db/migrate/20210426191914_migration_remove_locked_from_conversation.rb @@ -0,0 +1,5 @@ +class MigrationRemoveLockedFromConversation < ActiveRecord::Migration[6.0] + def change + remove_column :conversations, :locked, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 13c8a69a1..a4af968d3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index 49227aef9..115a27108 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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, diff --git a/lib/events/types.rb b/lib/events/types.rb index d63255379..c8c420ffe 100644 --- a/lib/events/types.rb +++ b/lib/events/types.rb @@ -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' diff --git a/spec/factories/conversations.rb b/spec/factories/conversations.rb index f09fa6e11..6c7054a51 100644 --- a/spec/factories/conversations.rb +++ b/spec/factories/conversations.rb @@ -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| diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index f6c6f8f63..065cc9999 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -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) } diff --git a/spec/presenters/conversations/event_data_presenter_spec.rb b/spec/presenters/conversations/event_data_presenter_spec.rb index e79ba41ab..2e04e8e75 100644 --- a/spec/presenters/conversations/event_data_presenter_spec.rb +++ b/spec/presenters/conversations/event_data_presenter_spec.rb @@ -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 {