2019-10-13 15:08:50 +03:00
|
|
|
/*
|
2021-05-01 16:11:30 +03:00
|
|
|
Copyright 2020-2021 Tulir Asokan <tulir@maunium.net>
|
2019-10-13 15:08: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';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2020-04-10 14:23:34 +03:00
|
|
|
import classNames from 'classnames';
|
2019-10-13 15:08:50 +03:00
|
|
|
import { _t, _td } from '../../../languageHandler';
|
|
|
|
|
|
2020-04-10 14:23:34 +03:00
|
|
|
import * as sdk from '../../../index';
|
2019-10-13 15:08:50 +03:00
|
|
|
|
2020-05-25 19:33:30 +03:00
|
|
|
import dis from '../../../dispatcher/dispatcher';
|
2019-10-13 15:08:50 +03:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2021-07-02 12:56:08 +03:00
|
|
|
import { MatrixClient } from 'matrix-js-sdk';
|
2019-10-13 15:08:50 +03:00
|
|
|
|
2021-07-02 12:56:08 +03:00
|
|
|
import { objectHasDiff } from '../../../utils/objects';
|
|
|
|
|
import { getHandlerTile } from "./EventTile";
|
2019-10-13 15:08:50 +03:00
|
|
|
|
|
|
|
|
class ReplyTile extends React.Component {
|
|
|
|
|
static contextTypes = {
|
|
|
|
|
matrixClient: PropTypes.instanceOf(MatrixClient).isRequired,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
|
mxEvent: PropTypes.object.isRequired,
|
|
|
|
|
isRedacted: PropTypes.bool,
|
|
|
|
|
permalinkCreator: PropTypes.object,
|
|
|
|
|
onHeightChanged: PropTypes.func,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
|
onHeightChanged: function() {},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
|
super(props, context);
|
|
|
|
|
this.state = {};
|
|
|
|
|
this.onClick = this.onClick.bind(this);
|
2020-04-10 15:56:09 +03:00
|
|
|
this._onDecrypted = this._onDecrypted.bind(this);
|
2019-10-13 15:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.props.mxEvent.on("Event.decrypted", this._onDecrypted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
2021-02-19 21:47:10 +02:00
|
|
|
if (objectHasDiff(this.state, nextState)) {
|
2019-10-13 15:08:50 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-01 16:11:30 +03:00
|
|
|
return objectHasDiff(this.props, nextProps);
|
2019-10-13 15:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
this.props.mxEvent.removeListener("Event.decrypted", this._onDecrypted);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_onDecrypted() {
|
|
|
|
|
this.forceUpdate();
|
2020-04-10 15:56:09 +03:00
|
|
|
if (this.props.onHeightChanged) {
|
|
|
|
|
this.props.onHeightChanged();
|
|
|
|
|
}
|
2019-10-13 15:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onClick(e) {
|
|
|
|
|
// This allows the permalink to be opened in a new tab/window or copied as
|
|
|
|
|
// matrix.to, but also for it to enable routing within Riot when clicked.
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
dis.dispatch({
|
|
|
|
|
action: 'view_room',
|
|
|
|
|
event_id: this.props.mxEvent.getId(),
|
|
|
|
|
highlighted: true,
|
|
|
|
|
room_id: this.props.mxEvent.getRoomId(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const SenderProfile = sdk.getComponent('messages.SenderProfile');
|
|
|
|
|
|
|
|
|
|
const content = this.props.mxEvent.getContent();
|
|
|
|
|
const msgtype = content.msgtype;
|
|
|
|
|
const eventType = this.props.mxEvent.getType();
|
|
|
|
|
|
|
|
|
|
// Info messages are basically information about commands processed on a room
|
|
|
|
|
let isInfoMessage = (
|
|
|
|
|
eventType !== 'm.room.message' && eventType !== 'm.sticker' && eventType !== 'm.room.create'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let tileHandler = getHandlerTile(this.props.mxEvent);
|
|
|
|
|
// If we're showing hidden events in the timeline, we should use the
|
|
|
|
|
// source tile when there's no regular tile for an event and also for
|
|
|
|
|
// replace relations (which otherwise would display as a confusing
|
|
|
|
|
// duplicate of the thing they are replacing).
|
|
|
|
|
const useSource = !tileHandler || this.props.mxEvent.isRelation("m.replace");
|
|
|
|
|
if (useSource && SettingsStore.getValue("showHiddenEventsInTimeline")) {
|
|
|
|
|
tileHandler = "messages.ViewSourceEvent";
|
|
|
|
|
// Reuse info message avatar and sender profile styling
|
|
|
|
|
isInfoMessage = true;
|
|
|
|
|
}
|
|
|
|
|
// This shouldn't happen: the caller should check we support this type
|
|
|
|
|
// before trying to instantiate us
|
|
|
|
|
if (!tileHandler) {
|
2021-07-02 12:56:08 +03:00
|
|
|
const { mxEvent } = this.props;
|
2019-10-13 15:08:50 +03:00
|
|
|
console.warn(`Event type not supported: type:${mxEvent.getType()} isState:${mxEvent.isState()}`);
|
|
|
|
|
return <div className="mx_ReplyTile mx_ReplyTile_info mx_MNoticeBody">
|
|
|
|
|
{ _t('This event could not be displayed') }
|
|
|
|
|
</div>;
|
|
|
|
|
}
|
|
|
|
|
const EventTileType = sdk.getComponent(tileHandler);
|
|
|
|
|
|
|
|
|
|
const classes = classNames({
|
|
|
|
|
mx_ReplyTile: true,
|
|
|
|
|
mx_ReplyTile_info: isInfoMessage,
|
|
|
|
|
mx_ReplyTile_redacted: this.props.isRedacted,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let permalink = "#";
|
|
|
|
|
if (this.props.permalinkCreator) {
|
|
|
|
|
permalink = this.props.permalinkCreator.forEvent(this.props.mxEvent.getId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let sender;
|
2020-04-10 15:45:59 +03:00
|
|
|
const needsSenderProfile = msgtype !== 'm.image' && tileHandler !== 'messages.RoomCreate' && !isInfoMessage;
|
2019-10-13 15:08:50 +03:00
|
|
|
|
|
|
|
|
if (needsSenderProfile) {
|
|
|
|
|
let text = null;
|
|
|
|
|
if (msgtype === 'm.image') text = _td('%(senderName)s sent an image');
|
|
|
|
|
else if (msgtype === 'm.video') text = _td('%(senderName)s sent a video');
|
|
|
|
|
else if (msgtype === 'm.file') text = _td('%(senderName)s uploaded a file');
|
|
|
|
|
sender = <SenderProfile onClick={this.onSenderProfileClick}
|
|
|
|
|
mxEvent={this.props.mxEvent}
|
|
|
|
|
enableFlair={false}
|
|
|
|
|
text={text} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MImageReplyBody = sdk.getComponent('messages.MImageReplyBody');
|
|
|
|
|
const TextualBody = sdk.getComponent('messages.TextualBody');
|
|
|
|
|
const msgtypeOverrides = {
|
|
|
|
|
"m.image": MImageReplyBody,
|
|
|
|
|
// We don't want a download link for files, just the file name is enough.
|
|
|
|
|
"m.file": TextualBody,
|
|
|
|
|
"m.sticker": TextualBody,
|
|
|
|
|
"m.audio": TextualBody,
|
|
|
|
|
"m.video": TextualBody,
|
|
|
|
|
};
|
|
|
|
|
const evOverrides = {
|
|
|
|
|
"m.sticker": TextualBody,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={classes}>
|
|
|
|
|
<a href={permalink} onClick={this.onClick}>
|
|
|
|
|
{ sender }
|
|
|
|
|
<EventTileType ref="tile"
|
|
|
|
|
mxEvent={this.props.mxEvent}
|
|
|
|
|
highlights={this.props.highlights}
|
|
|
|
|
highlightLink={this.props.highlightLink}
|
|
|
|
|
onHeightChanged={this.props.onHeightChanged}
|
|
|
|
|
showUrlPreview={false}
|
|
|
|
|
overrideBodyTypes={msgtypeOverrides}
|
|
|
|
|
overrideEventTypes={evOverrides}
|
2020-03-05 13:44:54 +02:00
|
|
|
maxImageHeight={96} />
|
2019-10-13 15:08:50 +03:00
|
|
|
</a>
|
|
|
|
|
</div>
|
2020-03-05 13:44:54 +02:00
|
|
|
);
|
2019-10-13 15:08:50 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 14:23:34 +03:00
|
|
|
export default ReplyTile;
|