mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-20 00:51:17 -08:00
feat(call): discover LiveKit transports and subscribe the call room
This commit is contained in:
1 parent
6442510033
commit
cb71537349
4 files changed
+91
-7
No files matched your search
+17
-6
@@ -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<string, unknown> & {
|
||||
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<AutoDiscoveryInfo, 'org.matrix.msc4143.rtc_foci'> | 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 (
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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<string>();
|
||||
|
||||
private readonly callRoomSubscriptions = new Set<string>();
|
||||
|
||||
/**
|
||||
* 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();
|
||||
|
||||
@@ -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';
|
||||
Reference in new issue
Block a user