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';
|
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 `[^\/]*`)
|
|
|
|
const REGEX_LOCAL_MATRIXTO = /^#\/room\/(([\#\!])[^\/]*)\/(\$[^\/]*)$/;
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
static propTypes = {
|
2017-12-10 15:50:41 +03:00
|
|
|
// The matrix.to url of the event
|
|
|
|
url: PropTypes.string,
|
2017-12-15 21:39:01 +03:00
|
|
|
// The parent event
|
|
|
|
parentEv: PropTypes.instanceOf(MatrixEvent),
|
2017-12-18 22:28:01 +03:00
|
|
|
// Whether this isn't the first Quote, and we're being nested
|
|
|
|
isNested: PropTypes.bool,
|
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 = {
|
2017-12-10 15:50:41 +03:00
|
|
|
// The event related to this quote
|
|
|
|
event: null,
|
2017-12-18 22:28:01 +03:00
|
|
|
show: !this.props.isNested,
|
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);
|
2017-12-17 23:20:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
matrixClient: MatrixClientPeg.get(),
|
|
|
|
};
|
|
|
|
}
|
2017-12-10 15:50:41 +03:00
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
let roomId;
|
|
|
|
let prefix;
|
|
|
|
let eventId;
|
|
|
|
|
|
|
|
if (nextProps.url) {
|
|
|
|
// 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(nextProps.url) || [];
|
|
|
|
|
|
|
|
roomId = matrixToMatch[1]; // The room ID
|
|
|
|
prefix = matrixToMatch[2]; // The first character of prefix
|
|
|
|
eventId = matrixToMatch[3]; // The event ID
|
|
|
|
}
|
|
|
|
|
|
|
|
const room = prefix === '#' ?
|
|
|
|
MatrixClientPeg.get().getRooms().find((r) => {
|
|
|
|
return r.getAliases().includes(roomId);
|
|
|
|
}) : MatrixClientPeg.get().getRoom(roomId);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (room) this.getEvent(room, eventId);
|
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
|
|
|
|
|
|
|
async getEvent(room, eventId) {
|
|
|
|
let event = room.findEventById(eventId);
|
|
|
|
if (event) {
|
|
|
|
this.setState({room, event});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await MatrixClientPeg.get().getEventTimeline(room.getUnfilteredTimelineSet(), eventId);
|
|
|
|
event = room.findEventById(eventId);
|
|
|
|
this.setState({room, event});
|
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() {
|
2017-12-18 22:28:01 +03:00
|
|
|
this.setState({
|
|
|
|
show: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-17 23:20:45 +03:00
|
|
|
render() {
|
2017-12-10 15:50:41 +03:00
|
|
|
const ev = this.state.event;
|
|
|
|
if (ev) {
|
2017-12-18 22:28:01 +03:00
|
|
|
if (this.state.show) {
|
|
|
|
const EventTile = sdk.getComponent('views.rooms.EventTile');
|
|
|
|
let dateSep = null;
|
2018-01-10 15:06:24 +03:00
|
|
|
|
|
|
|
const evDate = ev.getDate();
|
|
|
|
if (wantsDateSeparator(this.props.parentEv.getDate(), evDate)) {
|
2017-12-18 22:28:01 +03:00
|
|
|
const DateSeparator = sdk.getComponent('messages.DateSeparator');
|
2018-01-10 15:06:24 +03:00
|
|
|
dateSep = <a href={this.props.url}><DateSeparator ts={evDate} /></a>;
|
2017-12-18 22:28:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return <blockquote className="mx_Quote">
|
|
|
|
{ dateSep }
|
|
|
|
<EventTile mxEvent={ev} tileShape="quote" />
|
|
|
|
</blockquote>;
|
2017-12-15 21:39:01 +03:00
|
|
|
}
|
|
|
|
|
2017-12-18 22:28:01 +03:00
|
|
|
return <div>
|
2018-01-10 14:51:23 +03:00
|
|
|
<a onClick={this.onQuoteClick} className="mx_Quote_show">{ _t('Quote') }</a>
|
2017-12-18 22:28:01 +03:00
|
|
|
<br />
|
|
|
|
</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
|
|
|
|
return <div>
|
2017-12-15 21:39:01 +03:00
|
|
|
<a href={this.props.url}>{ _t('Quote') }</a>
|
2017-12-10 15:54:19 +03:00
|
|
|
<br />
|
|
|
|
</div>;
|
2017-12-17 23:20:45 +03:00
|
|
|
}
|
|
|
|
}
|