From 16adc869c8545b9318d7d7fb68eb383ba71ce64c Mon Sep 17 00:00:00 2001 From: Florian Duros Date: Wed, 26 Jul 2023 16:29:52 +0200 Subject: [PATCH] Review changes --- spec/unit/rust-crypto/rust-crypto.spec.ts | 2 +- src/client.ts | 2 - src/crypto-api.ts | 2 +- .../verification/request/InRoomChannel.ts | 2 +- src/rust-crypto/rust-crypto.ts | 42 ++++++++++++------- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/spec/unit/rust-crypto/rust-crypto.spec.ts b/spec/unit/rust-crypto/rust-crypto.spec.ts index 13c3b944179..54f5f180555 100644 --- a/spec/unit/rust-crypto/rust-crypto.spec.ts +++ b/spec/unit/rust-crypto/rust-crypto.spec.ts @@ -515,7 +515,7 @@ describe("RustCrypto", () => { }); describe("findVerificationRequestDMInProgress", () => { - it("misses userId", async () => { + it("returns null if the userId is not provided", async () => { const rustCrypto = await makeTestRustCrypto(); expect(rustCrypto.findVerificationRequestDMInProgress(testData.TEST_ROOM_ID)).not.toBeDefined(); }); diff --git a/src/client.ts b/src/client.ts index 46fb5112e71..20aba4ee61a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -2248,8 +2248,6 @@ export class MatrixClient extends TypedEventEmitter { rustCrypto.onLiveEventFromSync(event); }); diff --git a/src/crypto-api.ts b/src/crypto-api.ts index 1fe487c413f..e9d3f4ce6cc 100644 --- a/src/crypto-api.ts +++ b/src/crypto-api.ts @@ -255,7 +255,7 @@ export interface CryptoApi { findVerificationRequestDMInProgress(roomId: string): VerificationRequest | undefined; /** - * Finds a DM verification request that is already in progress for the given room id. + * Finds a DM verification request that is already in progress for the given room and user. * * @param roomId - the room to use for verification. * @param userId - search for a verification request for the given user. diff --git a/src/crypto/verification/request/InRoomChannel.ts b/src/crypto/verification/request/InRoomChannel.ts index af3cb59a0d0..c776ddbe395 100644 --- a/src/crypto/verification/request/InRoomChannel.ts +++ b/src/crypto/verification/request/InRoomChannel.ts @@ -354,7 +354,7 @@ export class InRoomRequests implements IRequestsMap { const requestsByTxnId = this.requestsByRoomId.get(roomId); if (requestsByTxnId) { for (const request of requestsByTxnId.values()) { - if (request.pending && (!userId || request.requestingUserId === userId)) { + if (request.pending && (userId === undefined || request.requestingUserId === userId)) { return request; } } diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index b0345fe7ada..50d999bf419 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -19,7 +19,7 @@ import * as RustSdkCryptoJs from "@matrix-org/matrix-sdk-crypto-wasm"; import type { IEventDecryptionResult, IMegolmSessionData } from "../@types/crypto"; import type { IDeviceLists, IToDeviceEvent } from "../sync-accumulator"; import type { IEncryptedEventInfo } from "../crypto/api"; -import { IContent, MatrixEvent } from "../models/event"; +import { IContent, MatrixEvent, MatrixEventEvent } from "../models/event"; import { Room } from "../models/room"; import { RoomMember } from "../models/room-member"; import { CryptoBackend, OnSyncCompletedData } from "../common-crypto/CryptoBackend"; @@ -97,6 +97,7 @@ export class RustCrypto extends TypedEventEmitter { // Process only key validation request + // and ignore state event if ( + !event.isState() && event.getType() === EventType.RoomMessage && event.getContent().msgtype === MsgType.KeyVerificationRequest ) { @@ -982,18 +983,29 @@ export class RustCrypto extends TypedEventEmitter => { + await this.olmMachine.receiveVerificationEvent( + JSON.stringify({ + event_id: evt.getId(), + type: evt.getWireType(), + sender: evt.getSender(), + state_key: evt.getStateKey(), + content: evt.getContent(), + origin_server_ts: evt.getTs(), + }), + new RustSdkCryptoJs.RoomId(roomId), + ); + }; - await this.olmMachine.receiveVerificationEvent( - JSON.stringify({ - event_id: event.getId(), - type: event.getWireType(), - sender: event.getSender(), - state_key: event.getStateKey(), - content: event.getWireContent(), - origin_server_ts: event.getTs(), - }), - new RustSdkCryptoJs.RoomId(roomId), - ); + // In case we failed to decrypt the event + // We wait for the event to be decrypted + if (event.isDecryptionFailure()) { + event.once(MatrixEventEvent.Decrypted, (decryptedEvent) => { + receiveVerificationEvent(decryptedEvent); + }); + } else { + await receiveVerificationEvent(event); + } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////