From 615a575bdd3eeb346169d0c432107380dc140a24 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Mon, 18 Apr 2022 19:05:45 +0530 Subject: [PATCH] fix: Agent typing indicator for website widget (#4495) When we migrated the pubsub tokens from contact to contact inboxes, we missed out on doing this update for the typing indicator events. Hence the agent typing events weren't visible on the widget side. This change fixes that and removes the necessary column contact pubsub token from the model. fixes: #4476 --- app/jobs/data_import_job.rb | 3 --- app/listeners/action_cable_listener.rb | 3 ++- app/models/contact.rb | 4 ---- ...20418094715_remove_contact_pubsub_token.rb | 5 ++++ db/schema.rb | 4 +--- spec/jobs/data_import_job_spec.rb | 3 --- spec/listeners/action_cable_listener_spec.rb | 24 +++++++++++++++++-- 7 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 db/migrate/20220418094715_remove_contact_pubsub_token.rb diff --git a/app/jobs/data_import_job.rb b/app/jobs/data_import_job.rb index 7c285eb8c..24610acec 100644 --- a/app/jobs/data_import_job.rb +++ b/app/jobs/data_import_job.rb @@ -21,9 +21,6 @@ class DataImportJob < ApplicationJob contact.name = params[:name] if params[:name].present? contact.assign_attributes(custom_attributes: contact.custom_attributes.merge(params.except(:identifier, :email, :name))) - - # since callbacks aren't triggered lets ensure a pubsub token - contact.pubsub_token ||= SecureRandom.base58(24) contact end diff --git a/app/listeners/action_cable_listener.rb b/app/listeners/action_cable_listener.rb index 815103c33..d9099eda8 100644 --- a/app/listeners/action_cable_listener.rb +++ b/app/listeners/action_cable_listener.rb @@ -135,7 +135,8 @@ class ActionCableListener < BaseListener private def typing_event_listener_tokens(account, conversation, user) - (user_tokens(account, conversation.inbox.members) + [conversation.contact.pubsub_token]) - [user&.pubsub_token] + current_user_token = user.is_a?(Contact) ? conversation.contact_inbox.pubsub_token : user.pubsub_token + (user_tokens(account, conversation.inbox.members) + [conversation.contact_inbox.pubsub_token]) - [current_user_token] end def user_tokens(account, agents) diff --git a/app/models/contact.rb b/app/models/contact.rb index 106930db4..2261d4230 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -10,7 +10,6 @@ # last_activity_at :datetime # name :string # phone_number :string -# pubsub_token :string # created_at :datetime not null # updated_at :datetime not null # account_id :integer not null @@ -19,13 +18,11 @@ # # index_contacts_on_account_id (account_id) # index_contacts_on_phone_number_and_account_id (phone_number,account_id) -# index_contacts_on_pubsub_token (pubsub_token) UNIQUE # uniq_email_per_account_contact (email,account_id) UNIQUE # uniq_identifier_per_account_contact (identifier,account_id) UNIQUE # class Contact < ApplicationRecord - # TODO: remove the pubsub_token attribute from this model in future. include Avatarable include AvailabilityStatusable include Labelable @@ -117,7 +114,6 @@ class Contact < ApplicationRecord identifier: identifier, name: name, phone_number: phone_number, - pubsub_token: pubsub_token, thumbnail: avatar_url, type: 'contact' } diff --git a/db/migrate/20220418094715_remove_contact_pubsub_token.rb b/db/migrate/20220418094715_remove_contact_pubsub_token.rb new file mode 100644 index 000000000..e625b9046 --- /dev/null +++ b/db/migrate/20220418094715_remove_contact_pubsub_token.rb @@ -0,0 +1,5 @@ +class RemoveContactPubsubToken < ActiveRecord::Migration[6.1] + def change + remove_column :contacts, :pubsub_token, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 93b96cec7..8373fe31f 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: 2022_04_09_044943) do +ActiveRecord::Schema.define(version: 2022_04_18_094715) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" @@ -325,7 +325,6 @@ ActiveRecord::Schema.define(version: 2022_04_09_044943) do t.integer "account_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "pubsub_token" t.jsonb "additional_attributes", default: {} t.string "identifier" t.jsonb "custom_attributes", default: {} @@ -334,7 +333,6 @@ ActiveRecord::Schema.define(version: 2022_04_09_044943) do t.index ["email", "account_id"], name: "uniq_email_per_account_contact", unique: true t.index ["identifier", "account_id"], name: "uniq_identifier_per_account_contact", unique: true t.index ["phone_number", "account_id"], name: "index_contacts_on_phone_number_and_account_id" - t.index ["pubsub_token"], name: "index_contacts_on_pubsub_token", unique: true end create_table "conversations", id: :serial, force: :cascade do |t| diff --git a/spec/jobs/data_import_job_spec.rb b/spec/jobs/data_import_job_spec.rb index ad5e7b597..712f3283d 100644 --- a/spec/jobs/data_import_job_spec.rb +++ b/spec/jobs/data_import_job_spec.rb @@ -17,8 +17,5 @@ RSpec.describe DataImportJob, type: :job do expect(data_import.account.contacts.count).to eq(csv_length) expect(data_import.reload.total_records).to eq(csv_length) expect(data_import.reload.processed_records).to eq(csv_length) - - # should generate pubsub tokens for contacts - expect(data_import.account.contacts.last.pubsub_token).present? end end diff --git a/spec/listeners/action_cable_listener_spec.rb b/spec/listeners/action_cable_listener_spec.rb index b3266abc8..d1bad487f 100644 --- a/spec/listeners/action_cable_listener_spec.rb +++ b/spec/listeners/action_cable_listener_spec.rb @@ -61,7 +61,7 @@ describe ActionCableListener do expect(conversation.inbox.reload.inbox_members.count).to eq(1) expect(ActionCableBroadcastJob).to receive(:perform_later).with( a_collection_containing_exactly( - admin.pubsub_token, conversation.contact.pubsub_token + admin.pubsub_token, conversation.contact_inbox.pubsub_token ), 'conversation.typing_on', conversation: conversation.push_event_data, user: agent.push_event_data, @@ -72,6 +72,26 @@ describe ActionCableListener do end end + describe '#typing_on with contact' do + let(:event_name) { :'conversation.typing_on' } + let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: conversation.contact, is_private: false) } + + it 'sends message to account admins, inbox agents and the contact' do + # HACK: to reload conversation inbox members + expect(conversation.inbox.reload.inbox_members.count).to eq(1) + expect(ActionCableBroadcastJob).to receive(:perform_later).with( + a_collection_containing_exactly( + admin.pubsub_token, agent.pubsub_token + ), + 'conversation.typing_on', conversation: conversation.push_event_data, + user: conversation.contact.push_event_data, + account_id: account.id, + is_private: false + ) + listener.conversation_typing_on(event) + end + end + describe '#typing_off' do let(:event_name) { :'conversation.typing_off' } let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: agent, is_private: false) } @@ -81,7 +101,7 @@ describe ActionCableListener do expect(conversation.inbox.reload.inbox_members.count).to eq(1) expect(ActionCableBroadcastJob).to receive(:perform_later).with( a_collection_containing_exactly( - admin.pubsub_token, conversation.contact.pubsub_token + admin.pubsub_token, conversation.contact_inbox.pubsub_token ), 'conversation.typing_off', conversation: conversation.push_event_data, user: agent.push_event_data,