Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Use getShowSasCallbacks() and getReciprocateQrCodeCallbacks() (#1…
Browse files Browse the repository at this point in the history
…1015)

* Use `getShowSasCallbacks()` and `getShowQrCodeCallbacks()`

... instead of type-casting

* Update method names

These methods got renamed in the js-sdk PR

* Fix strict typing errors
  • Loading branch information
richvdh authored Jun 7, 2023
1 parent b2452a4 commit 4c73903
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
24 changes: 12 additions & 12 deletions src/components/views/right_panel/VerificationPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ limitations under the License.

import React from "react";
import { verificationMethods } from "matrix-js-sdk/src/crypto";
import { ReciprocateQRCode, SCAN_QR_CODE_METHOD } from "matrix-js-sdk/src/crypto/verification/QRCode";
import { SCAN_QR_CODE_METHOD } from "matrix-js-sdk/src/crypto/verification/QRCode";
import {
Phase,
VerificationRequest,
VerificationRequestEvent,
} from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { User } from "matrix-js-sdk/src/models/user";
import { SAS } from "matrix-js-sdk/src/crypto/verification/SAS";
import { logger } from "matrix-js-sdk/src/logger";
import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo";
import { ShowQrCodeCallbacks, ShowSasCallbacks, VerifierEvent } from "matrix-js-sdk/src/crypto-api/verification";
Expand All @@ -49,18 +48,18 @@ interface IProps {
}

interface IState {
sasEvent?: ShowSasCallbacks;
sasEvent: ShowSasCallbacks | null;
emojiButtonClicked?: boolean;
reciprocateButtonClicked?: boolean;
reciprocateQREvent?: ShowQrCodeCallbacks;
reciprocateQREvent: ShowQrCodeCallbacks | null;
}

export default class VerificationPanel extends React.PureComponent<IProps, IState> {
private hasVerifier: boolean;

public constructor(props: IProps) {
super(props);
this.state = {};
this.state = { sasEvent: null, reciprocateQREvent: null };
this.hasVerifier = false;
}

Expand Down Expand Up @@ -399,11 +398,12 @@ export default class VerificationPanel extends React.PureComponent<IProps, IStat
};

private updateVerifierState = (): void => {
const { request } = this.props;
const sasEvent = (request.verifier as SAS).sasEvent;
const reciprocateQREvent = (request.verifier as ReciprocateQRCode).reciprocateQREvent;
request.verifier?.off(VerifierEvent.ShowSas, this.updateVerifierState);
request.verifier?.off(VerifierEvent.ShowReciprocateQr, this.updateVerifierState);
// this method is only called once we know there is a verifier.
const verifier = this.props.request.verifier!;
const sasEvent = verifier.getShowSasCallbacks();
const reciprocateQREvent = verifier.getReciprocateQrCodeCallbacks();
verifier.off(VerifierEvent.ShowSas, this.updateVerifierState);
verifier.off(VerifierEvent.ShowReciprocateQr, this.updateVerifierState);
this.setState({ sasEvent, reciprocateQREvent });
};

Expand All @@ -428,8 +428,8 @@ export default class VerificationPanel extends React.PureComponent<IProps, IStat
const { request } = this.props;
request.on(VerificationRequestEvent.Change, this.onRequestChange);
if (request.verifier) {
const sasEvent = (request.verifier as SAS).sasEvent;
const reciprocateQREvent = (request.verifier as ReciprocateQRCode).reciprocateQREvent;
const sasEvent = request.verifier.getShowSasCallbacks();
const reciprocateQREvent = request.verifier.getReciprocateQrCodeCallbacks();
this.setState({ sasEvent, reciprocateQREvent });
}
this.onRequestChange();
Expand Down
5 changes: 3 additions & 2 deletions test/components/views/right_panel/VerificationPanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
VerifierEvent,
VerifierEventHandlerMap,
} from "matrix-js-sdk/src/crypto-api/verification";
import { SAS } from "matrix-js-sdk/src/crypto/verification/SAS";
import { IVerificationChannel } from "matrix-js-sdk/src/crypto/verification/request/Channel";

import VerificationPanel from "../../../../src/components/views/right_panel/VerificationPanel";
Expand Down Expand Up @@ -78,7 +77,7 @@ describe("<VerificationPanel />", () => {

// fire the ShowSas event
const sasEvent = makeMockSasCallbacks();
(mockVerifier as unknown as SAS).sasEvent = sasEvent;
mockVerifier.getShowSasCallbacks.mockReturnValue(sasEvent);
act(() => {
mockVerifier.emit(VerifierEvent.ShowSas, sasEvent);
});
Expand Down Expand Up @@ -119,6 +118,8 @@ function makeMockVerifier(): Mocked<VerificationBase> {
Object.assign(verifier, {
cancel: jest.fn(),
verify: jest.fn(),
getShowSasCallbacks: jest.fn(),
getReciprocateQrCodeCallbacks: jest.fn(),
});
return verifier as unknown as Mocked<VerificationBase>;
}
Expand Down

0 comments on commit 4c73903

Please sign in to comment.