chore: Reload conversation data in ActionCableBroadcastJob before sending (#10876)

During high-traffic periods, events may appear out of order, causing the
conversation job to queue outdated data, which can lead to issues in the
UI. This update ensures that only the latest available data is sent to the UI.

The conversation object is refreshed before sending it to the UI.
This commit is contained in:
Pranav
2025-02-10 23:16:15 -08:00
committed by GitHub
parent 8faccba052
commit 3c78d25306

View File

@@ -2,8 +2,34 @@ class ActionCableBroadcastJob < ApplicationJob
queue_as :critical
def perform(members, event_name, data)
return if members.blank?
broadcast_data = prepare_broadcast_data(event_name, data)
broadcast_to_members(members, event_name, broadcast_data)
end
private
# Ensures that only the latest available data is sent to prevent UI issues
# caused by out-of-order events during high-traffic periods. This prevents
# the conversation job from processing outdated data.
def prepare_broadcast_data(event_name, data)
return data unless event_name == 'conversation.updated'
account = Account.find(data[:account_id])
conversation = account.conversations.find_by!(display_id: data[:id])
conversation.push_event_data.merge(account_id: data[:account_id])
end
def broadcast_to_members(members, event_name, broadcast_data)
members.each do |member|
ActionCable.server.broadcast(member, { event: event_name, data: data })
ActionCable.server.broadcast(
member,
{
event: event_name,
data: broadcast_data
}
)
end
end
end