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';
|
2020-04-10 14:23:34 +03:00
|
|
|
import classNames from 'classnames';
|
2021-07-02 14:51:51 +02:00
|
|
|
import { _t } from '../../../languageHandler';
|
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 { getHandlerTile } from "./EventTile";
|
2021-07-02 14:51:51 +02:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
|
import { RoomPermalinkCreator } from '../../../utils/permalinks/Permalinks';
|
|
|
|
|
import SenderProfile from "../messages/SenderProfile";
|
|
|
|
|
import TextualBody from "../messages/TextualBody";
|
|
|
|
|
import MImageReplyBody from "../messages/MImageReplyBody";
|
|
|
|
|
import * as sdk from '../../../index';
|
2021-07-13 09:26:27 +02:00
|
|
|
import { EventType, MsgType, RelationType } from 'matrix-js-sdk/src/@types/event';
|
2021-07-13 09:40:29 +02:00
|
|
|
import { replaceableComponent } from '../../../utils/replaceableComponent';
|
2019-10-13 15:08:50 +03:00
|
|
|
|
2021-07-02 14:51:51 +02:00
|
|
|
interface IProps {
|
|
|
|
|
mxEvent: MatrixEvent;
|
|
|
|
|
permalinkCreator?: RoomPermalinkCreator;
|
|
|
|
|
highlights?: Array<string>;
|
|
|
|
|
highlightLink?: string;
|
|
|
|
|
onHeightChanged?(): void;
|
|
|
|
|
}
|
2019-10-13 15:08:50 +03:00
|
|
|
|
2021-07-13 09:22:13 +02:00
|
|
|
@replaceableComponent("views.rooms.ReplyTile")
|
2021-07-02 14:51:51 +02:00
|
|
|
export default class ReplyTile extends React.PureComponent<IProps> {
|
2019-10-13 15:08:50 +03:00
|
|
|
static defaultProps = {
|
2021-07-02 14:51:51 +02:00
|
|
|
onHeightChanged: () => {},
|
|
|
|
|
};
|
2019-10-13 15:08:50 +03:00
|
|
|
|
|
|
|
|
componentDidMount() {
|
2021-07-02 14:51:51 +02:00
|
|
|
this.props.mxEvent.on("Event.decrypted", this.onDecrypted);
|
2021-07-13 10:12:24 +02:00
|
|
|
this.props.mxEvent.on("Event.beforeRedaction", this.onBeforeRedaction);
|
2019-10-13 15:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2021-07-02 14:51:51 +02:00
|
|
|
this.props.mxEvent.removeListener("Event.decrypted", this.onDecrypted);
|
2019-10-13 15:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
2021-07-02 14:51:51 +02:00
|
|
|
private onDecrypted = (): void => {
|
2019-10-13 15:08:50 +03:00
|
|
|
this.forceUpdate();
|
2020-04-10 15:56:09 +03:00
|
|
|
if (this.props.onHeightChanged) {
|
|
|
|
|
this.props.onHeightChanged();
|
|
|
|
|
}
|
2021-07-02 14:51:51 +02:00
|
|
|
};
|
2019-10-13 15:08:50 +03:00
|
|
|
|
2021-07-13 10:12:24 +02:00
|
|
|
private onBeforeRedaction = (): void => {
|
|
|
|
|
// When the event gets redacted, update it, so that a different tile handler is used
|
|
|
|
|
this.forceUpdate();
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-02 14:51:51 +02:00
|
|
|
private onClick = (e: React.MouseEvent): void => {
|
2019-10-13 15:08:50 +03:00
|
|
|
// 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(),
|
|
|
|
|
});
|
2021-07-02 14:51:51 +02:00
|
|
|
};
|
2019-10-13 15:08:50 +03:00
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
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
|
2021-07-13 09:23:57 +02:00
|
|
|
let isInfoMessage = [
|
|
|
|
|
EventType.RoomMessage,
|
|
|
|
|
EventType.Sticker,
|
|
|
|
|
EventType.RoomCreate,
|
|
|
|
|
].includes(eventType as EventType);
|
2019-10-13 15:08:50 +03:00
|
|
|
|
|
|
|
|
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).
|
2021-07-13 09:24:40 +02:00
|
|
|
const useSource = !tileHandler || this.props.mxEvent.isRelation(RelationType.Replace);
|
2019-10-13 15:08:50 +03:00
|
|
|
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>;
|
|
|
|
|
}
|
2021-07-02 14:51:51 +02:00
|
|
|
|
2019-10-13 15:08:50 +03:00
|
|
|
const EventTileType = sdk.getComponent(tileHandler);
|
|
|
|
|
|
2021-07-13 09:27:22 +02:00
|
|
|
const classes = classNames("mx_ReplyTile", {
|
2019-10-13 15:08:50 +03:00
|
|
|
mx_ReplyTile_info: isInfoMessage,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let permalink = "#";
|
|
|
|
|
if (this.props.permalinkCreator) {
|
|
|
|
|
permalink = this.props.permalinkCreator.forEvent(this.props.mxEvent.getId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let sender;
|
2021-07-13 09:26:27 +02:00
|
|
|
const needsSenderProfile = msgtype !== MsgType.Image && tileHandler !== EventType.RoomCreate && !isInfoMessage;
|
2019-10-13 15:08:50 +03:00
|
|
|
|
|
|
|
|
if (needsSenderProfile) {
|
2021-07-02 14:51:51 +02:00
|
|
|
sender = <SenderProfile
|
2019-10-13 15:08:50 +03:00
|
|
|
mxEvent={this.props.mxEvent}
|
|
|
|
|
enableFlair={false}
|
2021-07-02 14:51:51 +02:00
|
|
|
/>;
|
2019-10-13 15:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const msgtypeOverrides = {
|
2021-07-13 09:30:52 +02:00
|
|
|
[MsgType.Image]: MImageReplyBody,
|
2019-10-13 15:08:50 +03:00
|
|
|
// We don't want a download link for files, just the file name is enough.
|
2021-07-13 09:30:52 +02:00
|
|
|
[MsgType.File]: TextualBody,
|
2019-10-13 15:08:50 +03:00
|
|
|
"m.sticker": TextualBody,
|
2021-07-13 09:30:52 +02:00
|
|
|
[MsgType.Audio]: TextualBody,
|
|
|
|
|
[MsgType.Video]: TextualBody,
|
2019-10-13 15:08:50 +03:00
|
|
|
};
|
|
|
|
|
const evOverrides = {
|
2021-07-13 09:30:52 +02:00
|
|
|
[EventType.Sticker]: TextualBody,
|
2019-10-13 15:08:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={classes}>
|
|
|
|
|
<a href={permalink} onClick={this.onClick}>
|
|
|
|
|
{ sender }
|
2021-07-02 14:51:51 +02:00
|
|
|
<EventTileType
|
|
|
|
|
ref="tile"
|
2019-10-13 15:08:50 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|