feat: enhance call handling by prioritizing homeservers based on active call members

This commit is contained in:
2026-04-20 21:15:46 +10:00
parent 9161c4bde2
commit 13a0c98d1d
3 changed files with 62 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ import { useMatrixClient } from '../../hooks/useMatrixClient';
import { LiveKitFocus, LiveKitJWTResponse, OpenIdToken, WellKnownWithRTC } from './types';
import { getMxIdServer } from '../../utils/matrix';
import { guessDmRoomUserId } from '../../utils/matrix';
import { getActiveCallMembers } from './useRoomCallMembers';
/**
* Get room server from room ID (format: !roomid:server.com)
@@ -14,7 +15,8 @@ function getRoomServer(roomId: string): string | undefined {
/**
* Determine which homeservers to try for LiveKit JWT, in priority order
* @param room - Room to start call in
* Prioritizes servers where active call members are located when joining existing calls
* @param room - Room to start/join call in
* @param userHomeserver - User's homeserver
* @param myUserId - Current user's Matrix ID
* @returns Array of homeserver base URLs to try in order
@@ -27,29 +29,61 @@ export function getLiveKitHomeserverPriority(
const servers: string[] = [];
const memberCount = room.getMembers().length;
const isDM = memberCount <= 2;
// Check for active call members
const activeCallMembers = getActiveCallMembers(room);
const hasActiveCall = activeCallMembers.length > 0;
if (isDM) {
// For DMs: user's homeserver first, then other member's homeserver
servers.push(userHomeserver);
const otherUserId = guessDmRoomUserId(room, myUserId);
if (otherUserId && otherUserId !== myUserId) {
const otherServer = getMxIdServer(otherUserId);
if (otherServer && otherServer !== userHomeserver) {
servers.push(`https://${otherServer}`);
}
}
} else {
// For channels: room's homeserver first, then user's homeserver
if (hasActiveCall) {
// Joining existing call - prioritize room's homeserver (where call is hosted)
const roomServer = getRoomServer(room.roomId);
if (roomServer) {
servers.push(`https://${roomServer}`);
}
// Fallback to user's homeserver if different
if (!roomServer || roomServer !== userHomeserver.replace('https://', '').replace('http://', '')) {
// Add user's homeserver as fallback
const userDomain = userHomeserver.replace('https://', '').replace('http://', '');
if (!roomServer || roomServer !== userDomain) {
servers.push(userHomeserver);
}
// For DMs, also try other member's homeserver
if (isDM) {
const otherUserId = guessDmRoomUserId(room, myUserId);
if (otherUserId && otherUserId !== myUserId) {
const otherServer = getMxIdServer(otherUserId);
if (otherServer && otherServer !== userDomain && otherServer !== roomServer) {
servers.push(`https://${otherServer}`);
}
}
}
} else {
// Starting new call - use original priority logic
if (isDM) {
// For DMs: user's homeserver first, then other member's homeserver
servers.push(userHomeserver);
const otherUserId = guessDmRoomUserId(room, myUserId);
if (otherUserId && otherUserId !== myUserId) {
const otherServer = getMxIdServer(otherUserId);
const userDomain = userHomeserver.replace('https://', '').replace('http://', '');
if (otherServer && otherServer !== userDomain) {
servers.push(`https://${otherServer}`);
}
}
} else {
// For channels: room's homeserver first, then user's homeserver
const roomServer = getRoomServer(room.roomId);
if (roomServer) {
servers.push(`https://${roomServer}`);
}
// Fallback to user's homeserver if different
const userDomain = userHomeserver.replace('https://', '').replace('http://', '');
if (!roomServer || roomServer !== userDomain) {
servers.push(userHomeserver);
}
}
}
return servers;