2019-05-29 19:03:05 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
2019-05-29 19:03:05 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from "../../../index";
|
2019-06-24 18:45:57 +03:00
|
|
|
import {wantsDateSeparator} from '../../../DateUtils';
|
|
|
|
import SettingsStore from '../../../settings/SettingsStore';
|
2021-03-09 05:59:41 +03:00
|
|
|
import {replaceableComponent} from "../../../utils/replaceableComponent";
|
2019-05-29 19:03:05 +03:00
|
|
|
|
2021-03-09 05:59:41 +03:00
|
|
|
@replaceableComponent("views.dialogs.MessageEditHistoryDialog")
|
2019-06-26 17:12:44 +03:00
|
|
|
export default class MessageEditHistoryDialog extends React.PureComponent {
|
2019-05-29 19:03:05 +03:00
|
|
|
static propTypes = {
|
|
|
|
mxEvent: PropTypes.object.isRequired,
|
|
|
|
};
|
|
|
|
|
2019-06-26 16:51:46 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2019-07-09 18:53:35 +03:00
|
|
|
originalEvent: null,
|
2019-07-03 13:16:58 +03:00
|
|
|
error: null,
|
2019-06-25 15:50:56 +03:00
|
|
|
events: [],
|
|
|
|
nextBatch: null,
|
2019-06-24 18:45:57 +03:00
|
|
|
isLoading: true,
|
|
|
|
isTwelveHour: SettingsStore.getValue("showTwelveHourTimestamps"),
|
2019-06-26 16:51:46 +03:00
|
|
|
};
|
2019-05-29 19:03:05 +03:00
|
|
|
}
|
|
|
|
|
2019-06-26 16:51:28 +03:00
|
|
|
loadMoreEdits = async (backwards) => {
|
2019-06-25 15:50:56 +03:00
|
|
|
if (backwards || (!this.state.nextBatch && !this.state.isLoading)) {
|
|
|
|
// bail out on backwards as we only paginate in one direction
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-26 16:49:24 +03:00
|
|
|
const opts = {from: this.state.nextBatch};
|
2019-05-29 19:03:05 +03:00
|
|
|
const roomId = this.props.mxEvent.getRoomId();
|
|
|
|
const eventId = this.props.mxEvent.getId();
|
2019-07-05 17:03:34 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
2019-07-03 13:16:58 +03:00
|
|
|
let result;
|
2019-06-25 15:50:56 +03:00
|
|
|
let resolve;
|
2019-07-03 13:16:58 +03:00
|
|
|
let reject;
|
|
|
|
const promise = new Promise((_resolve, _reject) => {resolve = _resolve; reject = _reject;});
|
|
|
|
try {
|
2019-07-05 17:03:34 +03:00
|
|
|
result = await client.relations(
|
2019-07-03 13:16:58 +03:00
|
|
|
roomId, eventId, "m.replace", "m.room.message", opts);
|
|
|
|
} catch (error) {
|
2019-07-03 18:53:32 +03:00
|
|
|
// log if the server returned an error
|
|
|
|
if (error.errcode) {
|
|
|
|
console.error("fetching /relations failed with error", error);
|
|
|
|
}
|
2019-07-03 13:16:58 +03:00
|
|
|
this.setState({error}, () => reject(error));
|
|
|
|
return promise;
|
|
|
|
}
|
2019-07-05 17:03:34 +03:00
|
|
|
|
|
|
|
const newEvents = result.events;
|
|
|
|
this._locallyRedactEventsIfNeeded(newEvents);
|
2019-06-25 15:50:56 +03:00
|
|
|
this.setState({
|
2019-07-09 18:53:35 +03:00
|
|
|
originalEvent: this.state.originalEvent || result.originalEvent,
|
2019-07-05 17:03:34 +03:00
|
|
|
events: this.state.events.concat(newEvents),
|
2019-06-25 15:50:56 +03:00
|
|
|
nextBatch: result.nextBatch,
|
|
|
|
isLoading: false,
|
|
|
|
}, () => {
|
|
|
|
const hasMoreResults = !!this.state.nextBatch;
|
|
|
|
resolve(hasMoreResults);
|
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2019-07-05 17:03:34 +03:00
|
|
|
_locallyRedactEventsIfNeeded(newEvents) {
|
|
|
|
const roomId = this.props.mxEvent.getRoomId();
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
const pendingEvents = room.getPendingEvents();
|
|
|
|
for (const e of newEvents) {
|
|
|
|
const pendingRedaction = pendingEvents.find(pe => {
|
|
|
|
return pe.getType() === "m.room.redaction" && pe.getAssociatedId() === e.getId();
|
|
|
|
});
|
|
|
|
if (pendingRedaction) {
|
|
|
|
e.markLocallyRedacted(pendingRedaction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 15:50:56 +03:00
|
|
|
componentDidMount() {
|
|
|
|
this.loadMoreEdits();
|
2019-05-29 19:03:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_renderEdits() {
|
2019-06-26 17:17:25 +03:00
|
|
|
const EditHistoryMessage = sdk.getComponent('messages.EditHistoryMessage');
|
2019-05-29 19:03:05 +03:00
|
|
|
const DateSeparator = sdk.getComponent('messages.DateSeparator');
|
|
|
|
const nodes = [];
|
|
|
|
let lastEvent;
|
2019-07-09 18:53:35 +03:00
|
|
|
let allEvents = this.state.events;
|
|
|
|
// append original event when we've done last pagination
|
|
|
|
if (this.state.originalEvent && !this.state.nextBatch) {
|
|
|
|
allEvents = allEvents.concat(this.state.originalEvent);
|
|
|
|
}
|
2019-07-18 15:09:29 +03:00
|
|
|
const baseEventId = this.props.mxEvent.getId();
|
2019-07-19 17:09:23 +03:00
|
|
|
allEvents.forEach((e, i) => {
|
2019-05-29 19:03:05 +03:00
|
|
|
if (!lastEvent || wantsDateSeparator(lastEvent.getDate(), e.getDate())) {
|
|
|
|
nodes.push(<li key={e.getTs() + "~"}><DateSeparator ts={e.getTs()} /></li>);
|
|
|
|
}
|
2019-07-18 15:09:29 +03:00
|
|
|
const isBaseEvent = e.getId() === baseEventId;
|
|
|
|
nodes.push((
|
|
|
|
<EditHistoryMessage
|
|
|
|
key={e.getId()}
|
2019-11-09 01:54:48 +03:00
|
|
|
previousEdit={!isBaseEvent ? allEvents[i + 1] : null}
|
2019-07-18 15:09:29 +03:00
|
|
|
isBaseEvent={isBaseEvent}
|
|
|
|
mxEvent={e}
|
|
|
|
isTwelveHour={this.state.isTwelveHour}
|
|
|
|
/>));
|
2019-05-29 19:03:05 +03:00
|
|
|
lastEvent = e;
|
|
|
|
});
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-06-25 15:50:56 +03:00
|
|
|
let content;
|
|
|
|
if (this.state.error) {
|
2019-07-03 13:16:58 +03:00
|
|
|
const {error} = this.state;
|
|
|
|
if (error.errcode === "M_UNRECOGNIZED") {
|
|
|
|
content = (<p className="mx_MessageEditHistoryDialog_error">
|
|
|
|
{_t("Your homeserver doesn't seem to support this feature.")}
|
|
|
|
</p>);
|
|
|
|
} else if (error.errcode) {
|
|
|
|
// some kind of error from the homeserver
|
|
|
|
content = (<p className="mx_MessageEditHistoryDialog_error">
|
|
|
|
{_t("Something went wrong!")}
|
|
|
|
</p>);
|
|
|
|
} else {
|
|
|
|
content = (<p className="mx_MessageEditHistoryDialog_error">
|
2019-07-03 18:51:16 +03:00
|
|
|
{_t("Cannot reach homeserver")}
|
2019-07-03 13:16:58 +03:00
|
|
|
<br />
|
2019-07-03 18:51:16 +03:00
|
|
|
{_t("Ensure you have a stable internet connection, or get in touch with the server admin")}
|
2019-07-03 13:16:58 +03:00
|
|
|
</p>);
|
|
|
|
}
|
2019-06-25 15:50:56 +03:00
|
|
|
} else if (this.state.isLoading) {
|
2019-05-29 19:03:05 +03:00
|
|
|
const Spinner = sdk.getComponent("elements.Spinner");
|
2019-06-25 15:50:56 +03:00
|
|
|
content = <Spinner />;
|
|
|
|
} else {
|
|
|
|
const ScrollPanel = sdk.getComponent("structures.ScrollPanel");
|
|
|
|
content = (<ScrollPanel
|
|
|
|
className="mx_MessageEditHistoryDialog_scrollPanel"
|
|
|
|
onFillRequest={ this.loadMoreEdits }
|
|
|
|
stickyBottom={false}
|
|
|
|
startAtBottom={false}
|
|
|
|
>
|
2019-06-26 12:57:49 +03:00
|
|
|
<ul className="mx_MessageEditHistoryDialog_edits mx_MessagePanel_alwaysShowTimestamps">{this._renderEdits()}</ul>
|
2019-06-25 15:50:56 +03:00
|
|
|
</ScrollPanel>);
|
2019-05-29 19:03:05 +03:00
|
|
|
}
|
2019-06-25 15:50:56 +03:00
|
|
|
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
2019-05-29 19:03:05 +03:00
|
|
|
return (
|
2021-04-27 18:23:27 +03:00
|
|
|
<BaseDialog
|
|
|
|
className='mx_MessageEditHistoryDialog'
|
|
|
|
hasCancel={true}
|
|
|
|
onFinished={this.props.onFinished}
|
|
|
|
title={_t("Message edits")}
|
|
|
|
>
|
2019-06-25 15:50:56 +03:00
|
|
|
{content}
|
2019-05-29 19:03:05 +03:00
|
|
|
</BaseDialog>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|