fix: Move contact events to account stream rather than individual user stream (#11082)

This commit is contained in:
Pranav
2025-03-13 17:46:48 -07:00
committed by GitHub
parent a8001ccabc
commit 325dc4a741
4 changed files with 28 additions and 22 deletions

View File

@@ -2,10 +2,9 @@ class RoomChannel < ApplicationCable::Channel
def subscribed def subscribed
# TODO: should we only do ensure stream if current account is present? # TODO: should we only do ensure stream if current account is present?
# for now going ahead with guard clauses in update_subscription and broadcast_presence # for now going ahead with guard clauses in update_subscription and broadcast_presence
ensure_stream
current_user current_user
current_account current_account
ensure_stream
update_subscription update_subscription
broadcast_presence broadcast_presence
end end
@@ -22,12 +21,12 @@ class RoomChannel < ApplicationCable::Channel
data = { account_id: @current_account.id, users: ::OnlineStatusTracker.get_available_users(@current_account.id) } data = { account_id: @current_account.id, users: ::OnlineStatusTracker.get_available_users(@current_account.id) }
data[:contacts] = ::OnlineStatusTracker.get_available_contacts(@current_account.id) if @current_user.is_a? User data[:contacts] = ::OnlineStatusTracker.get_available_contacts(@current_account.id) if @current_user.is_a? User
ActionCable.server.broadcast(@pubsub_token, { event: 'presence.update', data: data }) ActionCable.server.broadcast(pubsub_token, { event: 'presence.update', data: data })
end end
def ensure_stream def ensure_stream
@pubsub_token = params[:pubsub_token] stream_from pubsub_token
stream_from @pubsub_token stream_from "account_#{@current_account.id}" if @current_account.present? && @current_user.is_a?(User)
end end
def update_subscription def update_subscription
@@ -36,11 +35,15 @@ class RoomChannel < ApplicationCable::Channel
::OnlineStatusTracker.update_presence(@current_account.id, @current_user.class.name, @current_user.id) ::OnlineStatusTracker.update_presence(@current_account.id, @current_user.class.name, @current_user.id)
end end
def pubsub_token
@pubsub_token ||= params[:pubsub_token]
end
def current_user def current_user
@current_user ||= if params[:user_id].blank? @current_user ||= if params[:user_id].blank?
ContactInbox.find_by!(pubsub_token: @pubsub_token).contact ContactInbox.find_by!(pubsub_token: pubsub_token).contact
else else
User.find_by!(pubsub_token: @pubsub_token, id: params[:user_id]) User.find_by!(pubsub_token: pubsub_token, id: params[:user_id])
end end
end end

View File

@@ -137,30 +137,22 @@ class ActionCableListener < BaseListener
def contact_created(event) def contact_created(event)
contact, account = extract_contact_and_account(event) contact, account = extract_contact_and_account(event)
tokens = user_tokens(account, account.agents) broadcast(account, [account_token(account)], CONTACT_CREATED, contact.push_event_data)
broadcast(account, tokens, CONTACT_CREATED, contact.push_event_data)
end end
def contact_updated(event) def contact_updated(event)
contact, account = extract_contact_and_account(event) contact, account = extract_contact_and_account(event)
tokens = user_tokens(account, account.agents) broadcast(account, [account_token(account)], CONTACT_UPDATED, contact.push_event_data)
broadcast(account, tokens, CONTACT_UPDATED, contact.push_event_data)
end end
def contact_merged(event) def contact_merged(event)
contact, account = extract_contact_and_account(event) contact, account = extract_contact_and_account(event)
tokens = event.data[:tokens] broadcast(account, [account_token(account)], CONTACT_MERGED, contact.push_event_data)
broadcast(account, tokens, CONTACT_MERGED, contact.push_event_data)
end end
def contact_deleted(event) def contact_deleted(event)
contact, account = extract_contact_and_account(event) contact, account = extract_contact_and_account(event)
tokens = user_tokens(account, account.agents) broadcast(account, [account_token(account)], CONTACT_DELETED, contact.push_event_data)
broadcast(account, tokens, CONTACT_DELETED, contact.push_event_data)
end end
def conversation_mentioned(event) def conversation_mentioned(event)
@@ -172,6 +164,10 @@ class ActionCableListener < BaseListener
private private
def account_token(account)
"account_#{account.id}"
end
def typing_event_listener_tokens(account, conversation, user) def typing_event_listener_tokens(account, conversation, user)
current_user_token = user.is_a?(Contact) ? conversation.contact_inbox.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] (user_tokens(account, conversation.inbox.members) + [conversation.contact_inbox.pubsub_token]) - [current_user_token]

View File

@@ -2,6 +2,8 @@ require 'rails_helper'
RSpec.describe RoomChannel do RSpec.describe RoomChannel do
let!(:contact_inbox) { create(:contact_inbox) } let!(:contact_inbox) { create(:contact_inbox) }
let!(:account) { create(:account) }
let!(:user) { create(:user, account: account) }
before do before do
stub_connection stub_connection
@@ -12,4 +14,11 @@ RSpec.describe RoomChannel do
expect(subscription).to be_confirmed expect(subscription).to be_confirmed
expect(subscription).to have_stream_for(contact_inbox.pubsub_token) expect(subscription).to have_stream_for(contact_inbox.pubsub_token)
end end
it 'subscribes to a stream when pubsub_token is provided for user' do
subscribe(user_id: user.id, pubsub_token: user.pubsub_token, account_id: account.id)
expect(subscription).to be_confirmed
expect(subscription).to have_stream_for(user.pubsub_token)
expect(subscription).to have_stream_for("account_#{account.id}")
end
end end

View File

@@ -121,9 +121,7 @@ describe ActionCableListener do
it 'sends message to account admins, inbox agents' do it 'sends message to account admins, inbox agents' do
expect(ActionCableBroadcastJob).to receive(:perform_later).with( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
a_collection_containing_exactly( ["account_#{account.id}"],
agent.pubsub_token, admin.pubsub_token
),
'contact.deleted', 'contact.deleted',
contact.push_event_data.merge(account_id: account.id) contact.push_event_data.merge(account_id: account.id)
) )