2017-12-10 15:50:41 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 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';
|
|
|
|
import sdk from '../../../index';
|
2017-12-15 21:39:01 +03:00
|
|
|
import {_t} from '../../../languageHandler';
|
2017-12-10 15:50:41 +03:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import MatrixClientPeg from '../../../MatrixClientPeg';
|
2018-01-10 15:06:24 +03:00
|
|
|
import {wantsDateSeparator} from '../../../DateUtils';
|
2017-12-15 21:39:01 +03:00
|
|
|
import {MatrixEvent} from 'matrix-js-sdk';
|
2018-01-22 19:34:47 +03:00
|
|
|
import {makeUserPermalink} from "../../../matrix-to";
|
2017-12-10 15:50:41 +03:00
|
|
|
|
|
|
|
// For URLs of matrix.to links in the timeline which have been reformatted by
|
|
|
|
// HttpUtils transformTags to relative links. This excludes event URLs (with `[^\/]*`)
|
2018-01-22 19:34:47 +03:00
|
|
|
const REGEX_LOCAL_MATRIXTO = /^#\/room\/([\#\!][^\/]*)\/(\$[^\/]*)$/;
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2017-12-17 23:20:45 +03:00
|
|
|
export default class Quote extends React.Component {
|
|
|
|
static isMessageUrl(url) {
|
|
|
|
return !!REGEX_LOCAL_MATRIXTO.exec(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
static childContextTypes = {
|
|
|
|
matrixClient: PropTypes.object,
|
2018-01-22 19:34:47 +03:00
|
|
|
addRichQuote: PropTypes.func,
|
2017-12-17 23:20:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static propTypes = {
|
2017-12-10 15:50:41 +03:00
|
|
|
// The matrix.to url of the event
|
|
|
|
url: PropTypes.string,
|
2018-01-22 19:34:47 +03:00
|
|
|
// The original node that was rendered
|
|
|
|
node: PropTypes.instanceOf(Element),
|
2017-12-15 21:39:01 +03:00
|
|
|
// The parent event
|
|
|
|
parentEv: PropTypes.instanceOf(MatrixEvent),
|
2017-12-17 23:20:45 +03:00
|
|
|
};
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2017-12-17 23:20:45 +03:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2017-12-17 23:20:45 +03:00
|
|
|
this.state = {
|
2018-01-22 19:34:47 +03:00
|
|
|
// The event related to this quote and their nested rich quotes
|
|
|
|
events: [],
|
|
|
|
// Whether the top (oldest) event should be shown or spoilered
|
|
|
|
show: true,
|
2018-01-22 19:41:32 +03:00
|
|
|
// Whether an error was encountered fetching nested older event, show node if it does
|
|
|
|
err: false,
|
2017-12-10 15:50:41 +03:00
|
|
|
};
|
2017-12-18 22:28:01 +03:00
|
|
|
|
2018-01-10 14:51:23 +03:00
|
|
|
this.onQuoteClick = this.onQuoteClick.bind(this);
|
2018-01-22 19:34:47 +03:00
|
|
|
this.addRichQuote = this.addRichQuote.bind(this);
|
2017-12-17 23:20:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
matrixClient: MatrixClientPeg.get(),
|
2018-01-22 19:34:47 +03:00
|
|
|
addRichQuote: this.addRichQuote,
|
2017-12-17 23:20:45 +03:00
|
|
|
};
|
|
|
|
}
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2018-01-22 19:34:47 +03:00
|
|
|
parseUrl(url) {
|
|
|
|
if (!url) return;
|
|
|
|
|
|
|
|
// Default to the empty array if no match for simplicity
|
|
|
|
// resource and prefix will be undefined instead of throwing
|
|
|
|
const matrixToMatch = REGEX_LOCAL_MATRIXTO.exec(url) || [];
|
|
|
|
|
|
|
|
const [, roomIdentifier, eventId] = matrixToMatch;
|
|
|
|
return {roomIdentifier, eventId};
|
|
|
|
}
|
|
|
|
|
2017-12-10 15:50:41 +03:00
|
|
|
componentWillReceiveProps(nextProps) {
|
2018-01-22 19:34:47 +03:00
|
|
|
const {roomIdentifier, eventId} = this.parseUrl(nextProps.url);
|
|
|
|
if (!roomIdentifier || !eventId) return;
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2018-01-22 19:34:47 +03:00
|
|
|
const room = this.getRoom(roomIdentifier);
|
|
|
|
if (!room) return;
|
2017-12-10 15:50:41 +03:00
|
|
|
|
|
|
|
// Only try and load the event if we know about the room
|
|
|
|
// otherwise we just leave a `Quote` anchor which can be used to navigate/join the room manually.
|
2018-01-22 19:34:47 +03:00
|
|
|
this.setState({ events: [] });
|
|
|
|
if (room) this.getEvent(room, eventId, true);
|
2017-12-17 23:20:45 +03:00
|
|
|
}
|
2017-12-10 15:50:41 +03:00
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
this.componentWillReceiveProps(this.props);
|
2017-12-17 23:20:45 +03:00
|
|
|
}
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2018-01-22 19:34:47 +03:00
|
|
|
getRoom(id) {
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
if (id[0] === '!') return cli.getRoom(id);
|
|
|
|
|
|
|
|
return cli.getRooms().find((r) => {
|
|
|
|
return r.getAliases().includes(id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getEvent(room, eventId, show) {
|
|
|
|
const event = room.findEventById(eventId);
|
2017-12-10 15:50:41 +03:00
|
|
|
if (event) {
|
2018-01-22 19:34:47 +03:00
|
|
|
this.addEvent(event, show);
|
2017-12-10 15:50:41 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await MatrixClientPeg.get().getEventTimeline(room.getUnfilteredTimelineSet(), eventId);
|
2018-01-22 19:34:47 +03:00
|
|
|
this.addEvent(room.findEventById(eventId), show);
|
|
|
|
}
|
|
|
|
|
|
|
|
addEvent(event, show) {
|
|
|
|
const events = [event].concat(this.state.events);
|
|
|
|
this.setState({events, show});
|
|
|
|
}
|
|
|
|
|
|
|
|
// addRichQuote(roomId, eventId) {
|
|
|
|
addRichQuote(href) {
|
|
|
|
const {roomIdentifier, eventId} = this.parseUrl(href);
|
2018-01-22 19:41:32 +03:00
|
|
|
if (!roomIdentifier || !eventId) {
|
|
|
|
this.setState({ err: true });
|
|
|
|
return;
|
|
|
|
}
|
2018-01-22 19:34:47 +03:00
|
|
|
|
|
|
|
const room = this.getRoom(roomIdentifier);
|
2018-01-22 19:41:32 +03:00
|
|
|
if (!room) {
|
|
|
|
this.setState({ err: true });
|
|
|
|
return;
|
|
|
|
}
|
2018-01-22 19:34:47 +03:00
|
|
|
|
|
|
|
this.getEvent(room, eventId, false);
|
2017-12-17 23:20:45 +03:00
|
|
|
}
|
2017-12-10 15:50:41 +03:00
|
|
|
|
2018-01-10 14:51:23 +03:00
|
|
|
onQuoteClick() {
|
2018-01-22 19:34:47 +03:00
|
|
|
this.setState({ show: true });
|
2017-12-18 22:28:01 +03:00
|
|
|
}
|
|
|
|
|
2017-12-17 23:20:45 +03:00
|
|
|
render() {
|
2018-01-22 19:34:47 +03:00
|
|
|
const events = this.state.events.slice();
|
|
|
|
if (events.length) {
|
|
|
|
const evTiles = [];
|
|
|
|
|
|
|
|
if (!this.state.show) {
|
|
|
|
const oldestEv = events.shift();
|
|
|
|
const Pill = sdk.getComponent('elements.Pill');
|
|
|
|
const room = MatrixClientPeg.get().getRoom(oldestEv.getRoomId());
|
|
|
|
|
|
|
|
evTiles.push(<blockquote className="mx_Quote" key="load">
|
|
|
|
{
|
|
|
|
_t('<a>In reply to</a> <pill>', {}, {
|
|
|
|
'a': (sub) => <a onClick={this.onQuoteClick} className="mx_Quote_show">{ sub }</a>,
|
2018-01-22 20:02:20 +03:00
|
|
|
'pill': <Pill type={Pill.TYPE_USER_MENTION} room={room}
|
|
|
|
url={makeUserPermalink(oldestEv.getSender())} shouldShowPillAvatar={true} />,
|
2018-01-22 19:34:47 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
</blockquote>);
|
|
|
|
}
|
|
|
|
|
|
|
|
const EventTile = sdk.getComponent('views.rooms.EventTile');
|
|
|
|
const DateSeparator = sdk.getComponent('messages.DateSeparator');
|
|
|
|
events.forEach((ev) => {
|
2017-12-18 22:28:01 +03:00
|
|
|
let dateSep = null;
|
2018-01-10 15:06:24 +03:00
|
|
|
|
2018-01-22 19:34:47 +03:00
|
|
|
if (wantsDateSeparator(this.props.parentEv.getDate(), ev.getDate())) {
|
|
|
|
dateSep = <a href={this.props.url}><DateSeparator ts={ev.getTs()} /></a>;
|
2017-12-18 22:28:01 +03:00
|
|
|
}
|
|
|
|
|
2018-01-22 19:34:47 +03:00
|
|
|
evTiles.push(<blockquote className="mx_Quote" key={ev.getId()}>
|
2017-12-18 22:28:01 +03:00
|
|
|
{ dateSep }
|
|
|
|
<EventTile mxEvent={ev} tileShape="quote" />
|
2018-01-22 19:34:47 +03:00
|
|
|
</blockquote>);
|
|
|
|
});
|
2017-12-15 21:39:01 +03:00
|
|
|
|
2018-01-22 19:34:47 +03:00
|
|
|
return <div>{ evTiles }</div>;
|
2017-12-10 15:50:41 +03:00
|
|
|
}
|
2017-12-10 15:54:19 +03:00
|
|
|
|
|
|
|
// Deliberately render nothing if the URL isn't recognised
|
2018-01-22 19:34:47 +03:00
|
|
|
return this.props.node;
|
2017-12-17 23:20:45 +03:00
|
|
|
}
|
|
|
|
}
|