Skip to content

Commit 816a1ce

Browse files
committed
only start keytransport when session is joined
1 parent 1c4ae1b commit 816a1ce

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/matrixrtc/MatrixRTCSession.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export type JoinSessionConfig = MembershipConfig & EncryptionConfig;
160160
*/
161161
export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, MatrixRTCSessionEventHandlerMap> {
162162
private membershipManager?: IMembershipManager;
163-
private encryptionManager: IEncryptionManager;
163+
private encryptionManager?: IEncryptionManager;
164164
// The session Id of the call, this is the call_id of the call Member event.
165165
private _callId: string | undefined;
166166

@@ -317,18 +317,6 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
317317
// TODO: double check if this is actually needed. Should be covered by refreshRoom in MatrixRTCSessionManager
318318
roomState?.on(RoomStateEvent.Members, this.onRoomMemberUpdate);
319319
this.setExpiryTimer();
320-
321-
const transport = new RoomKeyTransport(this.roomSubset, this.client, this.statistics);
322-
this.encryptionManager = new EncryptionManager(
323-
this.client.getUserId()!,
324-
this.client.getDeviceId()!,
325-
() => this.memberships,
326-
transport,
327-
this.statistics,
328-
(keyBin: Uint8Array<ArrayBufferLike>, encryptionKeyIndex: number, participantId: string) => {
329-
this.emit(MatrixRTCSessionEvent.EncryptionKeyChanged, keyBin, encryptionKeyIndex, participantId);
330-
},
331-
);
332320
}
333321

334322
/*
@@ -381,6 +369,18 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
381369
this.getOldestMembership(),
382370
);
383371
}
372+
// Create Encryption manager
373+
const transport = new RoomKeyTransport(this.roomSubset, this.client, this.statistics);
374+
this.encryptionManager = new EncryptionManager(
375+
this.client.getUserId()!,
376+
this.client.getDeviceId()!,
377+
() => this.memberships,
378+
transport,
379+
this.statistics,
380+
(keyBin: Uint8Array<ArrayBufferLike>, encryptionKeyIndex: number, participantId: string) => {
381+
this.emit(MatrixRTCSessionEvent.EncryptionKeyChanged, keyBin, encryptionKeyIndex, participantId);
382+
},
383+
);
384384
}
385385

386386
// Join!
@@ -412,7 +412,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
412412

413413
logger.info(`Leaving call session in room ${this.roomSubset.roomId}`);
414414

415-
this.encryptionManager.leave();
415+
this.encryptionManager!.leave();
416416

417417
const leavePromise = this.membershipManager!.leave(timeout);
418418
this.emit(MatrixRTCSessionEvent.JoinStateChanged, false);
@@ -452,7 +452,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
452452
* the keys.
453453
*/
454454
public reemitEncryptionKeys(): void {
455-
this.encryptionManager.getEncryptionKeys().forEach((keys, participantId) => {
455+
this.encryptionManager?.getEncryptionKeys().forEach((keys, participantId) => {
456456
keys.forEach((key, index) => {
457457
this.emit(MatrixRTCSessionEvent.EncryptionKeyChanged, key.key, index, participantId);
458458
});
@@ -467,7 +467,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
467467
*/
468468
public getEncryptionKeys(): IterableIterator<[string, Array<Uint8Array>]> {
469469
const keys =
470-
this.encryptionManager.getEncryptionKeys() ??
470+
this.encryptionManager?.getEncryptionKeys() ??
471471
new Map<string, Array<{ key: Uint8Array; timestamp: number }>>();
472472
// the returned array doesn't contain the timestamps
473473
return Array.from(keys.entries())
@@ -540,7 +540,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
540540
}
541541
// This also needs to be done if `changed` = false
542542
// A member might have updated their fingerprint (created_ts)
543-
void this.encryptionManager.onMembershipsUpdate(oldMemberships);
543+
void this.encryptionManager?.onMembershipsUpdate(oldMemberships);
544544

545545
this.setExpiryTimer();
546546
};

src/matrixrtc/MatrixRTCSessionManager.ts

+2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ export class MatrixRTCSessionManager extends TypedEventEmitter<MatrixRTCSessionM
127127
const nowActive = session.memberships.length > 0;
128128

129129
if (wasActiveAndKnown && !nowActive) {
130+
logger.trace(`Session ended for ${room.roomId} (${session.memberships.length} members)`);
130131
this.emit(MatrixRTCSessionManagerEvents.SessionEnded, room.roomId, this.roomSessions.get(room.roomId)!);
131132
} else if (!wasActiveAndKnown && nowActive) {
133+
logger.trace(`Session started for ${room.roomId} (${session.memberships.length} members)`);
132134
this.emit(MatrixRTCSessionManagerEvents.SessionStarted, room.roomId, this.roomSessions.get(room.roomId)!);
133135
}
134136
}

0 commit comments

Comments
 (0)