element-web/src/components/views/messages/MKeyVerificationRequest.js

154 lines
5.2 KiB
JavaScript
Raw Normal View History

/*
Copyright 2019 The Matrix.org Foundation C.I.C.
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 PropTypes from 'prop-types';
import MatrixClientPeg from '../../../MatrixClientPeg';
import sdk from '../../../index';
import { _t } from '../../../languageHandler';
2019-12-10 19:53:51 +03:00
import {getNameForEventRoom, userLabelForEventRoom}
from '../../../utils/KeyVerificationStateObserver';
2019-12-10 19:53:51 +03:00
import dis from "../../../dispatcher";
import {RIGHT_PANEL_PHASES} from "../../../stores/RightPanelStorePhases";
export default class MKeyVerificationRequest extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
2019-12-10 19:53:51 +03:00
const request = this.props.mxEvent.verificationRequest;
if (request) {
request.on("change", this._onRequestChanged);
}
}
componentWillUnmount() {
2019-12-10 19:53:51 +03:00
const request = this.props.mxEvent.verificationRequest;
if (request) {
request.off("change", this._onRequestChanged);
}
}
2019-12-10 19:53:51 +03:00
_onRequestChanged = () => {
this.forceUpdate();
};
_onAcceptClicked = async () => {
const request = this.props.mxEvent.verificationRequest;
if (request) {
try {
await request.accept();
dis.dispatch({action: "show_right_panel"});
dis.dispatch({
action: "set_right_panel_phase",
phase: RIGHT_PANEL_PHASES.EncryptionPanel,
2019-12-19 19:08:11 +03:00
refireParams: {verificationRequest: request},
2019-12-10 19:53:51 +03:00
});
} catch (err) {
console.error(err.message);
}
}
};
2019-12-10 19:53:51 +03:00
_onRejectClicked = async () => {
const request = this.props.mxEvent.verificationRequest;
if (request) {
try {
await request.cancel();
} catch (err) {
console.error(err.message);
}
}
};
_acceptedLabel(userId) {
const client = MatrixClientPeg.get();
const myUserId = client.getUserId();
if (userId === myUserId) {
return _t("You accepted");
} else {
return _t("%(name)s accepted", {name: getNameForEventRoom(userId, this.props.mxEvent)});
}
}
_cancelledLabel(userId) {
const client = MatrixClientPeg.get();
const myUserId = client.getUserId();
if (userId === myUserId) {
return _t("You cancelled");
} else {
return _t("%(name)s cancelled", {name: getNameForEventRoom(userId, this.props.mxEvent)});
}
}
render() {
const {mxEvent} = this.props;
2019-12-10 19:53:51 +03:00
const request = mxEvent.verificationRequest;
let title;
let subtitle;
let stateNode;
2019-12-10 19:53:51 +03:00
if (!request) {
2019-12-20 14:24:27 +03:00
return <p>This is a verification request tile without a request.</p>;
2019-12-10 19:53:51 +03:00
}
if (request.ready || request.started || request.cancelled) {
let stateLabel;
2019-12-10 19:53:51 +03:00
if (request.ready || request.started) {
stateLabel = this._acceptedLabel(request.receivingUserId);
} else if (request.cancelled) {
stateLabel = this._cancelledLabel(request.cancellingUserId);
}
stateNode = (<div className="mx_KeyVerification_state">{stateLabel}</div>);
}
2019-12-10 19:53:51 +03:00
if (!request.initiatedByMe) {
title = (<div className="mx_KeyVerification_title">{
2019-12-10 19:53:51 +03:00
_t("%(name)s wants to verify", {name: getNameForEventRoom(request.requestingUserId, mxEvent)})}</div>);
subtitle = (<div className="mx_KeyVerification_subtitle">{
2019-12-10 19:53:51 +03:00
userLabelForEventRoom(request.requestingUserId, mxEvent)}</div>);
if (request.requested) {
const FormButton = sdk.getComponent("elements.FormButton");
stateNode = (<div className="mx_KeyVerification_buttons">
<FormButton kind="danger" onClick={this._onRejectClicked} label={_t("Decline")} />
<FormButton onClick={this._onAcceptClicked} label={_t("Accept")} />
</div>);
}
2019-12-10 19:53:51 +03:00
} else { // request sent by us
title = (<div className="mx_KeyVerification_title">{
_t("You sent a verification request")}</div>);
subtitle = (<div className="mx_KeyVerification_subtitle">{
2019-12-10 19:53:51 +03:00
userLabelForEventRoom(request.receivingUserId, mxEvent)}</div>);
}
if (title) {
return (<div className="mx_EventTile_bubble mx_KeyVerification mx_KeyVerification_icon">
{title}
{subtitle}
{stateNode}
</div>);
}
return null;
}
}
MKeyVerificationRequest.propTypes = {
/* the MatrixEvent to show */
mxEvent: PropTypes.object.isRequired,
};