feat: transmit cache keys on room channel subscribe

This commit is contained in:
Shivam Mishra
2026-06-10 13:16:04 +05:30
parent 59d869d1ed
commit 261deaaeec
2 changed files with 29 additions and 0 deletions
+14
View File
@@ -7,6 +7,7 @@ class RoomChannel < ApplicationCable::Channel
ensure_stream
update_subscription
broadcast_presence
transmit_cache_keys
end
def update_presence
@@ -24,6 +25,19 @@ class RoomChannel < ApplicationCable::Channel
ActionCable.server.broadcast(pubsub_token, { event: 'presence.update', data: data })
end
# Push the authoritative cache-key map to this subscriber on every
# (re)subscribe. Boot and reconnect cache freshness ride the same
# account.cache_invalidated event the dashboard already handles for live
# invalidations — the client never pulls /cache_keys itself.
def transmit_cache_keys
return if @current_account.blank? || !@current_user.is_a?(User)
transmit({
event: Events::Types::ACCOUNT_CACHE_INVALIDATED,
data: { account_id: @current_account.id, cache_keys: @current_account.cache_keys }
})
end
def ensure_stream
stream_from pubsub_token
stream_from "account_#{@current_account.id}" if @current_account.present? && @current_user.is_a?(User)
+15
View File
@@ -21,4 +21,19 @@ RSpec.describe RoomChannel do
expect(subscription).to have_stream_for(user.pubsub_token)
expect(subscription).to have_stream_for("account_#{account.id}")
end
it 'transmits the account cache keys to user subscribers' do
subscribe(user_id: user.id, pubsub_token: user.pubsub_token, account_id: account.id)
cache_event = transmissions.find { |message| message['event'] == 'account.cache_invalidated' }
expect(cache_event['data']['account_id']).to eq(account.id)
expect(cache_event['data']['cache_keys'].keys).to match_array(%w[label inbox team])
end
it 'does not transmit cache keys to contact subscribers' do
subscribe(pubsub_token: contact_inbox.pubsub_token)
cache_event = transmissions.find { |message| message['event'] == 'account.cache_invalidated' }
expect(cache_event).to be_nil
end
end