2016-08-31 13:58:06 +03:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2019-12-20 03:45:24 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2016-08-31 13:58:06 +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.
|
|
|
|
*/
|
|
|
|
|
2017-05-23 17:16:31 +03:00
|
|
|
import React from 'react';
|
2019-08-30 12:34:59 +03:00
|
|
|
import createReactClass from 'create-react-class';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2016-08-31 13:58:06 +03:00
|
|
|
|
2020-01-22 18:26:40 +03:00
|
|
|
import {Filter} from 'matrix-js-sdk';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../index';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from '../../MatrixClientPeg';
|
2020-01-17 12:35:33 +03:00
|
|
|
import EventIndexPeg from "../../indexing/EventIndexPeg";
|
2017-11-13 22:19:33 +03:00
|
|
|
import { _t } from '../../languageHandler';
|
2016-08-31 13:58:06 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Component which shows the filtered file using a TimelinePanel
|
|
|
|
*/
|
2019-08-30 12:34:59 +03:00
|
|
|
const FilePanel = createReactClass({
|
2016-08-31 13:58:06 +03:00
|
|
|
displayName: 'FilePanel',
|
2020-01-22 18:21:11 +03:00
|
|
|
decryptingEvents: new Set(),
|
2016-08-31 13:58:06 +03:00
|
|
|
|
2016-09-06 02:59:17 +03:00
|
|
|
propTypes: {
|
2017-12-26 04:03:18 +03:00
|
|
|
roomId: PropTypes.string.isRequired,
|
2016-09-06 02:59:17 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
timelineSet: null,
|
2017-01-20 17:22:27 +03:00
|
|
|
};
|
2016-09-06 02:59:17 +03:00
|
|
|
},
|
|
|
|
|
2020-01-22 18:31:49 +03:00
|
|
|
onRoomTimeline(ev, room, toStartOfTimeline, removed, data) {
|
2020-01-22 18:21:11 +03:00
|
|
|
if (room.roomId !== this.props.roomId) return;
|
|
|
|
if (toStartOfTimeline || !data || !data.liveEvent || ev.isRedacted()) return;
|
|
|
|
|
|
|
|
if (ev.isBeingDecrypted()) {
|
|
|
|
this.decryptingEvents.add(ev.getId());
|
|
|
|
} else {
|
|
|
|
this.addEncryptedLiveEvent(ev);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-01-22 18:31:49 +03:00
|
|
|
onEventDecrypted(ev, err) {
|
2020-01-22 18:21:11 +03:00
|
|
|
if (ev.getRoomId() !== this.props.roomId) return;
|
|
|
|
const eventId = ev.getId();
|
|
|
|
|
|
|
|
if (!this.decryptingEvents.delete(eventId)) return;
|
|
|
|
if (err) return;
|
|
|
|
|
|
|
|
this.addEncryptedLiveEvent(ev);
|
|
|
|
},
|
|
|
|
|
|
|
|
addEncryptedLiveEvent(ev, toStartOfTimeline) {
|
|
|
|
if (!this.state.timelineSet) return;
|
|
|
|
|
|
|
|
const timeline = this.state.timelineSet.getLiveTimeline();
|
|
|
|
if (ev.getType() !== "m.room.message") return;
|
|
|
|
if (["m.file", "m.image", "m.video", "m.audio"].indexOf(ev.getContent().msgtype) == -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.state.timelineSet.eventIdToTimeline(ev.getId())) {
|
|
|
|
this.state.timelineSet.addEventToTimeline(ev, timeline, false);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-01-14 13:20:56 +03:00
|
|
|
async componentDidMount() {
|
2020-01-22 18:21:11 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
2020-01-14 13:20:56 +03:00
|
|
|
await this.updateTimelineSet(this.props.roomId);
|
2020-01-22 18:21:11 +03:00
|
|
|
|
|
|
|
if (!MatrixClientPeg.get().isRoomEncrypted(this.props.roomId)) return;
|
|
|
|
|
|
|
|
if (EventIndexPeg.get() !== null) {
|
|
|
|
client.on('Room.timeline', this.onRoomTimeline.bind(this));
|
|
|
|
client.on('Event.decrypted', this.onEventDecrypted.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (client === null) return;
|
|
|
|
|
|
|
|
if (!MatrixClientPeg.get().isRoomEncrypted(this.props.roomId)) return;
|
|
|
|
|
|
|
|
if (EventIndexPeg.get() !== null) {
|
|
|
|
client.removeListener('Room.timeline', this.onRoomTimeline.bind(this));
|
|
|
|
client.removeListener('Event.decrypted', this.onEventDecrypted.bind(this));
|
|
|
|
}
|
2016-09-07 17:43:14 +03:00
|
|
|
},
|
|
|
|
|
2020-01-14 13:20:56 +03:00
|
|
|
async fetchFileEventsServer(room) {
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
2020-01-22 18:26:40 +03:00
|
|
|
const filter = new Filter(client.credentials.userId);
|
2020-01-14 13:20:56 +03:00
|
|
|
filter.setDefinition(
|
|
|
|
{
|
|
|
|
"room": {
|
|
|
|
"timeline": {
|
|
|
|
"contains_url": true,
|
|
|
|
"types": [
|
|
|
|
"m.room.message",
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-01-20 14:43:20 +03:00
|
|
|
const filterId = await client.getOrCreateFilter("FILTER_FILES_" + client.credentials.userId, filter);
|
2020-01-14 13:20:56 +03:00
|
|
|
filter.filterId = filterId;
|
|
|
|
const timelineSet = room.getOrCreateFilteredTimelineSet(filter);
|
|
|
|
|
|
|
|
return timelineSet;
|
|
|
|
},
|
|
|
|
|
2020-01-17 12:04:53 +03:00
|
|
|
onPaginationRequest(timelineWindow, direction, limit) {
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const eventIndex = EventIndexPeg.get();
|
|
|
|
const roomId = this.props.roomId;
|
|
|
|
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
|
|
|
|
if (client.isRoomEncrypted(roomId) && eventIndex !== null) {
|
|
|
|
return eventIndex.paginateTimelineWindow(room, timelineWindow, direction, limit);
|
|
|
|
} else {
|
|
|
|
return timelineWindow.paginate(direction, limit);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-01-14 13:20:56 +03:00
|
|
|
async updateTimelineSet(roomId: string) {
|
2017-10-11 19:56:17 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const room = client.getRoom(roomId);
|
2020-01-15 14:05:43 +03:00
|
|
|
const eventIndex = EventIndexPeg.get();
|
2016-09-07 17:43:14 +03:00
|
|
|
|
2017-05-13 00:38:57 +03:00
|
|
|
this.noRoom = !room;
|
|
|
|
|
2016-09-07 17:43:14 +03:00
|
|
|
if (room) {
|
2020-01-15 14:05:43 +03:00
|
|
|
let timelineSet;
|
|
|
|
|
2020-01-14 13:20:56 +03:00
|
|
|
try {
|
2020-01-17 13:52:20 +03:00
|
|
|
timelineSet = await this.fetchFileEventsServer(room);
|
2020-01-15 14:05:43 +03:00
|
|
|
|
|
|
|
if (client.isRoomEncrypted(roomId) && eventIndex !== null) {
|
2020-01-17 12:04:53 +03:00
|
|
|
const timeline = timelineSet.getLiveTimeline();
|
2020-01-22 18:11:54 +03:00
|
|
|
await eventIndex.populateFileTimeline(timelineSet, timeline, room, 10);
|
2020-01-15 14:05:43 +03:00
|
|
|
}
|
|
|
|
|
2020-01-14 13:20:56 +03:00
|
|
|
this.setState({ timelineSet: timelineSet });
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Failed to get or create file panel filter", error);
|
|
|
|
}
|
2017-05-13 00:38:57 +03:00
|
|
|
} else {
|
2016-09-06 02:59:17 +03:00
|
|
|
console.error("Failed to add filtered timelineSet for FilePanel as no room!");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-31 13:58:06 +03:00
|
|
|
render: function() {
|
2017-05-15 12:15:35 +03:00
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
|
|
|
return <div className="mx_FilePanel mx_RoomView_messageListWrapper">
|
2017-06-07 13:40:46 +03:00
|
|
|
<div className="mx_RoomView_empty">
|
2017-11-13 22:19:33 +03:00
|
|
|
{ _t("You must <a>register</a> to use this functionality",
|
|
|
|
{},
|
|
|
|
{ 'a': (sub) => <a href="#/register" key="sub">{ sub }</a> })
|
|
|
|
}
|
2017-06-07 13:40:46 +03:00
|
|
|
</div>
|
2017-05-15 12:15:35 +03:00
|
|
|
</div>;
|
|
|
|
} else if (this.noRoom) {
|
2017-05-13 00:38:57 +03:00
|
|
|
return <div className="mx_FilePanel mx_RoomView_messageListWrapper">
|
2017-10-11 19:56:17 +03:00
|
|
|
<div className="mx_RoomView_empty">{ _t("You must join the room to see its files") }</div>
|
2017-05-13 00:38:57 +03:00
|
|
|
</div>;
|
|
|
|
}
|
|
|
|
|
2016-08-31 13:58:06 +03:00
|
|
|
// wrap a TimelinePanel with the jump-to-event bits turned off.
|
2017-10-11 19:56:17 +03:00
|
|
|
const TimelinePanel = sdk.getComponent("structures.TimelinePanel");
|
|
|
|
const Loader = sdk.getComponent("elements.Spinner");
|
2016-08-31 13:58:06 +03:00
|
|
|
|
2016-09-06 03:44:55 +03:00
|
|
|
if (this.state.timelineSet) {
|
2016-09-10 03:39:19 +03:00
|
|
|
// console.log("rendering TimelinePanel for timelineSet " + this.state.timelineSet.room.roomId + " " +
|
|
|
|
// "(" + this.state.timelineSet._timelines.join(", ") + ")" + " with key " + this.props.roomId);
|
2016-09-06 03:44:55 +03:00
|
|
|
return (
|
2019-10-14 12:44:42 +03:00
|
|
|
<div className="mx_FilePanel" role="tabpanel">
|
|
|
|
<TimelinePanel key={"filepanel_" + this.props.roomId}
|
|
|
|
manageReadReceipts={false}
|
|
|
|
manageReadMarkers={false}
|
|
|
|
timelineSet={this.state.timelineSet}
|
|
|
|
showUrlPreview = {false}
|
2020-01-17 12:04:53 +03:00
|
|
|
onPaginationRequest={this.onPaginationRequest}
|
2019-10-14 12:44:42 +03:00
|
|
|
tileShape="file_grid"
|
|
|
|
resizeNotifier={this.props.resizeNotifier}
|
|
|
|
empty={_t('There are no visible files in this room')}
|
|
|
|
/>
|
|
|
|
</div>
|
2016-09-06 03:44:55 +03:00
|
|
|
);
|
2017-10-11 19:56:17 +03:00
|
|
|
} else {
|
2016-09-07 17:43:14 +03:00
|
|
|
return (
|
2019-10-03 11:35:39 +03:00
|
|
|
<div className="mx_FilePanel" role="tabpanel">
|
2017-10-11 19:56:17 +03:00
|
|
|
<Loader />
|
2016-09-07 17:43:14 +03:00
|
|
|
</div>
|
|
|
|
);
|
2016-09-06 03:44:55 +03:00
|
|
|
}
|
2016-08-31 13:58:06 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-12-20 03:45:24 +03:00
|
|
|
export default FilePanel;
|