2018-04-12 01:58:04 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
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';
|
2018-04-20 11:23:27 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2018-06-23 19:41:50 +03:00
|
|
|
import {EventStatus} from 'matrix-js-sdk';
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2018-04-13 02:43:44 +03:00
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
|
|
|
import dis from '../../../dispatcher';
|
|
|
|
import sdk from '../../../index';
|
|
|
|
import { _t } from '../../../languageHandler';
|
|
|
|
import Modal from '../../../Modal';
|
|
|
|
import Resend from '../../../Resend';
|
|
|
|
import SettingsStore from '../../../settings/SettingsStore';
|
|
|
|
import { isUrlPermitted } from '../../../HtmlUtils';
|
2019-05-01 15:58:32 +03:00
|
|
|
import { isContentActionable } from '../../../utils/EventUtils';
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'MessageContextMenu',
|
|
|
|
|
|
|
|
propTypes: {
|
|
|
|
/* the MatrixEvent associated with the context menu */
|
2018-04-20 11:23:27 +03:00
|
|
|
mxEvent: PropTypes.object.isRequired,
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
/* an optional EventTileOps implementation that can be used to unhide preview widgets */
|
2018-04-20 11:23:27 +03:00
|
|
|
eventTileOps: PropTypes.object,
|
|
|
|
|
2018-04-27 13:26:42 +03:00
|
|
|
/* an optional function to be called when the user clicks collapse thread, if not provided hide button */
|
2018-04-20 11:23:27 +03:00
|
|
|
collapseReplyThread: PropTypes.func,
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
/* callback called when the menu is dismissed */
|
2018-04-20 11:23:27 +03:00
|
|
|
onFinished: PropTypes.func,
|
2018-04-12 01:58:04 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
canRedact: false,
|
|
|
|
canPin: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillMount: function() {
|
|
|
|
MatrixClientPeg.get().on('RoomMember.powerLevel', this._checkPermissions);
|
|
|
|
this._checkPermissions();
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount: function() {
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
if (cli) {
|
|
|
|
cli.removeListener('RoomMember.powerLevel', this._checkPermissions);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_checkPermissions: function() {
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const room = cli.getRoom(this.props.mxEvent.getRoomId());
|
|
|
|
|
|
|
|
const canRedact = room.currentState.maySendRedactionForEvent(this.props.mxEvent, cli.credentials.userId);
|
|
|
|
let canPin = room.currentState.mayClientSendStateEvent('m.room.pinned_events', cli);
|
|
|
|
|
|
|
|
// HACK: Intentionally say we can't pin if the user doesn't want to use the functionality
|
|
|
|
if (!SettingsStore.isFeatureEnabled("feature_pinning")) canPin = false;
|
|
|
|
|
|
|
|
this.setState({canRedact, canPin});
|
|
|
|
},
|
|
|
|
|
|
|
|
_isPinned: function() {
|
|
|
|
const room = MatrixClientPeg.get().getRoom(this.props.mxEvent.getRoomId());
|
|
|
|
const pinnedEvent = room.currentState.getStateEvents('m.room.pinned_events', '');
|
|
|
|
if (!pinnedEvent) return false;
|
|
|
|
return pinnedEvent.getContent().pinned.includes(this.props.mxEvent.getId());
|
|
|
|
},
|
|
|
|
|
|
|
|
onResendClick: function() {
|
|
|
|
Resend.resend(this.props.mxEvent);
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
2019-02-12 21:10:42 +03:00
|
|
|
e2eInfoClicked: function() {
|
|
|
|
this.props.e2eInfoCallback();
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
onViewSourceClick: function() {
|
|
|
|
const ViewSource = sdk.getComponent('structures.ViewSource');
|
|
|
|
Modal.createTrackedDialog('View Event Source', '', ViewSource, {
|
2019-02-24 06:43:44 +03:00
|
|
|
roomId: this.props.mxEvent.getRoomId(),
|
|
|
|
eventId: this.props.mxEvent.getId(),
|
2018-04-12 01:58:04 +03:00
|
|
|
content: this.props.mxEvent.event,
|
|
|
|
}, 'mx_Dialog_viewsource');
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
|
|
|
onViewClearSourceClick: function() {
|
|
|
|
const ViewSource = sdk.getComponent('structures.ViewSource');
|
|
|
|
Modal.createTrackedDialog('View Clear Event Source', '', ViewSource, {
|
2019-02-24 06:43:44 +03:00
|
|
|
roomId: this.props.mxEvent.getRoomId(),
|
|
|
|
eventId: this.props.mxEvent.getId(),
|
2018-04-12 01:58:04 +03:00
|
|
|
// FIXME: _clearEvent is private
|
|
|
|
content: this.props.mxEvent._clearEvent,
|
|
|
|
}, 'mx_Dialog_viewsource');
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
|
|
|
onRedactClick: function() {
|
|
|
|
const ConfirmRedactDialog = sdk.getComponent("dialogs.ConfirmRedactDialog");
|
|
|
|
Modal.createTrackedDialog('Confirm Redact Dialog', '', ConfirmRedactDialog, {
|
2019-06-04 14:10:17 +03:00
|
|
|
onFinished: async (proceed) => {
|
2018-04-12 01:58:04 +03:00
|
|
|
if (!proceed) return;
|
|
|
|
|
|
|
|
const cli = MatrixClientPeg.get();
|
2019-06-04 14:10:17 +03:00
|
|
|
try {
|
|
|
|
await cli.redactEvent(
|
|
|
|
this.props.mxEvent.getRoomId(),
|
|
|
|
this.props.mxEvent.getId(),
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2018-04-12 01:58:04 +03:00
|
|
|
const code = e.errcode || e.statusCode;
|
2019-06-04 14:10:17 +03:00
|
|
|
// only show the dialog if failing for something other than a network error
|
2019-06-04 17:53:01 +03:00
|
|
|
// (e.g. no errcode or statusCode) as in that case the redactions end up in the
|
2019-06-04 17:11:49 +03:00
|
|
|
// detached queue and we show the room status bar to allow retry
|
2019-06-04 14:10:17 +03:00
|
|
|
if (typeof code !== "undefined") {
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
// display error message stating you couldn't delete this.
|
|
|
|
Modal.createTrackedDialog('You cannot delete this message', '', ErrorDialog, {
|
|
|
|
title: _t('Error'),
|
|
|
|
description: _t('You cannot delete this message. (%(code)s)', {code}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
},
|
|
|
|
}, 'mx_Dialog_confirmredact');
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
|
|
|
onCancelSendClick: function() {
|
|
|
|
Resend.removeFromQueue(this.props.mxEvent);
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
|
|
|
onForwardClick: function() {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'forward_event',
|
|
|
|
event: this.props.mxEvent,
|
|
|
|
});
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
|
|
|
onPinClick: function() {
|
|
|
|
MatrixClientPeg.get().getStateEvent(this.props.mxEvent.getRoomId(), 'm.room.pinned_events', '')
|
|
|
|
.catch((e) => {
|
|
|
|
// Intercept the Event Not Found error and fall through the promise chain with no event.
|
|
|
|
if (e.errcode === "M_NOT_FOUND") return null;
|
|
|
|
throw e;
|
|
|
|
})
|
|
|
|
.then((event) => {
|
|
|
|
const eventIds = (event ? event.pinned : []) || [];
|
|
|
|
if (!eventIds.includes(this.props.mxEvent.getId())) {
|
|
|
|
// Not pinned - add
|
|
|
|
eventIds.push(this.props.mxEvent.getId());
|
|
|
|
} else {
|
|
|
|
// Pinned - remove
|
|
|
|
eventIds.splice(eventIds.indexOf(this.props.mxEvent.getId()), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
cli.sendStateEvent(this.props.mxEvent.getRoomId(), 'm.room.pinned_events', {pinned: eventIds}, '');
|
|
|
|
});
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
|
|
|
closeMenu: function() {
|
|
|
|
if (this.props.onFinished) this.props.onFinished();
|
|
|
|
},
|
|
|
|
|
|
|
|
onUnhidePreviewClick: function() {
|
|
|
|
if (this.props.eventTileOps) {
|
|
|
|
this.props.eventTileOps.unhideWidget();
|
|
|
|
}
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
|
|
|
onQuoteClick: function() {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'quote',
|
2018-05-21 05:48:59 +03:00
|
|
|
event: this.props.mxEvent,
|
2018-04-12 01:58:04 +03:00
|
|
|
});
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
2018-06-14 15:35:35 +03:00
|
|
|
onPermalinkClick: function(e: Event) {
|
|
|
|
e.preventDefault();
|
|
|
|
const ShareDialog = sdk.getComponent("dialogs.ShareDialog");
|
|
|
|
Modal.createTrackedDialog('share room message dialog', '', ShareDialog, {
|
|
|
|
target: this.props.mxEvent,
|
2019-02-25 17:35:09 +03:00
|
|
|
permalinkCreator: this.props.permalinkCreator,
|
2018-06-14 15:35:35 +03:00
|
|
|
});
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
2018-04-20 11:23:27 +03:00
|
|
|
onCollapseReplyThreadClick: function() {
|
|
|
|
this.props.collapseReplyThread();
|
|
|
|
this.closeMenu();
|
|
|
|
},
|
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
render: function() {
|
2019-02-24 05:03:20 +03:00
|
|
|
const mxEvent = this.props.mxEvent;
|
|
|
|
const eventStatus = mxEvent.status;
|
2018-04-12 01:58:04 +03:00
|
|
|
let resendButton;
|
|
|
|
let redactButton;
|
|
|
|
let cancelButton;
|
|
|
|
let forwardButton;
|
|
|
|
let pinButton;
|
|
|
|
let viewClearSourceButton;
|
|
|
|
let unhidePreviewButton;
|
|
|
|
let externalURLButton;
|
|
|
|
let quoteButton;
|
2018-04-20 11:23:27 +03:00
|
|
|
let collapseReplyThread;
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2018-06-23 19:41:50 +03:00
|
|
|
// status is SENT before remote-echo, null after
|
|
|
|
const isSent = !eventStatus || eventStatus === EventStatus.SENT;
|
|
|
|
|
|
|
|
if (eventStatus === EventStatus.NOT_SENT) {
|
2018-04-12 01:58:04 +03:00
|
|
|
resendButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onResendClick}>
|
|
|
|
{ _t('Resend') }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-23 19:41:50 +03:00
|
|
|
if (isSent && this.state.canRedact) {
|
2018-04-12 01:58:04 +03:00
|
|
|
redactButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onRedactClick}>
|
|
|
|
{ _t('Remove') }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-23 19:41:50 +03:00
|
|
|
if (eventStatus === EventStatus.QUEUED || eventStatus === EventStatus.NOT_SENT) {
|
2018-04-12 01:58:04 +03:00
|
|
|
cancelButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onCancelSendClick}>
|
|
|
|
{ _t('Cancel Sending') }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-05-01 15:58:32 +03:00
|
|
|
if (isContentActionable(mxEvent)) {
|
|
|
|
forwardButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onForwardClick}>
|
|
|
|
{ _t('Forward Message') }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.state.canPin) {
|
|
|
|
pinButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onPinClick}>
|
|
|
|
{ this._isPinned() ? _t('Unpin Message') : _t('Pin Message') }
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const viewSourceButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onViewSourceClick}>
|
|
|
|
{ _t('View Source') }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2019-02-24 05:03:20 +03:00
|
|
|
if (mxEvent.getType() !== mxEvent.getWireType()) {
|
2018-04-12 01:58:04 +03:00
|
|
|
viewClearSourceButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onViewClearSourceClick}>
|
|
|
|
{ _t('View Decrypted Source') }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.eventTileOps) {
|
|
|
|
if (this.props.eventTileOps.isWidgetHidden()) {
|
|
|
|
unhidePreviewButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onUnhidePreviewClick}>
|
|
|
|
{ _t('Unhide Preview') }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 17:35:09 +03:00
|
|
|
let permalink;
|
|
|
|
if (this.props.permalinkCreator) {
|
2019-03-11 18:02:33 +03:00
|
|
|
permalink = this.props.permalinkCreator.forEvent(this.props.mxEvent.getId());
|
2019-02-25 17:35:09 +03:00
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
// XXX: if we use room ID, we should also include a server where the event can be found (other than in the domain of the event ID)
|
|
|
|
const permalinkButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field">
|
2019-02-25 17:35:09 +03:00
|
|
|
<a href={permalink}
|
2019-02-24 05:03:20 +03:00
|
|
|
target="_blank" rel="noopener" onClick={this.onPermalinkClick}>
|
|
|
|
{ mxEvent.isRedacted() || mxEvent.getType() !== 'm.room.message'
|
|
|
|
? _t('Share Permalink') : _t('Share Message') }
|
|
|
|
</a>
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (this.props.eventTileOps && this.props.eventTileOps.getInnerText) {
|
|
|
|
quoteButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onQuoteClick}>
|
|
|
|
{ _t('Quote') }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bridges can provide a 'external_url' to link back to the source.
|
|
|
|
if (
|
2019-02-24 05:03:20 +03:00
|
|
|
typeof(mxEvent.event.content.external_url) === "string" &&
|
|
|
|
isUrlPermitted(mxEvent.event.content.external_url)
|
2018-04-12 01:58:04 +03:00
|
|
|
) {
|
|
|
|
externalURLButton = (
|
|
|
|
<div className="mx_MessageContextMenu_field">
|
2019-02-24 05:03:20 +03:00
|
|
|
<a href={mxEvent.event.content.external_url}
|
2018-04-12 01:58:04 +03:00
|
|
|
rel="noopener" target="_blank" onClick={this.closeMenu}>{ _t('Source URL') }</a>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-20 11:23:27 +03:00
|
|
|
if (this.props.collapseReplyThread) {
|
|
|
|
collapseReplyThread = (
|
|
|
|
<div className="mx_MessageContextMenu_field" onClick={this.onCollapseReplyThreadClick}>
|
|
|
|
{ _t('Collapse Reply Thread') }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2019-02-12 21:10:42 +03:00
|
|
|
let e2eInfo;
|
|
|
|
if (this.props.e2eInfoCallback) {
|
|
|
|
e2eInfo = <div className="mx_MessageContextMenu_field" onClick={this.e2eInfoClicked}>
|
|
|
|
{ _t('End-to-end encryption information') }
|
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
return (
|
2018-10-23 16:23:59 +03:00
|
|
|
<div className="mx_MessageContextMenu">
|
2018-04-12 01:58:04 +03:00
|
|
|
{ resendButton }
|
|
|
|
{ redactButton }
|
|
|
|
{ cancelButton }
|
|
|
|
{ forwardButton }
|
|
|
|
{ pinButton }
|
|
|
|
{ viewSourceButton }
|
|
|
|
{ viewClearSourceButton }
|
|
|
|
{ unhidePreviewButton }
|
|
|
|
{ permalinkButton }
|
|
|
|
{ quoteButton }
|
|
|
|
{ externalURLButton }
|
2018-04-20 11:23:27 +03:00
|
|
|
{ collapseReplyThread }
|
2019-02-12 21:10:42 +03:00
|
|
|
{ e2eInfo }
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|