diff --git a/src/app/cs-api.ts b/src/app/cs-api.ts index ec985b97b..386ca3d19 100644 --- a/src/app/cs-api.ts +++ b/src/app/cs-api.ts @@ -1,4 +1,5 @@ import to from 'await-to-js'; +import type { LivekitTransportConfig } from '$types/matrix-sdk'; import { trimTrailingSlash } from './utils/common'; export enum AutoDiscoveryAction { @@ -22,12 +23,22 @@ export type AutoDiscoveryInfo = Record & { account?: string; issuer?: string; }; - 'org.matrix.msc4143.rtc_foci'?: [ - { - livekit_service_url: string; - type: 'livekit'; - }, - ]; + 'org.matrix.msc4143.rtc_foci'?: LivekitTransportConfig[]; +}; + +export const getLivekitTransports = ( + discovery: Pick | undefined +): LivekitTransportConfig[] => { + const foci = discovery?.['org.matrix.msc4143.rtc_foci']; + if (!Array.isArray(foci)) return []; + + return foci.filter( + (focus): focus is LivekitTransportConfig => + typeof focus === 'object' && + focus !== null && + focus.type === 'livekit' && + typeof focus.livekit_service_url === 'string' + ); }; export const autoDiscovery = async ( diff --git a/src/client/slidingSync.test.ts b/src/client/slidingSync.test.ts index 3324f4b00..463cc431b 100644 --- a/src/client/slidingSync.test.ts +++ b/src/client/slidingSync.test.ts @@ -500,6 +500,39 @@ describe('SlidingSyncManager initial request', () => { }); describe('SlidingSyncManager room subscription coordination', () => { + it('uses the full-membership subscription for the lifetime of a call', () => { + const manager = makeManager(makeMockMx()); + const roomId = '!call:example.com'; + + const subscription = manager.subscribeToCallRoom(roomId); + + expect(mocks.slidingSyncInstance.useCustomSubscription).toHaveBeenLastCalledWith( + roomId, + 'call_room' + ); + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenLastCalledWith( + new Set([roomId]) + ); + + subscription(); + + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenLastCalledWith(new Set()); + }); + + it('restores an active room subscription after a call ends', () => { + const manager = makeManager(makeMockMx()); + const roomId = '!call:example.com'; + manager.subscribeToRoom(roomId); + + const subscription = manager.subscribeToCallRoom(roomId); + subscription(); + + expect(mocks.slidingSyncInstance.useCustomSubscription).toHaveBeenLastCalledWith( + roomId, + 'active_room' + ); + }); + it('replaces route subscriptions atomically without cycling the retained space', () => { const manager = makeManager(makeMockMx()); const spaceId = '!space:example.com'; @@ -701,12 +734,17 @@ describe('SlidingSyncManager room subscription coordination', () => { { timeline_limit: number; required_state: [string, string][] }, ][]; const activeRoom = calls.find(([name]) => name === 'active_room'); + const callRoom = calls.find(([name]) => name === 'call_room'); const sidebarRoom = calls.find(([name]) => name === 'sidebar_room'); expect(activeRoom).toBeDefined(); expect(activeRoom![1].timeline_limit).toBe(50); expect(activeRoom![1].required_state).toContainEqual([EventType.RoomMember, '$LAZY']); + expect(callRoom).toBeDefined(); + expect(callRoom![1].timeline_limit).toBe(50); + expect(callRoom![1].required_state).toContainEqual([EventType.RoomMember, '*']); + expect(sidebarRoom).toBeDefined(); expect(sidebarRoom![1].timeline_limit).toBe(1); }); diff --git a/src/client/slidingSync.ts b/src/client/slidingSync.ts index 3eec08431..dfdebce01 100644 --- a/src/client/slidingSync.ts +++ b/src/client/slidingSync.ts @@ -43,6 +43,7 @@ const DEFAULT_POLL_TIMEOUT_MS = 45000; const LIST_SORT_ORDER = ['by_recency', 'by_name']; const ACTIVE_ROOM_SUBSCRIPTION_KEY = 'active_room'; +const CALL_ROOM_SUBSCRIPTION_KEY = 'call_room'; const SIDEBAR_ROOM_SUBSCRIPTION_KEY = 'sidebar_room'; const SPACE_SUBSCRIPTION_KEY = 'space'; const IMAGE_PACK_SUBSCRIPTION_KEY = 'image_packs'; @@ -88,6 +89,8 @@ export type SlidingSyncDiagnostics = { export type HydrationProgress = { loadedRooms: number; totalRooms: number }; +export type CallRoomSubscription = () => void; + const clampPositive = (value: number | undefined, fallback: number): number => { if (typeof value !== 'number' || Number.isNaN(value) || value <= 0) return fallback; return Math.round(value); @@ -209,6 +212,13 @@ const buildUnencryptedSubscription = (timelineLimit: number): MSC3575RoomSubscri required_state: ACTIVE_ROOM_REQUIRED_STATE, }); +const buildCallRoomSubscription = (timelineLimit: number): MSC3575RoomSubscription => ({ + timeline_limit: timelineLimit, + // MatrixRTC ignores memberships for users that are absent from the room + // roster. Unlike a timeline, calls need every membership continuously. + required_state: [...ACTIVE_ROOM_REQUIRED_STATE, [EventType.RoomMember, MSC3575_WILDCARD]], +}); + const IMAGE_PACK_REQUIRED_STATE: MSC3575RoomSubscription['required_state'] = [ [CustomStateEvent.ImagePack, MSC3575_WILDCARD], [CustomStateEvent.PoniesRoomEmotes, MSC3575_WILDCARD], @@ -302,6 +312,8 @@ export class SlidingSyncManager { private readonly activeRoomSubscriptions = new Set(); + private readonly callRoomSubscriptions = new Set(); + /** * Rooms joined locally via reconcileRoomMembership(Join) but not yet confirmed * joined by a sliding-sync response. The SDK reverts these to "invite" when the @@ -445,6 +457,10 @@ export class SlidingSyncManager { ACTIVE_ROOM_SUBSCRIPTION_KEY, buildUnencryptedSubscription(roomTimelineLimit) ); + this.slidingSync.addCustomSubscription( + CALL_ROOM_SUBSCRIPTION_KEY, + buildCallRoomSubscription(roomTimelineLimit) + ); this.slidingSync.addCustomSubscription( SIDEBAR_ROOM_SUBSCRIPTION_KEY, buildSidebarRoomSubscription() @@ -1309,6 +1325,7 @@ export class SlidingSyncManager { private syncRoomSubscriptions(): void { const desiredSubscriptions = new Set([ + ...this.callRoomSubscriptions, ...this.activeRoomSubscriptions, ...this.sidebarRoomSubscriptions, ...this.spaceSubscriptions, @@ -1316,7 +1333,9 @@ export class SlidingSyncManager { ]); desiredSubscriptions.forEach((roomId) => { - if (this.activeRoomSubscriptions.has(roomId)) { + if (this.callRoomSubscriptions.has(roomId)) { + this.slidingSync.useCustomSubscription(roomId, CALL_ROOM_SUBSCRIPTION_KEY); + } else if (this.activeRoomSubscriptions.has(roomId)) { this.slidingSync.useCustomSubscription(roomId, ACTIVE_ROOM_SUBSCRIPTION_KEY); } else if (this.sidebarRoomSubscriptions.has(roomId)) { this.slidingSync.useCustomSubscription(roomId, SIDEBAR_ROOM_SUBSCRIPTION_KEY); @@ -1517,6 +1536,19 @@ export class SlidingSyncManager { this.reportActiveSubscriptionCount(); } + public subscribeToCallRoom(roomId: string): CallRoomSubscription { + if (this.disposed) return () => undefined; + + this.callRoomSubscriptions.add(roomId); + this.syncRoomSubscriptions(); + return () => this.unsubscribeFromCallRoom(roomId); + } + + public unsubscribeFromCallRoom(roomId: string): void { + if (this.disposed || !this.callRoomSubscriptions.delete(roomId)) return; + this.syncRoomSubscriptions(); + } + public unsubscribeFromRoom(roomId: string): void { if (this.disposed || !this.removeActiveRoomSubscription(roomId)) return; this.syncRoomSubscriptions(); diff --git a/src/types/matrix-sdk.ts b/src/types/matrix-sdk.ts index f1bea4e09..7c2ec0653 100644 --- a/src/types/matrix-sdk.ts +++ b/src/types/matrix-sdk.ts @@ -55,5 +55,8 @@ export * from 'matrix-js-sdk/lib/common-crypto/CryptoBackend'; export * from 'matrix-js-sdk/lib/matrixrtc/CallMembership'; export * from 'matrix-js-sdk/lib/matrixrtc/MatrixRTCSession'; export * from 'matrix-js-sdk/lib/matrixrtc/MatrixRTCSessionManager'; +export { type CallMembershipIdentityParts } from 'matrix-js-sdk/lib/matrixrtc/EncryptionManager'; +export { type LivekitTransportConfig } from 'matrix-js-sdk/lib/matrixrtc/LivekitTransport'; +export { type Transport } from 'matrix-js-sdk/lib/matrixrtc/types'; export * from 'matrix-js-sdk/lib/models/thread';