2019-11-07 17:40:22 +01:00
|
|
|
/*
|
2020-01-30 20:03:26 +00:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2019-11-07 17:40:22 +01:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from "react";
|
|
|
|
|
import classNames from "classnames";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2023-06-14 15:35:32 +01:00
|
|
|
import { VerificationPhase, VerificationRequest, VerificationRequestEvent } from "matrix-js-sdk/src/crypto-api";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { EventType } from "matrix-js-sdk/src/@types/event";
|
2022-02-22 12:18:08 +00:00
|
|
|
import { CryptoEvent } from "matrix-js-sdk/src/crypto";
|
2021-10-22 17:23:32 -05:00
|
|
|
|
2021-06-29 13:11:58 +01:00
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
2019-11-07 17:40:22 +01:00
|
|
|
import { _t } from "../../../languageHandler";
|
2021-09-24 20:04:03 +02:00
|
|
|
import { getNameForEventRoom, userLabelForEventRoom } from "../../../utils/KeyVerificationStateObserver";
|
2020-11-04 17:00:07 +00:00
|
|
|
import EventTileBubble from "./EventTileBubble";
|
2021-09-24 20:04:03 +02:00
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
|
/* the MatrixEvent to show */
|
|
|
|
|
mxEvent: MatrixEvent;
|
2022-01-25 13:10:17 +00:00
|
|
|
timestamp?: JSX.Element;
|
2021-09-24 20:04:03 +02:00
|
|
|
}
|
2019-11-07 17:40:22 +01:00
|
|
|
|
2021-09-24 20:04:03 +02:00
|
|
|
export default class MKeyVerificationConclusion extends React.Component<IProps> {
|
2022-12-16 12:29:59 +00:00
|
|
|
public constructor(props: IProps) {
|
2019-11-07 17:40:22 +01:00
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-24 20:04:03 +02:00
|
|
|
public componentDidMount(): void {
|
2019-12-20 11:24:10 +00:00
|
|
|
const request = this.props.mxEvent.verificationRequest;
|
|
|
|
|
if (request) {
|
2022-02-22 12:18:08 +00:00
|
|
|
request.on(VerificationRequestEvent.Change, this.onRequestChanged);
|
2019-11-07 17:40:22 +01:00
|
|
|
}
|
2022-02-22 12:18:08 +00:00
|
|
|
MatrixClientPeg.get().on(CryptoEvent.UserTrustStatusChanged, this.onTrustChanged);
|
2019-11-07 17:40:22 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-24 20:04:03 +02:00
|
|
|
public componentWillUnmount(): void {
|
2019-12-20 11:24:10 +00:00
|
|
|
const request = this.props.mxEvent.verificationRequest;
|
|
|
|
|
if (request) {
|
2022-02-22 12:18:08 +00:00
|
|
|
request.off(VerificationRequestEvent.Change, this.onRequestChanged);
|
2019-11-07 17:40:22 +01:00
|
|
|
}
|
2020-02-04 23:13:55 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
|
if (cli) {
|
2022-02-22 12:18:08 +00:00
|
|
|
cli.removeListener(CryptoEvent.UserTrustStatusChanged, this.onTrustChanged);
|
2020-02-04 23:13:55 +00:00
|
|
|
}
|
2019-11-07 17:40:22 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-27 10:12:55 +02:00
|
|
|
private onRequestChanged = (): void => {
|
2019-12-20 11:24:10 +00:00
|
|
|
this.forceUpdate();
|
|
|
|
|
};
|
2019-11-07 17:40:22 +01:00
|
|
|
|
2021-09-24 20:04:03 +02:00
|
|
|
private onTrustChanged = (userId: string): void => {
|
2020-02-04 11:25:19 +00:00
|
|
|
const { mxEvent } = this.props;
|
|
|
|
|
const request = mxEvent.verificationRequest;
|
|
|
|
|
if (!request || request.otherUserId !== userId) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.forceUpdate();
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-03 15:27:47 +00:00
|
|
|
public static shouldRender(mxEvent: MatrixEvent, request?: VerificationRequest): boolean {
|
2020-01-03 13:40:20 +01:00
|
|
|
// normally should not happen
|
|
|
|
|
if (!request) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// .cancel event that was sent after the verification finished, ignore
|
2023-06-06 09:27:53 +01:00
|
|
|
if (mxEvent.getType() === EventType.KeyVerificationCancel && request.phase !== VerificationPhase.Cancelled) {
|
2020-01-03 13:40:20 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// .done event that was sent after the verification cancelled, ignore
|
2023-06-06 09:27:53 +01:00
|
|
|
if (mxEvent.getType() === EventType.KeyVerificationDone && request.phase !== VerificationPhase.Done) {
|
2020-01-03 13:40:20 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// request hasn't concluded yet
|
|
|
|
|
if (request.pending) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-01-31 10:37:40 +00:00
|
|
|
|
|
|
|
|
// User isn't actually verified
|
2021-04-29 19:57:02 +02:00
|
|
|
if (!MatrixClientPeg.get().checkUserTrust(request.otherUserId).isCrossSigningVerified()) {
|
2020-01-31 10:37:40 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-03 13:40:20 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 15:27:47 +00:00
|
|
|
public render(): JSX.Element | null {
|
2021-06-29 13:11:58 +01:00
|
|
|
const { mxEvent } = this.props;
|
2023-02-03 15:27:47 +00:00
|
|
|
const request = mxEvent.verificationRequest!;
|
2019-12-20 21:31:36 +01:00
|
|
|
|
2021-09-24 20:04:03 +02:00
|
|
|
if (!MKeyVerificationConclusion.shouldRender(mxEvent, request)) {
|
2019-12-20 21:31:36 +01:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 17:40:22 +01:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
const myUserId = client.getUserId();
|
2019-12-20 11:24:10 +00:00
|
|
|
|
2023-02-03 15:27:47 +00:00
|
|
|
let title: string | undefined;
|
2019-11-07 17:40:22 +01:00
|
|
|
|
2023-06-06 09:27:53 +01:00
|
|
|
if (request.phase === VerificationPhase.Done) {
|
2021-09-27 10:35:33 +02:00
|
|
|
title = _t("You verified %(name)s", {
|
2023-05-23 16:24:12 +01:00
|
|
|
name: getNameForEventRoom(client, request.otherUserId, mxEvent.getRoomId()!),
|
2021-09-27 10:35:33 +02:00
|
|
|
});
|
2023-06-06 09:27:53 +01:00
|
|
|
} else if (request.phase === VerificationPhase.Cancelled) {
|
2020-01-03 13:40:20 +01:00
|
|
|
const userId = request.cancellingUserId;
|
|
|
|
|
if (userId === myUserId) {
|
2019-11-07 20:04:36 +01:00
|
|
|
title = _t("You cancelled verifying %(name)s", {
|
2023-05-23 16:24:12 +01:00
|
|
|
name: getNameForEventRoom(client, request.otherUserId, mxEvent.getRoomId()!),
|
2021-09-27 10:35:33 +02:00
|
|
|
});
|
2023-03-29 08:22:35 +01:00
|
|
|
} else if (userId) {
|
2023-05-23 16:24:12 +01:00
|
|
|
title = _t("%(name)s cancelled verifying", {
|
|
|
|
|
name: getNameForEventRoom(client, userId, mxEvent.getRoomId()!),
|
|
|
|
|
});
|
2019-11-07 17:40:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (title) {
|
2020-11-04 17:00:07 +00:00
|
|
|
const classes = classNames("mx_cryptoEvent mx_cryptoEvent_icon", {
|
2023-06-06 09:27:53 +01:00
|
|
|
mx_cryptoEvent_icon_verified: request.phase === VerificationPhase.Done,
|
2019-11-07 17:40:22 +01:00
|
|
|
});
|
2020-11-04 17:00:07 +00:00
|
|
|
return (
|
|
|
|
|
<EventTileBubble
|
|
|
|
|
className={classes}
|
|
|
|
|
title={title}
|
2023-05-23 16:24:12 +01:00
|
|
|
subtitle={userLabelForEventRoom(client, request.otherUserId, mxEvent.getRoomId()!)}
|
2022-01-25 13:10:17 +00:00
|
|
|
timestamp={this.props.timestamp}
|
2020-11-04 17:00:07 +00:00
|
|
|
/>
|
|
|
|
|
);
|
2019-11-07 17:40:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|