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';
|
2016-08-31 13:58:06 +03:00
|
|
|
|
2021-06-29 15:26:09 +03:00
|
|
|
import { Filter } from 'matrix-js-sdk/src/filter';
|
2021-06-29 13:38:19 +03:00
|
|
|
import { EventTimelineSet } from "matrix-js-sdk/src/models/event-timeline-set";
|
2021-07-01 12:01:48 +03:00
|
|
|
import { Direction } from "matrix-js-sdk/src/models/event-timeline";
|
2021-06-29 14:18:41 +03:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
import { Room } from 'matrix-js-sdk/src/models/room';
|
2021-06-29 13:38:19 +03:00
|
|
|
import { TimelineWindow } from 'matrix-js-sdk/src/timeline-window';
|
|
|
|
|
2021-06-29 15:26:09 +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';
|
2020-09-08 12:19:51 +03:00
|
|
|
import BaseCard from "../views/right_panel/BaseCard";
|
2021-06-29 15:26:09 +03:00
|
|
|
import { RightPanelPhases } from "../../stores/RightPanelStorePhases";
|
|
|
|
import DesktopBuildsNotice, { WarningKind } from "../views/elements/DesktopBuildsNotice";
|
|
|
|
import { replaceableComponent } from "../../utils/replaceableComponent";
|
2016-08-31 13:58:06 +03:00
|
|
|
|
2021-06-29 13:38:19 +03:00
|
|
|
import ResizeNotifier from '../../utils/ResizeNotifier';
|
2021-07-02 18:08:27 +03:00
|
|
|
import TimelinePanel from "./TimelinePanel";
|
|
|
|
import Spinner from "../views/elements/Spinner";
|
|
|
|
import { TileShape } from '../views/rooms/EventTile';
|
2021-07-22 17:00:41 +03:00
|
|
|
import { Layout } from "../../settings/Layout";
|
2021-06-29 13:38:19 +03:00
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
roomId: string;
|
|
|
|
onClose: () => void;
|
2021-07-02 01:23:03 +03:00
|
|
|
resizeNotifier: ResizeNotifier;
|
2021-06-29 13:38:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
|
|
|
timelineSet: EventTimelineSet;
|
|
|
|
}
|
|
|
|
|
2016-08-31 13:58:06 +03:00
|
|
|
/*
|
|
|
|
* Component which shows the filtered file using a TimelinePanel
|
|
|
|
*/
|
2021-03-09 05:35:10 +03:00
|
|
|
@replaceableComponent("structures.FilePanel")
|
2021-06-29 13:38:19 +03:00
|
|
|
class FilePanel extends React.Component<IProps, IState> {
|
2020-01-24 16:23:43 +03:00
|
|
|
// This is used to track if a decrypted event was a live event and should be
|
|
|
|
// added to the timeline.
|
2021-06-29 13:38:19 +03:00
|
|
|
private decryptingEvents = new Set<string>();
|
|
|
|
public noRoom: boolean;
|
2016-09-06 02:59:17 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
state = {
|
|
|
|
timelineSet: null,
|
|
|
|
};
|
2016-09-06 02:59:17 +03:00
|
|
|
|
2021-06-29 14:18:41 +03:00
|
|
|
private onRoomTimeline = (ev: MatrixEvent, room: Room, toStartOfTimeline: true, removed: true, data: any): void => {
|
2021-01-29 14:15:30 +03:00
|
|
|
if (room?.roomId !== this.props?.roomId) return;
|
2020-01-22 18:21:11 +03:00
|
|
|
if (toStartOfTimeline || !data || !data.liveEvent || ev.isRedacted()) return;
|
|
|
|
|
2021-05-18 13:41:20 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
2021-05-18 15:01:38 +03:00
|
|
|
client.decryptEventIfNeeded(ev);
|
2021-05-18 12:02:21 +03:00
|
|
|
|
2020-01-22 18:21:11 +03:00
|
|
|
if (ev.isBeingDecrypted()) {
|
|
|
|
this.decryptingEvents.add(ev.getId());
|
|
|
|
} else {
|
|
|
|
this.addEncryptedLiveEvent(ev);
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2020-01-22 18:21:11 +03:00
|
|
|
|
2021-06-29 14:18:41 +03:00
|
|
|
private onEventDecrypted = (ev: MatrixEvent, err?: any): void => {
|
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);
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2020-01-22 18:21:11 +03:00
|
|
|
|
2021-06-29 14:18:41 +03:00
|
|
|
public addEncryptedLiveEvent(ev: MatrixEvent): void {
|
2020-01-22 18:21:11 +03:00
|
|
|
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-08-29 14:14:16 +03:00
|
|
|
}
|
2020-01-22 18:21:11 +03:00
|
|
|
|
2021-06-29 14:18:41 +03:00
|
|
|
public async componentDidMount(): Promise<void> {
|
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;
|
|
|
|
|
2020-01-24 16:23:43 +03:00
|
|
|
// The timelineSets filter makes sure that encrypted events that contain
|
|
|
|
// URLs never get added to the timeline, even if they are live events.
|
|
|
|
// These methods are here to manually listen for such events and add
|
|
|
|
// them despite the filter's best efforts.
|
|
|
|
//
|
|
|
|
// We do this only for encrypted rooms and if an event index exists,
|
|
|
|
// this could be made more general in the future or the filter logic
|
|
|
|
// could be fixed.
|
2020-01-22 18:21:11 +03:00
|
|
|
if (EventIndexPeg.get() !== null) {
|
2020-02-20 05:35:30 +03:00
|
|
|
client.on('Room.timeline', this.onRoomTimeline);
|
|
|
|
client.on('Event.decrypted', this.onEventDecrypted);
|
2020-01-22 18:21:11 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2020-01-22 18:21:11 +03:00
|
|
|
|
2021-06-29 14:18:41 +03:00
|
|
|
public componentWillUnmount(): void {
|
2020-01-22 18:21:11 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (client === null) return;
|
|
|
|
|
|
|
|
if (!MatrixClientPeg.get().isRoomEncrypted(this.props.roomId)) return;
|
|
|
|
|
|
|
|
if (EventIndexPeg.get() !== null) {
|
2020-02-20 05:35:30 +03:00
|
|
|
client.removeListener('Room.timeline', this.onRoomTimeline);
|
|
|
|
client.removeListener('Event.decrypted', this.onEventDecrypted);
|
2020-01-22 18:21:11 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-09-07 17:43:14 +03:00
|
|
|
|
2021-07-01 11:53:54 +03:00
|
|
|
public async fetchFileEventsServer(room: Room): Promise<EventTimelineSet> {
|
2020-01-14 13:20:56 +03:00
|
|
|
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-08-29 14:14:16 +03:00
|
|
|
}
|
2020-01-14 13:20:56 +03:00
|
|
|
|
2021-07-01 12:01:48 +03:00
|
|
|
private onPaginationRequest = (
|
|
|
|
timelineWindow: TimelineWindow,
|
|
|
|
direction: Direction,
|
|
|
|
limit: number,
|
|
|
|
): Promise<boolean> => {
|
2020-01-17 12:04:53 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const eventIndex = EventIndexPeg.get();
|
|
|
|
const roomId = this.props.roomId;
|
|
|
|
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
|
2020-01-24 16:23:43 +03:00
|
|
|
// We override the pagination request for encrypted rooms so that we ask
|
|
|
|
// the event index to fulfill the pagination request. Asking the server
|
|
|
|
// to paginate won't ever work since the server can't correctly filter
|
|
|
|
// out events containing URLs
|
2020-01-17 12:04:53 +03:00
|
|
|
if (client.isRoomEncrypted(roomId) && eventIndex !== null) {
|
|
|
|
return eventIndex.paginateTimelineWindow(room, timelineWindow, direction, limit);
|
|
|
|
} else {
|
|
|
|
return timelineWindow.paginate(direction, limit);
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2020-01-17 12:04:53 +03:00
|
|
|
|
2021-06-29 14:18:41 +03:00
|
|
|
public async updateTimelineSet(roomId: string): Promise<void> {
|
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
|
|
|
|
2020-01-24 16:23:43 +03:00
|
|
|
// If this room is encrypted the file panel won't be populated
|
|
|
|
// correctly since the defined filter doesn't support encrypted
|
|
|
|
// events and the server can't check if encrypted events contain
|
|
|
|
// URLs.
|
|
|
|
//
|
|
|
|
// This is where our event index comes into place, we ask the
|
|
|
|
// event index to populate the timelineSet for us. This call
|
|
|
|
// will add 10 events to the live timeline of the set. More can
|
|
|
|
// be requested using pagination.
|
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!");
|
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2016-09-06 02:59:17 +03:00
|
|
|
|
2021-06-29 13:38:19 +03:00
|
|
|
public render() {
|
2017-05-15 12:15:35 +03:00
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
2020-09-08 12:19:51 +03:00
|
|
|
return <BaseCard
|
|
|
|
className="mx_FilePanel mx_RoomView_messageListWrapper"
|
|
|
|
onClose={this.props.onClose}
|
|
|
|
previousPhase={RightPanelPhases.RoomSummary}
|
|
|
|
>
|
2017-06-07 13:40:46 +03:00
|
|
|
<div className="mx_RoomView_empty">
|
2021-04-27 18:23:27 +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>
|
2020-09-08 12:19:51 +03:00
|
|
|
</BaseCard>;
|
2017-05-15 12:15:35 +03:00
|
|
|
} else if (this.noRoom) {
|
2020-09-08 12:19:51 +03:00
|
|
|
return <BaseCard
|
|
|
|
className="mx_FilePanel mx_RoomView_messageListWrapper"
|
|
|
|
onClose={this.props.onClose}
|
|
|
|
previousPhase={RightPanelPhases.RoomSummary}
|
|
|
|
>
|
2017-10-11 19:56:17 +03:00
|
|
|
<div className="mx_RoomView_empty">{ _t("You must join the room to see its files") }</div>
|
2020-09-08 12:19:51 +03:00
|
|
|
</BaseCard>;
|
2017-05-13 00:38:57 +03:00
|
|
|
}
|
|
|
|
|
2016-08-31 13:58:06 +03:00
|
|
|
// wrap a TimelinePanel with the jump-to-event bits turned off.
|
|
|
|
|
2020-07-31 19:31:22 +03:00
|
|
|
const emptyState = (<div className="mx_RightPanel_empty mx_FilePanel_empty">
|
2021-07-20 00:43:11 +03:00
|
|
|
<h2>{ _t('No files visible in this room') }</h2>
|
|
|
|
<p>{ _t('Attach files from chat or just drag and drop them anywhere in a room.') }</p>
|
2020-07-31 19:31:22 +03:00
|
|
|
</div>);
|
|
|
|
|
2020-09-17 02:23:37 +03:00
|
|
|
const isRoomEncrypted = this.noRoom ? false : MatrixClientPeg.get().isRoomEncrypted(this.props.roomId);
|
|
|
|
|
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 (
|
2020-09-08 12:19:51 +03:00
|
|
|
<BaseCard
|
|
|
|
className="mx_FilePanel"
|
|
|
|
onClose={this.props.onClose}
|
|
|
|
previousPhase={RightPanelPhases.RoomSummary}
|
2020-09-09 11:50:08 +03:00
|
|
|
withoutScrollContainer
|
2020-09-08 12:19:51 +03:00
|
|
|
>
|
2020-09-17 02:23:37 +03:00
|
|
|
<DesktopBuildsNotice isRoomEncrypted={isRoomEncrypted} kind={WarningKind.Files} />
|
2020-09-03 19:05:33 +03:00
|
|
|
<TimelinePanel
|
2019-10-14 12:44:42 +03:00
|
|
|
manageReadReceipts={false}
|
|
|
|
manageReadMarkers={false}
|
|
|
|
timelineSet={this.state.timelineSet}
|
2021-07-20 00:43:11 +03:00
|
|
|
showUrlPreview={false}
|
2020-01-17 12:04:53 +03:00
|
|
|
onPaginationRequest={this.onPaginationRequest}
|
2021-07-02 18:08:27 +03:00
|
|
|
tileShape={TileShape.FileGrid}
|
2019-10-14 12:44:42 +03:00
|
|
|
resizeNotifier={this.props.resizeNotifier}
|
2020-07-31 19:31:22 +03:00
|
|
|
empty={emptyState}
|
2021-07-22 17:00:41 +03:00
|
|
|
layout={Layout.Group}
|
2019-10-14 12:44:42 +03:00
|
|
|
/>
|
2020-09-08 12:19:51 +03:00
|
|
|
</BaseCard>
|
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 (
|
2020-09-08 12:19:51 +03:00
|
|
|
<BaseCard
|
|
|
|
className="mx_FilePanel"
|
|
|
|
onClose={this.props.onClose}
|
|
|
|
previousPhase={RightPanelPhases.RoomSummary}
|
|
|
|
>
|
2021-07-02 18:08:27 +03:00
|
|
|
<Spinner />
|
2020-09-08 12:19:51 +03:00
|
|
|
</BaseCard>
|
2016-09-07 17:43:14 +03:00
|
|
|
);
|
2016-09-06 03:44:55 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|
2016-08-31 13:58:06 +03:00
|
|
|
|
2019-12-20 03:45:24 +03:00
|
|
|
export default FilePanel;
|