Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Jul 26, 2023
1 parent 19d7ee4 commit 16adc86
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion spec/unit/rust-crypto/rust-crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 0 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2248,8 +2248,6 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa

// attach the event listeners needed by RustCrypto
this.on(RoomMemberEvent.Membership, rustCrypto.onRoomMembership.bind(rustCrypto));
// listen only key verification request
// other events are processed during the /sync
this.on(ClientEvent.Event, (event) => {
rustCrypto.onLiveEventFromSync(event);
});
Expand Down
2 changes: 1 addition & 1 deletion src/crypto-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/verification/request/InRoomChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
42 changes: 27 additions & 15 deletions src/rust-crypto/rust-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -97,6 +97,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv

/** The local user's Device ID. */
_deviceId: string,

/** Interface to server-side secret storage */
private readonly secretStorage: ServerSideSecretStorage,
/** Crypto callbacks provided by the application */
Expand Down Expand Up @@ -954,16 +955,16 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
}

/**
* Handle the live event received via /sync.
* Handle a live event received via /sync.
* See {@link ClientEventHandlerMap#event}
*
* Event is ignored if not a key validation request.
*
* @param event - live event
*/
public async onLiveEventFromSync(event: MatrixEvent): Promise<void> {
// Process only key validation request
// and ignore state event
if (
!event.isState() &&
event.getType() === EventType.RoomMessage &&
event.getContent().msgtype === MsgType.KeyVerificationRequest
) {
Expand All @@ -982,18 +983,29 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
if (!roomId) {
throw new Error("missing roomId in the event");
}
const receiveVerificationEvent = async (evt: MatrixEvent): Promise<void> => {
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);
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 16adc86

Please sign in to comment.