fix: prevent auto-connect on dm_call_accepted for non-caller instances
Three fixes for multi-instance call state consistency: 1. Client dm_call_accepted handler only auto-connects to LiveKit if the user was the caller (outgoingCall was set). Other instances of the same user just clear ringing state without connecting. 2. Server processDmCallAcceptEvent remote path skips duplicate broadcast when FederatedCallEntry is already active (prevents state conflicts from host fan-out arriving after local accept). 3. Ready payload handler clears stuck incomingCall when restoring an already-active call after page refresh.
This commit is contained in:
@@ -3614,7 +3614,12 @@ function processDmCallAcceptEvent(
|
|||||||
// We're a REMOTE instance receiving fan-out — transition local state
|
// We're a REMOTE instance receiving fan-out — transition local state
|
||||||
const fedCall = connectionManager.getFederatedCall(event.federatedId);
|
const fedCall = connectionManager.getFederatedCall(event.federatedId);
|
||||||
if (fedCall) {
|
if (fedCall) {
|
||||||
|
// Only broadcast if transitioning from ringing → active.
|
||||||
|
// If already active (e.g., we initiated the accept and the host is fanning out back),
|
||||||
|
// skip the duplicate broadcast to avoid state conflicts on the client.
|
||||||
|
const wasRinging = fedCall.state === 'ringing';
|
||||||
connectionManager.activateFederatedCall(event.federatedId);
|
connectionManager.activateFederatedCall(event.federatedId);
|
||||||
|
if (wasRinging) {
|
||||||
connectionManager.sendToFederatedCallUsers(event.federatedId, {
|
connectionManager.sendToFederatedCallUsers(event.federatedId, {
|
||||||
type: 'dm_call_accepted',
|
type: 'dm_call_accepted',
|
||||||
dmChannelId: fedCall.dmChannelId,
|
dmChannelId: fedCall.dmChannelId,
|
||||||
@@ -3622,6 +3627,7 @@ function processDmCallAcceptEvent(
|
|||||||
} as ServerEvent);
|
} as ServerEvent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
accepted.push(event.messageId);
|
accepted.push(event.messageId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -322,6 +322,8 @@ function handleEvent(origin: string, event: ServerEvent): void {
|
|||||||
const isParticipant = call.participants.includes(myId) || !!call.livekitToken;
|
const isParticipant = call.participants.includes(myId) || !!call.livekitToken;
|
||||||
if (call.state === 'active' && isParticipant) {
|
if (call.state === 'active' && isParticipant) {
|
||||||
const callDmId = call.dmChannelId || call.federatedCallId || '';
|
const callDmId = call.dmChannelId || call.federatedCallId || '';
|
||||||
|
// Clear any stuck ringing UI from a ringing→active transition during refresh
|
||||||
|
setIncomingCall(null);
|
||||||
setActiveDmCall({ dmChannelId: callDmId });
|
setActiveDmCall({ dmChannelId: callDmId });
|
||||||
// Store federated call data if present (server already filtered to this user's token)
|
// Store federated call data if present (server already filtered to this user's token)
|
||||||
if (call.livekitUrl && call.livekitToken) {
|
if (call.livekitUrl && call.livekitToken) {
|
||||||
@@ -797,13 +799,17 @@ function handleEvent(origin: string, event: ServerEvent): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case 'dm_call_accepted': {
|
case 'dm_call_accepted': {
|
||||||
const { setIncomingCall, setOutgoingCall, setActiveDmCall, connectFn, isLiveKitConnected } = useVoiceStore.getState();
|
const { setIncomingCall, setOutgoingCall, outgoingCall, setActiveDmCall, connectFn, isLiveKitConnected } = useVoiceStore.getState();
|
||||||
|
// Track whether WE are the caller before clearing state
|
||||||
|
const wasOutgoingCall = !!outgoingCall;
|
||||||
setIncomingCall(null);
|
setIncomingCall(null);
|
||||||
setOutgoingCall(null);
|
setOutgoingCall(null);
|
||||||
const callDmId = event.dmChannelId || event.federatedCallId || '';
|
const callDmId = event.dmChannelId || event.federatedCallId || '';
|
||||||
setActiveDmCall({ dmChannelId: callDmId });
|
setActiveDmCall({ dmChannelId: callDmId });
|
||||||
// Only connect if not already connected (federated acceptor connects immediately in handleAccept)
|
// Only auto-connect if we're the CALLER waiting for acceptance.
|
||||||
if (connectFn && !isLiveKitConnected && callDmId) {
|
// The callee who accepted connects in the click handler (handleAccept).
|
||||||
|
// Other instances of the same user should NOT auto-connect.
|
||||||
|
if (connectFn && !isLiveKitConnected && wasOutgoingCall && callDmId) {
|
||||||
connectFn(callDmId, true).catch((err: unknown) => {
|
connectFn(callDmId, true).catch((err: unknown) => {
|
||||||
console.error('[WS] DM call connect failed:', err);
|
console.error('[WS] DM call connect failed:', err);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user