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

Commit 4c73903

Browse files
authored
Use getShowSasCallbacks() and getReciprocateQrCodeCallbacks() (#11015)
* Use `getShowSasCallbacks()` and `getShowQrCodeCallbacks()` ... instead of type-casting * Update method names These methods got renamed in the js-sdk PR * Fix strict typing errors
1 parent b2452a4 commit 4c73903

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/components/views/right_panel/VerificationPanel.tsx

+12-12
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ limitations under the License.
1616

1717
import React from "react";
1818
import { verificationMethods } from "matrix-js-sdk/src/crypto";
19-
import { ReciprocateQRCode, SCAN_QR_CODE_METHOD } from "matrix-js-sdk/src/crypto/verification/QRCode";
19+
import { SCAN_QR_CODE_METHOD } from "matrix-js-sdk/src/crypto/verification/QRCode";
2020
import {
2121
Phase,
2222
VerificationRequest,
2323
VerificationRequestEvent,
2424
} from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
2525
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
2626
import { User } from "matrix-js-sdk/src/models/user";
27-
import { SAS } from "matrix-js-sdk/src/crypto/verification/SAS";
2827
import { logger } from "matrix-js-sdk/src/logger";
2928
import { DeviceInfo } from "matrix-js-sdk/src/crypto/deviceinfo";
3029
import { ShowQrCodeCallbacks, ShowSasCallbacks, VerifierEvent } from "matrix-js-sdk/src/crypto-api/verification";
@@ -49,18 +48,18 @@ interface IProps {
4948
}
5049

5150
interface IState {
52-
sasEvent?: ShowSasCallbacks;
51+
sasEvent: ShowSasCallbacks | null;
5352
emojiButtonClicked?: boolean;
5453
reciprocateButtonClicked?: boolean;
55-
reciprocateQREvent?: ShowQrCodeCallbacks;
54+
reciprocateQREvent: ShowQrCodeCallbacks | null;
5655
}
5756

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

6160
public constructor(props: IProps) {
6261
super(props);
63-
this.state = {};
62+
this.state = { sasEvent: null, reciprocateQREvent: null };
6463
this.hasVerifier = false;
6564
}
6665

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

401400
private updateVerifierState = (): void => {
402-
const { request } = this.props;
403-
const sasEvent = (request.verifier as SAS).sasEvent;
404-
const reciprocateQREvent = (request.verifier as ReciprocateQRCode).reciprocateQREvent;
405-
request.verifier?.off(VerifierEvent.ShowSas, this.updateVerifierState);
406-
request.verifier?.off(VerifierEvent.ShowReciprocateQr, this.updateVerifierState);
401+
// this method is only called once we know there is a verifier.
402+
const verifier = this.props.request.verifier!;
403+
const sasEvent = verifier.getShowSasCallbacks();
404+
const reciprocateQREvent = verifier.getReciprocateQrCodeCallbacks();
405+
verifier.off(VerifierEvent.ShowSas, this.updateVerifierState);
406+
verifier.off(VerifierEvent.ShowReciprocateQr, this.updateVerifierState);
407407
this.setState({ sasEvent, reciprocateQREvent });
408408
};
409409

@@ -428,8 +428,8 @@ export default class VerificationPanel extends React.PureComponent<IProps, IStat
428428
const { request } = this.props;
429429
request.on(VerificationRequestEvent.Change, this.onRequestChange);
430430
if (request.verifier) {
431-
const sasEvent = (request.verifier as SAS).sasEvent;
432-
const reciprocateQREvent = (request.verifier as ReciprocateQRCode).reciprocateQREvent;
431+
const sasEvent = request.verifier.getShowSasCallbacks();
432+
const reciprocateQREvent = request.verifier.getReciprocateQrCodeCallbacks();
433433
this.setState({ sasEvent, reciprocateQREvent });
434434
}
435435
this.onRequestChange();

test/components/views/right_panel/VerificationPanel-test.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {
3131
VerifierEvent,
3232
VerifierEventHandlerMap,
3333
} from "matrix-js-sdk/src/crypto-api/verification";
34-
import { SAS } from "matrix-js-sdk/src/crypto/verification/SAS";
3534
import { IVerificationChannel } from "matrix-js-sdk/src/crypto/verification/request/Channel";
3635

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

7978
// fire the ShowSas event
8079
const sasEvent = makeMockSasCallbacks();
81-
(mockVerifier as unknown as SAS).sasEvent = sasEvent;
80+
mockVerifier.getShowSasCallbacks.mockReturnValue(sasEvent);
8281
act(() => {
8382
mockVerifier.emit(VerifierEvent.ShowSas, sasEvent);
8483
});
@@ -119,6 +118,8 @@ function makeMockVerifier(): Mocked<VerificationBase> {
119118
Object.assign(verifier, {
120119
cancel: jest.fn(),
121120
verify: jest.fn(),
121+
getShowSasCallbacks: jest.fn(),
122+
getReciprocateQrCodeCallbacks: jest.fn(),
122123
});
123124
return verifier as unknown as Mocked<VerificationBase>;
124125
}

0 commit comments

Comments
 (0)