fix: clean up cross-instance voice state when switching federated servers

Send explicit voice_leave to the old instance when joining voice on a
different origin, preventing stale voice state from showing the user in
two channels. Also make WS reconnect voice re-registration origin-aware
so remote reconnects properly restore voice state.
This commit is contained in:
Jannis Braun
2026-03-04 20:57:15 +01:00
parent c65a7387fa
commit 5e4a5c2ee9
4 changed files with 41 additions and 14 deletions
+26
View File
@@ -0,0 +1,26 @@
import { useVoiceStore } from '../stores/voiceStore';
import { getChannelOrigin } from '../stores/serverStore';
import { wsSend } from '../hooks/useWebSocket';
/**
* Centralized voice channel join that handles cross-instance cleanup.
* When switching from a channel on Instance A to one on Instance B,
* this sends an explicit voice_leave to Instance A first so it
* broadcasts a leave event and the client cleans up stale voice state.
*/
export function joinVoiceChannel(channelId: string): void {
const { currentVoiceChannelId, setCurrentVoiceChannel } = useVoiceStore.getState();
if (currentVoiceChannelId === channelId) return;
// Leave old instance if switching cross-origin
if (currentVoiceChannelId) {
const oldOrigin = getChannelOrigin(currentVoiceChannelId);
const newOrigin = getChannelOrigin(channelId);
if (oldOrigin !== newOrigin) {
wsSend({ type: 'voice_leave' }, oldOrigin);
}
}
setCurrentVoiceChannel(channelId);
wsSend({ type: 'voice_join', channelId }, getChannelOrigin(channelId));
}