Skip to content

Commit a9aa68d

Browse files
committed
review (dave)
1 parent 778db8c commit a9aa68d

File tree

6 files changed

+17
-41
lines changed

6 files changed

+17
-41
lines changed

spec/unit/matrixrtc/MatrixRTCSession.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ describe("MatrixRTCSession", () => {
835835
it("rotates key if a member leaves", async () => {
836836
jest.useFakeTimers();
837837
try {
838-
const KEY_DALAY = 3000;
838+
const KEY_DELAY = 3000;
839839
const member2 = Object.assign({}, membershipTemplate, {
840840
device_id: "BBBBBBB",
841841
});
@@ -856,7 +856,7 @@ describe("MatrixRTCSession", () => {
856856
sendEventMock.mockImplementation((_roomId, _evType, payload) => resolve(payload));
857857
});
858858

859-
sess.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true, makeKeyDelay: KEY_DALAY });
859+
sess.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true, makeKeyDelay: KEY_DELAY });
860860
const sendKeySpy = jest.spyOn((sess as unknown as any).encryptionManager.transport, "sendKey");
861861
const firstKeysPayload = await keysSentPromise1;
862862
expect(firstKeysPayload.keys).toHaveLength(1);
@@ -874,7 +874,7 @@ describe("MatrixRTCSession", () => {
874874
.mockReturnValue(makeMockRoomState([membershipTemplate], mockRoom.roomId));
875875
sess.onRTCSessionMemberUpdate();
876876

877-
jest.advanceTimersByTime(3000);
877+
jest.advanceTimersByTime(KEY_DELAY);
878878
expect(sendKeySpy).toHaveBeenCalledTimes(1);
879879
// check that we send the key with index 1 even though the send gets delayed when leaving.
880880
// this makes sure we do not use an index that is one too old.

spec/unit/matrixrtc/ToDeviceKeyTransport.spec.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe("ToDeviceKeyTransport", () => {
3333
let mockLogger: Mocked<Logger>;
3434
let transport: ToDeviceKeyTransport;
3535

36-
function setup() {
36+
beforeEach(() => {
3737
mockClient = getMockClientWithEventEmitter({
3838
encryptAndSendToDevice: jest.fn(),
3939
});
@@ -54,11 +54,9 @@ describe("ToDeviceKeyTransport", () => {
5454
transport = new ToDeviceKeyTransport("@alice:example.org", "MYDEVICE", roomId, mockClient, statistics, {
5555
getChild: jest.fn().mockReturnValue(mockLogger),
5656
} as unknown as Mocked<Logger>);
57-
}
57+
});
5858

5959
it("should send my keys on via to device", async () => {
60-
setup();
61-
6260
transport.start();
6361

6462
const keyBase64Encoded = "ABCDEDF";
@@ -110,8 +108,6 @@ describe("ToDeviceKeyTransport", () => {
110108
});
111109

112110
it("should emit when a key is received", async () => {
113-
setup();
114-
115111
const deferred = defer<{ userId: string; deviceId: string; keyBase64Encoded: string; index: number }>();
116112
transport.on(KeyTransportEvents.ReceivedKeys, (userId, deviceId, keyBase64Encoded, index, timestamp) => {
117113
deferred.resolve({ userId, deviceId, keyBase64Encoded, index });
@@ -150,8 +146,6 @@ describe("ToDeviceKeyTransport", () => {
150146
});
151147

152148
it("should not sent to ourself", async () => {
153-
setup();
154-
155149
const keyBase64Encoded = "ABCDEDF";
156150
const keyIndex = 2;
157151
await transport.sendKey(keyBase64Encoded, keyIndex, [
@@ -168,8 +162,6 @@ describe("ToDeviceKeyTransport", () => {
168162
});
169163

170164
it("should warn when there is a room mismatch", () => {
171-
setup();
172-
173165
transport.start();
174166

175167
const testEncoded = "ABCDEDF";
@@ -243,8 +235,6 @@ describe("ToDeviceKeyTransport", () => {
243235
];
244236

245237
test.each(MALFORMED_EVENT)("should warn on malformed event %j", (event) => {
246-
setup();
247-
248238
transport.start();
249239

250240
mockClient.emit(

src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7946,9 +7946,9 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
79467946
* This will encrypt the payload for all devices in the list and will queue it.
79477947
* The type of the sent to-device message will be `m.room.encrypted`.
79487948
* @param eventType - The type of event to send
7949-
* @param devices - The list of devices to send the event to. Each device is
7949+
* @param devices - The list of devices to send the event to.
79507950
* @param payload - The payload to send. This will be encrypted.
7951-
* @returns Promise which resolves once queued.
7951+
* @returns Promise which resolves once queued there is no error feedback when sending fails.
79527952
*/
79537953
public async encryptAndSendToDevice(
79547954
eventType: string,

src/matrixrtc/EncryptionManager.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ export class EncryptionManager implements IEncryptionManager {
7676
// if it looks like a membership has been updated.
7777
private lastMembershipFingerprints: Set<string> | undefined;
7878

79-
// TODO: remove this value since I think this is not helpful to use
80-
// it represents what index we actually sent over to ElementCall (which we do in a delayed manner)
81-
// but it has no releavnt information for the encryption manager.
82-
private mediaTrailerKeyIndexInUse = -1;
8379
private latestGeneratedKeyIndex = -1;
8480
private joinConfig: EncryptionConfig | undefined;
8581

@@ -266,12 +262,7 @@ export class EncryptionManager implements IEncryptionManager {
266262
}
267263

268264
const keyIndexToSend = indexToSend ?? this.latestGeneratedKeyIndex;
269-
// TODO remove this debug log. it just shows then when sending mediaTrailerKeyIndexInUse contained the wrong index.
270-
logger.debug(
271-
`Compare key in use to last generated key\n`,
272-
`latestGeneratedKeyIndex: ${this.latestGeneratedKeyIndex}\n`,
273-
`mediaTrailerKeyIndexInUse: ${this.mediaTrailerKeyIndexInUse}`,
274-
);
265+
275266
logger.info(
276267
`Try sending encryption keys event. keyIndexToSend=${keyIndexToSend} (method parameter: ${indexToSend})`,
277268
);
@@ -382,16 +373,11 @@ export class EncryptionManager implements IEncryptionManager {
382373
const useKeyTimeout = setTimeout(() => {
383374
this.setNewKeyTimeouts.delete(useKeyTimeout);
384375
logger.info(`Delayed-emitting key changed event for ${participantId} index ${encryptionKeyIndex}`);
385-
if (userId === this.userId && deviceId === this.deviceId) {
386-
this.mediaTrailerKeyIndexInUse = encryptionKeyIndex;
387-
}
376+
388377
this.onEncryptionKeysChanged(keyBin, encryptionKeyIndex, participantId);
389378
}, this.useKeyDelay);
390379
this.setNewKeyTimeouts.add(useKeyTimeout);
391380
} else {
392-
if (userId === this.userId && deviceId === this.deviceId) {
393-
this.mediaTrailerKeyIndexInUse = encryptionKeyIndex;
394-
}
395381
this.onEncryptionKeysChanged(keyBin, encryptionKeyIndex, participantId);
396382
}
397383
}

src/matrixrtc/IKeyTransport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface IKeyTransport {
4747

4848
/** Subscribe to keys from this transport. */
4949
on(event: KeyTransportEvents.ReceivedKeys, listener: KeyTransportEventListener): this;
50-
/** Unsubscribe to keys from this transport. */
50+
/** Unsubscribe from keys from this transport. */
5151
off(event: KeyTransportEvents.ReceivedKeys, listener: KeyTransportEventListener): this;
5252

5353
/** Once start is called the underlying transport will subscribe to its transport system.

src/matrixrtc/ToDeviceKeyTransport.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import { ClientEvent, type MatrixClient } from "../client.ts";
2323
import type { MatrixEvent } from "../models/event.ts";
2424
import { EventType } from "../@types/event.ts";
2525

26+
/**
27+
* ToDeviceKeyTransport is used to send MatrixRTC keys to other devices using the
28+
* to-device CS-API.
29+
*/
2630
export class ToDeviceKeyTransport
2731
extends TypedEventEmitter<KeyTransportEvents, KeyTransportEventsHandlerMap>
2832
implements IKeyTransport
@@ -42,7 +46,7 @@ export class ToDeviceKeyTransport
4246
}
4347

4448
public start(): void {
45-
this.client.on(ClientEvent.ToDeviceEvent, (ev) => this.onToDeviceEvent(ev));
49+
this.client.on(ClientEvent.ToDeviceEvent, this.onToDeviceEvent);
4650
}
4751

4852
public stop(): void {
@@ -110,7 +114,7 @@ export class ToDeviceKeyTransport
110114
content.member.claimed_device_id!,
111115
content.keys.key,
112116
content.keys.index,
113-
age,
117+
now,
114118
);
115119
}
116120

@@ -159,11 +163,7 @@ export class ToDeviceKeyTransport
159163
return;
160164
}
161165

162-
// TODO session is not used so far
163-
// if (!content.session || !content.session.call_id || !content.session.scope || !content.session.application) {
164-
// this.prefixedLogger.warn("Malformed Event: Missing/Malformed content.session", content.session);
165-
// return;
166-
// }
166+
// TODO check for session related fields once the to-device encryption uses the new format.
167167
return content as EncryptionKeysToDeviceEventContent;
168168
}
169169
}

0 commit comments

Comments
 (0)