From c798a1f76dd89d9adecafc573e246ac86de0d2ee Mon Sep 17 00:00:00 2001 From: Timo Date: Fri, 10 Jan 2025 17:23:27 +0100 Subject: [PATCH] renaming public api --- spec/unit/matrixrtc/MatrixRTCSession.spec.ts | 22 ++++++------ src/matrixrtc/MatrixRTCSession.ts | 36 ++++++++++++++------ src/matrixrtc/MatrixRTCSessionManager.ts | 2 +- src/matrixrtc/MembershipManager.ts | 11 +++--- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/spec/unit/matrixrtc/MatrixRTCSession.spec.ts b/spec/unit/matrixrtc/MatrixRTCSession.spec.ts index 9f7a3a97d3..7e50d95223 100644 --- a/spec/unit/matrixrtc/MatrixRTCSession.spec.ts +++ b/spec/unit/matrixrtc/MatrixRTCSession.spec.ts @@ -555,7 +555,7 @@ describe("MatrixRTCSession", () => { const onMembershipsChanged = jest.fn(); sess.on(MatrixRTCSessionEvent.MembershipsChanged, onMembershipsChanged); - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); expect(onMembershipsChanged).not.toHaveBeenCalled(); }); @@ -568,7 +568,7 @@ describe("MatrixRTCSession", () => { sess.on(MatrixRTCSessionEvent.MembershipsChanged, onMembershipsChanged); mockRoom.getLiveTimeline().getState = jest.fn().mockReturnValue(makeMockRoomState([], mockRoom.roomId)); - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); expect(onMembershipsChanged).toHaveBeenCalled(); }); @@ -757,7 +757,7 @@ describe("MatrixRTCSession", () => { mockRoom.getLiveTimeline().getState = jest .fn() .mockReturnValue(makeMockRoomState([membershipTemplate], mockRoom.roomId)); - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); // member2 re-joins which should trigger an immediate re-send const keysSentPromise2 = new Promise((resolve) => { @@ -766,7 +766,7 @@ describe("MatrixRTCSession", () => { mockRoom.getLiveTimeline().getState = jest .fn() .mockReturnValue(makeMockRoomState([membershipTemplate, member2], mockRoom.roomId)); - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); // but, that immediate resend is throttled so we need to wait a bit jest.advanceTimersByTime(1000); const { keys } = await keysSentPromise2; @@ -819,7 +819,7 @@ describe("MatrixRTCSession", () => { mockRoom.getLiveTimeline().getState = jest .fn() .mockReturnValue(makeMockRoomState([membershipTemplate, member2], mockRoom.roomId)); - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); await keysSentPromise2; @@ -873,7 +873,7 @@ describe("MatrixRTCSession", () => { sendEventMock.mockClear(); // these should be a no-op: - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); expect(sendEventMock).toHaveBeenCalledTimes(0); expect(sess!.statistics.counters.roomEventEncryptionKeysSent).toEqual(1); } finally { @@ -927,7 +927,7 @@ describe("MatrixRTCSession", () => { sendEventMock.mockClear(); // this should be a no-op: - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); expect(sendEventMock).toHaveBeenCalledTimes(0); // advance time to avoid key throttling @@ -941,7 +941,7 @@ describe("MatrixRTCSession", () => { }); // this should re-send the key - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); await keysSentPromise2; @@ -1004,7 +1004,7 @@ describe("MatrixRTCSession", () => { mockRoom.getLiveTimeline().getState = jest .fn() .mockReturnValue(makeMockRoomState([membershipTemplate], mockRoom.roomId)); - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); jest.advanceTimersByTime(10000); @@ -1049,7 +1049,7 @@ describe("MatrixRTCSession", () => { ); } - sess!.onMembershipsUpdate(); + sess!.onRTCSessionMemberUpdate(); // advance time to avoid key throttling jest.advanceTimersByTime(10000); @@ -1090,7 +1090,7 @@ describe("MatrixRTCSession", () => { mockRoom.getLiveTimeline().getState = jest .fn() .mockReturnValue(makeMockRoomState([membershipTemplate, member2], mockRoom.roomId)); - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); await new Promise((resolve) => { realSetTimeout(resolve); diff --git a/src/matrixrtc/MatrixRTCSession.ts b/src/matrixrtc/MatrixRTCSession.ts index 3c311ccf4a..0da743e566 100644 --- a/src/matrixrtc/MatrixRTCSession.ts +++ b/src/matrixrtc/MatrixRTCSession.ts @@ -29,7 +29,7 @@ import { decodeBase64, encodeUnpaddedBase64 } from "../base64.ts"; import { KnownMembership } from "../@types/membership.ts"; import { MatrixError, safeGetRetryAfterMs } from "../http-api/errors.ts"; import { MatrixEvent } from "../models/event.ts"; -import { MembershipManager, MembershipManagerInterface } from "./MembershipManager.ts"; +import { LegacyMembershipManager, IMembershipManager } from "./MembershipManager.ts"; const logger = rootLogger.getChild("MatrixRTCSession"); @@ -132,7 +132,7 @@ export type JoinSessionConfig = MembershipConfig & EncryptionConfig; * This class doesn't deal with media at all, just membership & properties of a session. */ export class MatrixRTCSession extends TypedEventEmitter { - private membershipManager?: MembershipManagerInterface; + private membershipManager?: IMembershipManager; // The session Id of the call, this is the call_id of the call Member event. private _callId: string | undefined; @@ -284,7 +284,7 @@ export class MatrixRTCSession extends TypedEventEmitter + this.membershipManager = new LegacyMembershipManager(joinConfig, this.room, this.client, () => this.getOldestMembership(), ); } @@ -652,7 +652,7 @@ export class MatrixRTCSession extends TypedEventEmitter { - this.onMembershipsUpdate(); + this.recalculateSessionMembers(); }; /** + * Call this when the room members have changed. + */ + public onRoomMemberUpdate = (): void => { + this.recalculateSessionMembers(); + }; + + /** + * Call this when sth changed that impacts the current rtc members in this session. + */ + public onRTCSessionMemberUpdate = (): void => { + this.recalculateSessionMembers(); + }; + + /** + * Call this when anything that could impact rtc memberships has changed: Room Members or RTC members. + * * Examines the latest call memberships and handles any encryption key sending or rotation that is needed. * * This function should be called when the room members or call memberships might have changed. */ - public onMembershipsUpdate = (): void => { + private recalculateSessionMembers = (): void => { const oldMemberships = this.memberships; this.memberships = MatrixRTCSession.callMembershipsForRoom(this.room); @@ -781,7 +797,7 @@ export class MatrixRTCSession extends TypedEventEmitter 0 && !isNewSession; - sess.onMembershipsUpdate(); + sess.onRTCSessionMemberUpdate(); const nowActive = sess.memberships.length > 0; diff --git a/src/matrixrtc/MembershipManager.ts b/src/matrixrtc/MembershipManager.ts index 561677205e..e195917156 100644 --- a/src/matrixrtc/MembershipManager.ts +++ b/src/matrixrtc/MembershipManager.ts @@ -17,11 +17,14 @@ import { MembershipConfig } from "./MatrixRTCSession.ts"; * * @internal */ -export interface MembershipManagerInterface { +export interface IMembershipManager { isJoined(): boolean; join(fociPreferred: Focus[], fociActive?: Focus): void; leave(timeout: number | undefined): Promise; - onMembershipsUpdate(memberships: CallMembership[]): Promise; + /** + * call this if the MatrixRTC session members have changed + */ + onRTCSessionMemberUpdate(memberships: CallMembership[]): Promise; getActiveFocus(): Focus | undefined; } @@ -41,7 +44,7 @@ export interface MembershipManagerInterface { * * @internal */ -export class MembershipManager implements MembershipManagerInterface { +export class LegacyMembershipManager implements IMembershipManager { private relativeExpiry: number | undefined; private memberEventTimeout?: ReturnType; @@ -135,7 +138,7 @@ export class MembershipManager implements MembershipManagerInterface { } } - public async onMembershipsUpdate(memberships: CallMembership[]): Promise { + public async onRTCSessionMemberUpdate(memberships: CallMembership[]): Promise { const isMyMembership = (m: CallMembership): boolean => m.sender === this.client.getUserId() && m.deviceId === this.client.getDeviceId();