2019-11-07 19:39:50 +03:00
|
|
|
/*
|
2020-01-30 23:03:26 +03:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2019-11-07 19:39:50 +03: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';
|
2021-06-21 16:05:56 +03:00
|
|
|
import { MatrixEvent } from 'matrix-js-sdk/src';
|
|
|
|
import { MatrixClientPeg } from '../../../MatrixClientPeg';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2019-11-07 19:39:50 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2021-06-21 16:05:56 +03:00
|
|
|
import { getNameForEventRoom, userLabelForEventRoom }
|
2019-11-07 19:39:50 +03:00
|
|
|
from '../../../utils/KeyVerificationStateObserver';
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from "../../../dispatcher/dispatcher";
|
2021-06-21 16:05:56 +03:00
|
|
|
import { RightPanelPhases } from "../../../stores/RightPanelStorePhases";
|
|
|
|
import { Action } from "../../../dispatcher/actions";
|
2020-11-04 20:00:07 +03:00
|
|
|
import EventTileBubble from "./EventTileBubble";
|
2021-06-21 16:05:56 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2019-11-07 19:39:50 +03:00
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
interface IProps {
|
|
|
|
mxEvent: MatrixEvent
|
|
|
|
}
|
2019-11-07 19:39:50 +03:00
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
@replaceableComponent("views.messages.MKeyVerificationRequest")
|
|
|
|
export default class MKeyVerificationRequest extends React.Component<IProps> {
|
|
|
|
public componentDidMount() {
|
2019-12-10 19:53:51 +03:00
|
|
|
const request = this.props.mxEvent.verificationRequest;
|
|
|
|
if (request) {
|
2021-06-21 16:05:56 +03:00
|
|
|
request.on("change", this.onRequestChanged);
|
2019-12-10 19:53:51 +03:00
|
|
|
}
|
2019-11-07 19:39:50 +03:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
public componentWillUnmount() {
|
2019-12-10 19:53:51 +03:00
|
|
|
const request = this.props.mxEvent.verificationRequest;
|
|
|
|
if (request) {
|
2021-06-21 16:05:56 +03:00
|
|
|
request.off("change", this.onRequestChanged);
|
2019-12-10 19:53:51 +03:00
|
|
|
}
|
2019-11-07 19:39:50 +03:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
private openRequest = () => {
|
|
|
|
const { verificationRequest } = this.props.mxEvent;
|
2020-01-24 19:16:46 +03:00
|
|
|
const member = MatrixClientPeg.get().getUser(verificationRequest.otherUserId);
|
2020-01-03 15:40:20 +03:00
|
|
|
dis.dispatch({
|
2020-07-18 14:08:20 +03:00
|
|
|
action: Action.SetRightPanelPhase,
|
|
|
|
phase: RightPanelPhases.EncryptionPanel,
|
2021-06-29 15:11:58 +03:00
|
|
|
refireParams: { verificationRequest, member },
|
2020-01-03 15:40:20 +03:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
private onRequestChanged = () => {
|
2019-12-10 19:53:51 +03:00
|
|
|
this.forceUpdate();
|
|
|
|
};
|
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
private onAcceptClicked = async () => {
|
2019-12-10 19:53:51 +03:00
|
|
|
const request = this.props.mxEvent.verificationRequest;
|
|
|
|
if (request) {
|
|
|
|
try {
|
2021-06-21 16:05:56 +03:00
|
|
|
this.openRequest();
|
2020-02-25 15:27:19 +03:00
|
|
|
await request.accept();
|
2019-12-10 19:53:51 +03:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 19:39:50 +03:00
|
|
|
};
|
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
private onRejectClicked = async () => {
|
2019-12-10 19:53:51 +03:00
|
|
|
const request = this.props.mxEvent.verificationRequest;
|
|
|
|
if (request) {
|
|
|
|
try {
|
|
|
|
await request.cancel();
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 19:39:50 +03:00
|
|
|
};
|
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
private acceptedLabel(userId: string) {
|
2019-11-07 19:39:50 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const myUserId = client.getUserId();
|
|
|
|
if (userId === myUserId) {
|
|
|
|
return _t("You accepted");
|
|
|
|
} else {
|
2021-06-29 15:11:58 +03:00
|
|
|
return _t("%(name)s accepted", { name: getNameForEventRoom(userId, this.props.mxEvent.getRoomId()) });
|
2019-11-07 19:39:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
private cancelledLabel(userId: string) {
|
2019-11-07 19:39:50 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const myUserId = client.getUserId();
|
2021-06-29 15:11:58 +03:00
|
|
|
const { cancellationCode } = this.props.mxEvent.verificationRequest;
|
2020-02-13 19:29:38 +03:00
|
|
|
const declined = cancellationCode === "m.user";
|
2019-11-07 19:39:50 +03:00
|
|
|
if (userId === myUserId) {
|
2020-02-13 19:29:38 +03:00
|
|
|
if (declined) {
|
|
|
|
return _t("You declined");
|
|
|
|
} else {
|
|
|
|
return _t("You cancelled");
|
|
|
|
}
|
2019-11-07 19:39:50 +03:00
|
|
|
} else {
|
2020-02-13 19:29:38 +03:00
|
|
|
if (declined) {
|
2021-06-29 15:11:58 +03:00
|
|
|
return _t("%(name)s declined", { name: getNameForEventRoom(userId, this.props.mxEvent.getRoomId()) });
|
2020-02-13 19:29:38 +03:00
|
|
|
} else {
|
2021-06-29 15:11:58 +03:00
|
|
|
return _t("%(name)s cancelled", { name: getNameForEventRoom(userId, this.props.mxEvent.getRoomId()) });
|
2020-02-13 19:29:38 +03:00
|
|
|
}
|
2019-11-07 19:39:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:05:56 +03:00
|
|
|
public render() {
|
2020-01-03 15:40:20 +03:00
|
|
|
const AccessibleButton = sdk.getComponent("elements.AccessibleButton");
|
|
|
|
|
2021-06-29 15:11:58 +03:00
|
|
|
const { mxEvent } = this.props;
|
2019-12-10 19:53:51 +03:00
|
|
|
const request = mxEvent.verificationRequest;
|
2019-11-07 19:39:50 +03:00
|
|
|
|
2020-01-03 15:40:20 +03:00
|
|
|
if (!request || request.invalid) {
|
2019-12-20 23:31:36 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-07 19:39:50 +03:00
|
|
|
let title;
|
|
|
|
let subtitle;
|
|
|
|
let stateNode;
|
|
|
|
|
2020-02-13 19:29:38 +03:00
|
|
|
if (!request.canAccept) {
|
2019-11-07 19:39:50 +03:00
|
|
|
let stateLabel;
|
2020-02-13 19:29:38 +03:00
|
|
|
const accepted = request.ready || request.started || request.done;
|
2019-12-20 23:31:53 +03:00
|
|
|
if (accepted) {
|
2021-06-21 16:05:56 +03:00
|
|
|
stateLabel = (<AccessibleButton onClick={this.openRequest}>
|
|
|
|
{this.acceptedLabel(request.receivingUserId)}
|
2020-01-03 15:40:20 +03:00
|
|
|
</AccessibleButton>);
|
2020-02-13 19:29:38 +03:00
|
|
|
} else if (request.cancelled) {
|
2021-06-21 16:05:56 +03:00
|
|
|
stateLabel = this.cancelledLabel(request.cancellingUserId);
|
2020-02-13 19:29:38 +03:00
|
|
|
} else if (request.accepting) {
|
2020-02-25 15:27:59 +03:00
|
|
|
stateLabel = _t("Accepting …");
|
2020-02-13 19:29:38 +03:00
|
|
|
} else if (request.declining) {
|
2020-02-25 15:27:59 +03:00
|
|
|
stateLabel = _t("Declining …");
|
2019-11-07 19:39:50 +03:00
|
|
|
}
|
2020-01-30 23:03:26 +03:00
|
|
|
stateNode = (<div className="mx_cryptoEvent_state">{stateLabel}</div>);
|
2019-11-07 19:39:50 +03:00
|
|
|
}
|
|
|
|
|
2019-12-10 19:53:51 +03:00
|
|
|
if (!request.initiatedByMe) {
|
2020-01-24 14:08:47 +03:00
|
|
|
const name = getNameForEventRoom(request.requestingUserId, mxEvent.getRoomId());
|
2021-06-29 15:11:58 +03:00
|
|
|
title = _t("%(name)s wants to verify", { name });
|
2020-11-04 20:00:07 +03:00
|
|
|
subtitle = userLabelForEventRoom(request.requestingUserId, mxEvent.getRoomId());
|
2020-02-13 19:29:13 +03:00
|
|
|
if (request.canAccept) {
|
2020-01-30 23:03:26 +03:00
|
|
|
stateNode = (<div className="mx_cryptoEvent_buttons">
|
2021-06-21 16:16:37 +03:00
|
|
|
<AccessibleButton kind="danger" onClick={this.onRejectClicked}>
|
|
|
|
{_t("Decline")}
|
|
|
|
</AccessibleButton>
|
|
|
|
<AccessibleButton onClick={this.onAcceptClicked}>
|
|
|
|
{_t("Accept")}
|
|
|
|
</AccessibleButton>
|
2019-11-07 19:39:50 +03:00
|
|
|
</div>);
|
|
|
|
}
|
2019-12-10 19:53:51 +03:00
|
|
|
} else { // request sent by us
|
2020-11-04 20:00:07 +03:00
|
|
|
title = _t("You sent a verification request");
|
|
|
|
subtitle = userLabelForEventRoom(request.receivingUserId, mxEvent.getRoomId());
|
2019-11-07 19:39:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (title) {
|
2020-11-04 20:00:07 +03:00
|
|
|
return <EventTileBubble
|
|
|
|
className="mx_cryptoEvent mx_cryptoEvent_icon"
|
|
|
|
title={title}
|
|
|
|
subtitle={subtitle}
|
|
|
|
>
|
|
|
|
{ stateNode }
|
|
|
|
</EventTileBubble>;
|
2019-11-07 19:39:50 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|