Skip to content

Commit 3af1609

Browse files
committed
dedupe isMyMembership
1 parent 00579cd commit 3af1609

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

src/matrixrtc/EncryptionManager.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
import { logger as rootLogger } from "../logger.ts";
2-
import { Statistics, type EncryptionConfig } from "./MatrixRTCSession.ts";
2+
import { type EncryptionConfig } from "./MatrixRTCSession.ts";
33
import { secureRandomBase64Url } from "../randomstring.ts";
44
import { decodeBase64, encodeUnpaddedBase64 } from "../base64.ts";
55
import { safeGetRetryAfterMs } from "../http-api/errors.ts";
66
import { type CallMembership } from "./CallMembership.ts";
7-
import { KeyTransportEventListener, KeyTransportEvents, type IKeyTransport } from "./IKeyTransport.ts";
7+
import { type KeyTransportEventListener, KeyTransportEvents, type IKeyTransport } from "./IKeyTransport.ts";
8+
import { isMyMembership, type Statistics } from "./types.ts";
89

910
const logger = rootLogger.getChild("MatrixRTCSession");
1011

1112
/**
1213
* This interface is for testing and for making it possible to interchange the encryption manager.
1314
* @internal
1415
*/
16+
/**
17+
* Interface representing an encryption manager for handling encryption-related
18+
* operations in a real-time communication context.
19+
*/
1520
export interface IEncryptionManager {
21+
/**
22+
* Joins the encryption manager with the provided configuration.
23+
*
24+
* @param joinConfig - The configuration for joining encryption, or undefined
25+
* if no specific configuration is provided.
26+
*/
1627
join(joinConfig: EncryptionConfig | undefined): void;
1728

29+
/**
30+
* Leaves the encryption manager, cleaning up any associated resources.
31+
*/
1832
leave(): void;
1933

34+
/**
35+
* Called from the MatrixRTCSession when the memberships in this session updated.
36+
*
37+
* @param oldMemberships - The previous state of call memberships before the update.
38+
*/
2039
onMembershipsUpdate(oldMemberships: CallMembership[]): void;
2140

41+
/**
42+
* Retrieves the encryption keys currently managed by the encryption manager.
43+
*
44+
* @returns A map where the keys are identifiers and the values are arrays of
45+
* objects containing encryption keys and their associated timestamps.
46+
*/
2247
getEncryptionKeys(): Map<string, Array<{ key: Uint8Array; timestamp: number }>>;
2348
}
2449

@@ -112,17 +137,16 @@ export class EncryptionManager implements IEncryptionManager {
112137
this.joined = false;
113138
}
114139

115-
// TODO deduplicate this method. It also is in MatrixRTCSession.
116-
private isMyMembership = (m: CallMembership): boolean => m.sender === this.userId && m.deviceId === this.deviceId;
117-
118140
public onMembershipsUpdate(oldMemberships: CallMembership[]): void {
119141
if (this.manageMediaKeys && this.joined) {
120142
const oldMembershipIds = new Set(
121-
oldMemberships.filter((m) => !this.isMyMembership(m)).map(getParticipantIdFromMembership),
143+
oldMemberships
144+
.filter((m) => !isMyMembership(m, this.userId, this.deviceId))
145+
.map(getParticipantIdFromMembership),
122146
);
123147
const newMembershipIds = new Set(
124148
this.getMemberships()
125-
.filter((m) => !this.isMyMembership(m))
149+
.filter((m) => !isMyMembership(m, this.userId, this.deviceId))
126150
.map(getParticipantIdFromMembership),
127151
);
128152

@@ -272,7 +296,7 @@ export class EncryptionManager implements IEncryptionManager {
272296
private storeLastMembershipFingerprints(): void {
273297
this.lastMembershipFingerprints = new Set(
274298
this.getMemberships()
275-
.filter((m) => !this.isMyMembership(m))
299+
.filter((m) => !isMyMembership(m, this.userId, this.deviceId))
276300
.map((m) => `${getParticipantIdFromMembership(m)}:${m.createdTs()}`),
277301
);
278302
}

src/matrixrtc/NewMembershipManager.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ import { type Room } from "../models/room.ts";
2424
import { defer, type IDeferred } from "../utils.ts";
2525
import { type CallMembership, DEFAULT_EXPIRE_DURATION, type SessionMembershipData } from "./CallMembership.ts";
2626
import { type Focus } from "./focus.ts";
27-
import {
28-
type IMembershipManager,
29-
type MembershipManagerEventHandlerMap,
30-
MembershipManagerEvent,
31-
Status,
32-
} from "./types.ts";
27+
import { isMyMembership, Status } from "./types.ts";
3328
import { isLivekitFocusActive } from "./LivekitFocus.ts";
3429
import { type MembershipConfig } from "./MatrixRTCSession.ts";
3530
import { ActionScheduler, type ActionUpdate } from "./NewMembershipManagerActionScheduler.ts";
3631
import { TypedEventEmitter } from "../models/typed-event-emitter.ts";
32+
import {
33+
MembershipManagerEvent,
34+
type IMembershipManager,
35+
type MembershipManagerEventHandlerMap,
36+
} from "./IMembershipManager.ts";
3737

3838
const logger = rootLogger.getChild("MatrixRTCSession");
3939

@@ -219,10 +219,9 @@ export class MembershipManager
219219
private leavePromiseDefer?: IDeferred<boolean>;
220220

221221
public async onRTCSessionMemberUpdate(memberships: CallMembership[]): Promise<void> {
222-
const isMyMembership = (m: CallMembership): boolean =>
223-
m.sender === this.client.getUserId() && m.deviceId === this.client.getDeviceId();
224-
225-
if (this.isJoined() && !memberships.some(isMyMembership)) {
222+
const userId = this.client.getUserId();
223+
const deviceId = this.client.getDeviceId();
224+
if (userId && deviceId && this.isJoined() && !memberships.some((m) => isMyMembership(m, userId, deviceId))) {
226225
// If one of these actions are scheduled or are getting inserted in the next iteration, we should already
227226
// take care of our missing membership.
228227
const sendingMembershipActions = [

0 commit comments

Comments
 (0)