diff --git a/src/livekit/openIDSFU.ts b/src/livekit/openIDSFU.ts index dfe04323..20eb265b 100644 --- a/src/livekit/openIDSFU.ts +++ b/src/livekit/openIDSFU.ts @@ -155,6 +155,8 @@ export async function getSFUConfigWithOpenID( serviceUrl, roomId, openIdToken, + opts?.delayEndpointBaseUrl, + opts?.delayId, ); logger?.info(`Got JWT from call's active focus URL.`); return extractFullConfigFromToken(sfuConfig); @@ -187,20 +189,56 @@ async function getLiveKitJWT( livekitServiceURL: string, matrixRoomId: string, openIDToken: IOpenIDToken, + delayEndpointBaseUrl?: string, + delayId?: string, ): Promise<{ url: string; jwt: string }> { - const res = await doNetworkOperationWithRetry(async () => { + let bodyDalayParts = {}; + // Also check for empty string + if (delayId && delayEndpointBaseUrl) { + const delayTimeoutMs = + Config.get().matrix_rtc_session?.delayed_leave_event_delay_ms ?? 1000; + bodyDalayParts = { + delay_id: delayId, + delay_timeout: delayTimeoutMs, + delay_cs_api_url: delayEndpointBaseUrl, + }; + } + + const makeRequest = async ( + delayParts: Record, + ): Promise => { return await fetch(livekitServiceURL + "/sfu/get", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ - // This is the actual livekit room alias. For the legacy jwt endpoint simply the room id was used. + // The legacy jwt endpoint uses only the matrix room id to calculate the livekit room alias. + // In turn, the livekit room alias is provided as part f the JWT payload. room: matrixRoomId, openid_token: openIDToken, device_id: deviceId, + ...delayParts, }), }); + }; + + const res = await doNetworkOperationWithRetry(async () => { + let response = await makeRequest(bodyDalayParts); + + // If 400 with M_BAD_JSON and we sent delay parts, retry without them (old service compatibility) + if (response.status === 400 && Object.keys(bodyDalayParts).length > 0) { + try { + const errorBody = await response.json(); + if (errorBody.errcode === "M_BAD_JSON") { + response = await makeRequest({}); + } + } catch { + // If we can't parse the error, treat as real error + } + } + + return response; }); if (!res.ok) {