diff --git a/app/channels/room_channel.rb b/app/channels/room_channel.rb index 629078229..f7662ecda 100644 --- a/app/channels/room_channel.rb +++ b/app/channels/room_channel.rb @@ -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) diff --git a/spec/channels/room_channel_spec.rb b/spec/channels/room_channel_spec.rb index b39725f04..22b1a54a6 100644 --- a/spec/channels/room_channel_spec.rb +++ b/spec/channels/room_channel_spec.rb @@ -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