Compare commits

...
Author SHA1 Message Date
Pranav 4e919256e6 Update logic 2025-03-13 20:15:37 -07:00
Pranav 8ae7c15301 fix: Use inbox stream for conversation events instead of individual user stream 2025-03-13 18:08:39 -07:00
3 changed files with 121 additions and 133 deletions
+11 -2
View File
@@ -4,7 +4,7 @@ class RoomChannel < ApplicationCable::Channel
# 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
current_user current_user
current_account current_account
ensure_stream prepare_stream
update_subscription update_subscription
broadcast_presence broadcast_presence
end end
@@ -24,9 +24,18 @@ class RoomChannel < ApplicationCable::Channel
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 prepare_stream
stream_from pubsub_token stream_from pubsub_token
stream_from "account_#{@current_account.id}" if @current_account.present? && @current_user.is_a?(User) stream_from "account_#{@current_account.id}" if @current_account.present? && @current_user.is_a?(User)
stream_from_inboxes
end
def stream_from_inboxes
return if current_user.blank?
current_user.assigned_inboxes.each do |inbox|
stream_from "inbox_#{inbox.id}"
end
end end
def update_subscription def update_subscription
+71 -91
View File
@@ -23,136 +23,76 @@ class ActionCableListener < BaseListener
def account_cache_invalidated(event) def account_cache_invalidated(event)
account = event.data[:account] account = event.data[:account]
tokens = user_tokens(account, account.agents) broadcast(account, account_stream(account), ACCOUNT_CACHE_INVALIDATED, { cache_keys: event.data[:cache_keys] })
broadcast(account, tokens, ACCOUNT_CACHE_INVALIDATED, {
cache_keys: event.data[:cache_keys]
})
end end
def message_created(event) def message_created(event)
message, account = extract_message_and_account(event) broadcast_message_event(MESSAGE_CREATED, event)
conversation = message.conversation broadcast_message_event_to_contact(MESSAGE_CREATED, event)
tokens = user_tokens(account, conversation.inbox.members) + contact_tokens(conversation.contact_inbox, message)
broadcast(account, tokens, MESSAGE_CREATED, message.push_event_data)
end end
def message_updated(event) def message_updated(event)
message, account = extract_message_and_account(event) broadcast_message_event(MESSAGE_UPDATED, event)
conversation = message.conversation broadcast_message_event_to_contact(MESSAGE_UPDATED, event)
tokens = user_tokens(account, conversation.inbox.members) + contact_tokens(conversation.contact_inbox, message)
broadcast(account, tokens, MESSAGE_UPDATED, message.push_event_data.merge(previous_changes: event.data[:previous_changes]))
end end
def first_reply_created(event) def first_reply_created(event)
message, account = extract_message_and_account(event) broadcast_message_event(FIRST_REPLY_CREATED, event)
conversation = message.conversation
tokens = user_tokens(account, conversation.inbox.members)
broadcast(account, tokens, FIRST_REPLY_CREATED, message.push_event_data)
end end
def conversation_created(event) def conversation_created(event)
conversation, account = extract_conversation_and_account(event) broadcast_conversation_event_to_contact(CONVERSATION_CREATED, event)
tokens = user_tokens(account, conversation.inbox.members) + contact_inbox_tokens(conversation.contact_inbox) broadcast_conversation_event(CONVERSATION_CREATED, event)
broadcast(account, tokens, CONVERSATION_CREATED, conversation.push_event_data)
end end
def conversation_read(event) def conversation_read(event)
conversation, account = extract_conversation_and_account(event) broadcast_conversation_event(CONVERSATION_READ, event)
tokens = user_tokens(account, conversation.inbox.members)
broadcast(account, tokens, CONVERSATION_READ, conversation.push_event_data)
end end
def conversation_status_changed(event) def conversation_status_changed(event)
conversation, account = extract_conversation_and_account(event) broadcast_conversation_event_to_contact(CONVERSATION_STATUS_CHANGED, event)
tokens = user_tokens(account, conversation.inbox.members) + contact_inbox_tokens(conversation.contact_inbox) broadcast_conversation_event(CONVERSATION_STATUS_CHANGED, event)
broadcast(account, tokens, CONVERSATION_STATUS_CHANGED, conversation.push_event_data)
end end
def conversation_updated(event) def conversation_updated(event)
conversation, account = extract_conversation_and_account(event) broadcast_conversation_event_to_contact(CONVERSATION_UPDATED, event)
tokens = user_tokens(account, conversation.inbox.members) + contact_inbox_tokens(conversation.contact_inbox) broadcast_conversation_event(CONVERSATION_UPDATED, event)
broadcast(account, tokens, CONVERSATION_UPDATED, conversation.push_event_data)
end end
def conversation_typing_on(event) def conversation_typing_on(event)
conversation = event.data[:conversation] broadcast_typing_event(CONVERSATION_TYPING_ON, event.data)
account = conversation.account
user = event.data[:user]
tokens = typing_event_listener_tokens(account, conversation, user)
broadcast(
account,
tokens,
CONVERSATION_TYPING_ON,
conversation: conversation.push_event_data,
user: user.push_event_data,
is_private: event.data[:is_private] || false
)
end end
def conversation_typing_off(event) def conversation_typing_off(event)
conversation = event.data[:conversation] broadcast_typing_event(CONVERSATION_TYPING_OFF, event.data)
account = conversation.account
user = event.data[:user]
tokens = typing_event_listener_tokens(account, conversation, user)
broadcast(
account,
tokens,
CONVERSATION_TYPING_OFF,
conversation: conversation.push_event_data,
user: user.push_event_data,
is_private: event.data[:is_private] || false
)
end end
def assignee_changed(event) def assignee_changed(event)
conversation, account = extract_conversation_and_account(event) broadcast_conversation_event(ASSIGNEE_CHANGED, event)
tokens = user_tokens(account, conversation.inbox.members)
broadcast(account, tokens, ASSIGNEE_CHANGED, conversation.push_event_data)
end end
def team_changed(event) def team_changed(event)
conversation, account = extract_conversation_and_account(event) broadcast_conversation_event(TEAM_CHANGED, event)
tokens = user_tokens(account, conversation.inbox.members)
broadcast(account, tokens, TEAM_CHANGED, conversation.push_event_data)
end end
def conversation_contact_changed(event) def conversation_contact_changed(event)
conversation, account = extract_conversation_and_account(event) broadcast_conversation_event(CONVERSATION_CONTACT_CHANGED, event)
tokens = user_tokens(account, conversation.inbox.members)
broadcast(account, tokens, CONVERSATION_CONTACT_CHANGED, conversation.push_event_data)
end end
def contact_created(event) def contact_created(event)
contact, account = extract_contact_and_account(event) broadcast_contact_event(CONTACT_CREATED, event)
broadcast(account, [account_token(account)], CONTACT_CREATED, contact.push_event_data)
end end
def contact_updated(event) def contact_updated(event)
contact, account = extract_contact_and_account(event) broadcast_contact_event(CONTACT_UPDATED, event)
broadcast(account, [account_token(account)], CONTACT_UPDATED, contact.push_event_data)
end end
def contact_merged(event) def contact_merged(event)
contact, account = extract_contact_and_account(event) broadcast_contact_event(CONTACT_MERGED, event)
broadcast(account, [account_token(account)], CONTACT_MERGED, contact.push_event_data)
end end
def contact_deleted(event) def contact_deleted(event)
contact, account = extract_contact_and_account(event) broadcast_contact_event(CONTACT_DELETED, event)
broadcast(account, [account_token(account)], CONTACT_DELETED, contact.push_event_data)
end end
def conversation_mentioned(event) def conversation_mentioned(event)
@@ -164,19 +104,59 @@ class ActionCableListener < BaseListener
private private
def account_token(account) def account_stream(account)
"account_#{account.id}" ["account_#{account.id}"]
end end
def typing_event_listener_tokens(account, conversation, user) def inbox_stream(inbox)
current_user_token = user.is_a?(Contact) ? conversation.contact_inbox.pubsub_token : user.pubsub_token ["inbox_#{inbox.id}"]
(user_tokens(account, conversation.inbox.members) + [conversation.contact_inbox.pubsub_token]) - [current_user_token]
end end
def user_tokens(account, agents) def broadcast_contact_event(event_name, event_data)
agent_tokens = agents.pluck(:pubsub_token) contact, account = extract_contact_and_account(event_data)
admin_tokens = account.administrators.pluck(:pubsub_token) broadcast(account, account_stream(account), event_name, contact.push_event_data)
(agent_tokens + admin_tokens).uniq end
def broadcast_conversation_event(event_name, event_data)
conversation, account = extract_conversation_and_account(event_data)
broadcast(account, inbox_stream(conversation.inbox), event_name, conversation.push_event_data)
end
def broadcast_message_event(event_name, event_data)
message, account = extract_message_and_account(event_data)
broadcast(account, inbox_stream(message.inbox), event_name, message.push_event_data)
end
def broadcast_conversation_event_to_contact(event_name, event_data)
conversation, account = extract_conversation_and_account(event_data)
tokens = contact_inbox_tokens(conversation.contact_inbox)
broadcast(account, tokens, event_name, conversation.push_event_data)
end
def broadcast_message_event_to_contact(event_name, event_data)
message, account = extract_message_and_account(event_data)
tokens = contact_tokens(message.conversation.contact_inbox, message)
broadcast(account, tokens, event_name, message.push_event_data)
end
def broadcast_typing_event(event_name, event_data)
conversation = event_data[:conversation]
account = conversation.account
user = event_data[:user]
tokens = typing_event_listener_tokens(conversation)
broadcast(
account,
tokens,
event_name,
conversation: conversation.push_event_data,
user: user.push_event_data,
is_private: event_data[:is_private] || false
)
end
def typing_event_listener_tokens(conversation)
["inbox_#{conversation.inbox.id}"] + [conversation.contact_inbox.pubsub_token]
end end
def contact_tokens(contact_inbox, message) def contact_tokens(contact_inbox, message)
+39 -40
View File
@@ -2,7 +2,7 @@ require 'rails_helper'
describe ActionCableListener do describe ActionCableListener do
let(:listener) { described_class.instance } let(:listener) { described_class.instance }
let!(:account) { create(:account) } let!(:account) { create(:account) }
let!(:admin) { create(:user, account: account, role: :administrator) } let(:admin) { create(:user, account: account, role: :administrator) }
let!(:inbox) { create(:inbox, account: account) } let!(:inbox) { create(:inbox, account: account) }
let!(:agent) { create(:user, account: account, role: :agent) } let!(:agent) { create(:user, account: account, role: :agent) }
let!(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: agent) } let!(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: agent) }
@@ -21,32 +21,33 @@ describe ActionCableListener do
end end
let!(:event) { Events::Base.new(event_name, Time.zone.now, message: message) } let!(:event) { Events::Base.new(event_name, Time.zone.now, message: message) }
it 'sends message to account admins, inbox agents and the contact' do it 'sends message to inbox stream' do
# HACK: to reload conversation inbox members
expect(conversation.inbox.reload.inbox_members.count).to eq(1)
expect(ActionCableBroadcastJob).to receive(:perform_later).with( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
a_collection_containing_exactly( ["inbox_#{message.inbox.id}"],
agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token 'message.created',
), message.push_event_data.merge(account_id: account.id)
)
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[conversation.contact_inbox.pubsub_token],
'message.created', 'message.created',
message.push_event_data.merge(account_id: account.id) message.push_event_data.merge(account_id: account.id)
) )
listener.message_created(event) listener.message_created(event)
end end
it 'sends message to all hmac verified contact inboxes' do it 'sends message to all hmac verified contact inboxes and inbox stream' do
# HACK: to reload conversation inbox members
expect(conversation.inbox.reload.inbox_members.count).to eq(1)
conversation.contact_inbox.update(hmac_verified: true) conversation.contact_inbox.update(hmac_verified: true)
# creating a non verified contact inbox to ensure the events are not sent to it # creating a non verified contact inbox to ensure the events are not sent to it
create(:contact_inbox, contact: conversation.contact, inbox: inbox) create(:contact_inbox, contact: conversation.contact, inbox: inbox)
verified_contact_inbox = create(:contact_inbox, contact: conversation.contact, inbox: inbox, hmac_verified: true) verified_contact_inbox = create(:contact_inbox, contact: conversation.contact, inbox: inbox, hmac_verified: true)
expect(ActionCableBroadcastJob).to receive(:perform_later).with( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
a_collection_containing_exactly( [conversation.contact_inbox.pubsub_token, verified_contact_inbox.pubsub_token],
agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token, verified_contact_inbox.pubsub_token 'message.created',
), message.push_event_data.merge(account_id: account.id)
)
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
["inbox_#{inbox.id}"],
'message.created', 'message.created',
message.push_event_data.merge(account_id: account.id) message.push_event_data.merge(account_id: account.id)
) )
@@ -58,13 +59,9 @@ describe ActionCableListener do
let(:event_name) { :'conversation.typing_on' } let(:event_name) { :'conversation.typing_on' }
let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: agent, is_private: false) } let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: agent, is_private: false) }
it 'sends message to account admins, inbox agents and the contact' do it 'sends message to inbox stream and 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( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
a_collection_containing_exactly( ["inbox_#{conversation.inbox.id}", conversation.contact_inbox.pubsub_token],
admin.pubsub_token, conversation.contact_inbox.pubsub_token
),
'conversation.typing_on', { conversation: conversation.push_event_data, 'conversation.typing_on', { conversation: conversation.push_event_data,
user: agent.push_event_data, user: agent.push_event_data,
account_id: account.id, account_id: account.id,
@@ -78,13 +75,11 @@ describe ActionCableListener do
let(:event_name) { :'conversation.typing_on' } let(:event_name) { :'conversation.typing_on' }
let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: conversation.contact, is_private: false) } 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 it 'sends message to inbox stream and 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( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
a_collection_containing_exactly( [
admin.pubsub_token, agent.pubsub_token "inbox_#{conversation.inbox.id}", conversation.contact_inbox.pubsub_token
), ],
'conversation.typing_on', { conversation: conversation.push_event_data, 'conversation.typing_on', { conversation: conversation.push_event_data,
user: conversation.contact.push_event_data, user: conversation.contact.push_event_data,
account_id: account.id, account_id: account.id,
@@ -98,13 +93,9 @@ describe ActionCableListener do
let(:event_name) { :'conversation.typing_off' } let(:event_name) { :'conversation.typing_off' }
let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: agent, is_private: false) } let!(:event) { Events::Base.new(event_name, Time.zone.now, conversation: conversation, user: agent, is_private: false) }
it 'sends message to account admins, inbox agents and the contact' do it 'sends message to inbox stream and 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( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
a_collection_containing_exactly( ["inbox_#{conversation.inbox.id}", conversation.contact_inbox.pubsub_token],
admin.pubsub_token, conversation.contact_inbox.pubsub_token
),
'conversation.typing_off', { conversation: conversation.push_event_data, 'conversation.typing_off', { conversation: conversation.push_event_data,
user: agent.push_event_data, user: agent.push_event_data,
account_id: account.id, account_id: account.id,
@@ -119,7 +110,7 @@ describe ActionCableListener do
let!(:contact) { create(:contact, account: account) } let!(:contact) { create(:contact, account: account) }
let!(:event) { Events::Base.new(event_name, Time.zone.now, contact: contact) } let!(:event) { Events::Base.new(event_name, Time.zone.now, contact: contact) }
it 'sends message to account admins, inbox agents' do it 'sends message to account stream' do
expect(ActionCableBroadcastJob).to receive(:perform_later).with( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
["account_#{account.id}"], ["account_#{account.id}"],
'contact.deleted', 'contact.deleted',
@@ -134,7 +125,7 @@ describe ActionCableListener do
let!(:notification) { create(:notification, account: account, user: agent) } let!(:notification) { create(:notification, account: account, user: agent) }
let!(:event) { Events::Base.new(event_name, Time.zone.now, notification: notification) } let!(:event) { Events::Base.new(event_name, Time.zone.now, notification: notification) }
it 'sends message to account admins, inbox agents' do it 'sends message to agent pubsub stream' do
expect(ActionCableBroadcastJob).to receive(:perform_later).with( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[agent.pubsub_token], [agent.pubsub_token],
'notification.deleted', 'notification.deleted',
@@ -157,7 +148,7 @@ describe ActionCableListener do
let!(:notification) { create(:notification, account: account, user: agent) } let!(:notification) { create(:notification, account: account, user: agent) }
let!(:event) { Events::Base.new(event_name, Time.zone.now, notification: notification) } let!(:event) { Events::Base.new(event_name, Time.zone.now, notification: notification) }
it 'sends notification to account admins, inbox agents' do it 'sends notification to agent pubsub stream' do
expect(ActionCableBroadcastJob).to receive(:perform_later).with( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[agent.pubsub_token], [agent.pubsub_token],
'notification.updated', 'notification.updated',
@@ -181,11 +172,14 @@ describe ActionCableListener do
conversation.add_labels(['support']) conversation.add_labels(['support'])
end end
it 'sends update to inbox members' do it 'sends update to contact and inbox' do
expect(conversation.inbox.reload.inbox_members.count).to eq(1)
expect(ActionCableBroadcastJob).to receive(:perform_later).with( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token], [conversation.contact_inbox.pubsub_token],
'conversation.updated',
conversation.push_event_data.merge(account_id: account.id)
)
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
["inbox_#{inbox.id}"],
'conversation.updated', 'conversation.updated',
conversation.push_event_data.merge(account_id: account.id) conversation.push_event_data.merge(account_id: account.id)
) )
@@ -196,7 +190,12 @@ describe ActionCableListener do
expect(conversation.reload.push_event_data[:labels]).to eq(conversation.labels.pluck(:name)) expect(conversation.reload.push_event_data[:labels]).to eq(conversation.labels.pluck(:name))
expect(ActionCableBroadcastJob).to receive(:perform_later).with( expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[agent.pubsub_token, admin.pubsub_token, conversation.contact_inbox.pubsub_token], ["inbox_#{inbox.id}"],
'conversation.updated',
conversation.push_event_data.merge(account_id: account.id)
)
expect(ActionCableBroadcastJob).to receive(:perform_later).with(
[conversation.contact_inbox.pubsub_token],
'conversation.updated', 'conversation.updated',
conversation.push_event_data.merge(account_id: account.id) conversation.push_event_data.merge(account_id: account.id)
) )