2016-08-31 10:58:06 +00:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
2022-02-23 14:03:46 +00:00
|
|
|
Copyright 2019 - 2022 The Matrix.org Foundation C.I.C.
|
2016-08-31 10:58:06 +00: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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React, { createRef } from "react";
|
|
|
|
import { Filter } from "matrix-js-sdk/src/filter";
|
2022-01-26 13:24:14 +00:00
|
|
|
import { EventTimelineSet, IRoomTimelineData } from "matrix-js-sdk/src/models/event-timeline-set";
|
2021-07-01 09:01:48 +00:00
|
|
|
import { Direction } from "matrix-js-sdk/src/models/event-timeline";
|
2022-02-22 12:18:08 +00:00
|
|
|
import { MatrixEvent, MatrixEventEvent } from "matrix-js-sdk/src/models/event";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { Room, RoomEvent } from "matrix-js-sdk/src/models/room";
|
|
|
|
import { TimelineWindow } from "matrix-js-sdk/src/timeline-window";
|
2021-10-22 22:23:32 +00:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-06-29 10:38:19 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { MatrixClientPeg } from "../../MatrixClientPeg";
|
2020-01-17 09:35:33 +00:00
|
|
|
import EventIndexPeg from "../../indexing/EventIndexPeg";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { _t } from "../../languageHandler";
|
2021-10-03 15:38:49 +00:00
|
|
|
import SearchWarning, { WarningKind } from "../views/elements/SearchWarning";
|
2022-01-05 15:14:44 +00:00
|
|
|
import BaseCard from "../views/right_panel/BaseCard";
|
2022-12-12 11:24:14 +00:00
|
|
|
import ResizeNotifier from "../../utils/ResizeNotifier";
|
2021-07-02 15:08:27 +00:00
|
|
|
import TimelinePanel from "./TimelinePanel";
|
|
|
|
import Spinner from "../views/elements/Spinner";
|
2021-11-17 15:19:30 +00:00
|
|
|
import { Layout } from "../../settings/enums/Layout";
|
2022-12-12 11:24:14 +00:00
|
|
|
import RoomContext, { TimelineRenderingType } from "../../contexts/RoomContext";
|
|
|
|
import Measured from "../views/elements/Measured";
|
2021-06-29 10:38:19 +00:00
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
roomId: string;
|
|
|
|
onClose: () => void;
|
2021-07-01 22:23:03 +00:00
|
|
|
resizeNotifier: ResizeNotifier;
|
2021-06-29 10:38:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
2023-02-13 11:39:16 +00:00
|
|
|
timelineSet: EventTimelineSet | null;
|
2022-02-23 14:03:46 +00:00
|
|
|
narrow: boolean;
|
2021-06-29 10:38:19 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 10:58:06 +00:00
|
|
|
/*
|
|
|
|
* Component which shows the filtered file using a TimelinePanel
|
|
|
|
*/
|
2021-06-29 10:38:19 +00:00
|
|
|
class FilePanel extends React.Component<IProps, IState> {
|
2022-12-16 12:29:59 +00:00
|
|
|
public static contextType = RoomContext;
|
2022-02-23 14:03:46 +00:00
|
|
|
|
2020-01-24 13:23:43 +00:00
|
|
|
// This is used to track if a decrypted event was a live event and should be
|
|
|
|
// added to the timeline.
|
2021-06-29 10:38:19 +00:00
|
|
|
private decryptingEvents = new Set<string>();
|
|
|
|
public noRoom: boolean;
|
2022-02-23 14:03:46 +00:00
|
|
|
private card = createRef<HTMLDivElement>();
|
2016-09-05 23:59:17 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public state: IState = {
|
2020-08-29 11:14:16 +00:00
|
|
|
timelineSet: null,
|
2022-02-23 14:03:46 +00:00
|
|
|
narrow: false,
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2016-09-05 23:59:17 +00:00
|
|
|
|
2022-01-26 13:24:14 +00:00
|
|
|
private onRoomTimeline = (
|
|
|
|
ev: MatrixEvent,
|
2023-02-24 15:28:40 +00:00
|
|
|
room: Room | undefined,
|
2023-05-12 08:49:37 +00:00
|
|
|
toStartOfTimeline: boolean | undefined,
|
2022-01-26 13:24:14 +00:00
|
|
|
removed: boolean,
|
|
|
|
data: IRoomTimelineData,
|
|
|
|
): void => {
|
2023-05-09 17:24:40 +00:00
|
|
|
if (room?.roomId !== this.props.roomId) return;
|
2020-01-22 15:21:11 +00:00
|
|
|
if (toStartOfTimeline || !data || !data.liveEvent || ev.isRedacted()) return;
|
|
|
|
|
2021-05-18 10:41:20 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
2021-05-18 12:01:38 +00:00
|
|
|
client.decryptEventIfNeeded(ev);
|
2021-05-18 09:02:21 +00:00
|
|
|
|
2020-01-22 15:21:11 +00:00
|
|
|
if (ev.isBeingDecrypted()) {
|
2023-02-24 15:28:40 +00:00
|
|
|
this.decryptingEvents.add(ev.getId()!);
|
2020-01-22 15:21:11 +00:00
|
|
|
} else {
|
|
|
|
this.addEncryptedLiveEvent(ev);
|
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2020-01-22 15:21:11 +00:00
|
|
|
|
2021-06-29 11:18:41 +00:00
|
|
|
private onEventDecrypted = (ev: MatrixEvent, err?: any): void => {
|
2020-01-22 15:21:11 +00:00
|
|
|
if (ev.getRoomId() !== this.props.roomId) return;
|
2023-02-24 15:28:40 +00:00
|
|
|
const eventId = ev.getId()!;
|
2020-01-22 15:21:11 +00:00
|
|
|
|
|
|
|
if (!this.decryptingEvents.delete(eventId)) return;
|
|
|
|
if (err) return;
|
|
|
|
|
|
|
|
this.addEncryptedLiveEvent(ev);
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2020-01-22 15:21:11 +00:00
|
|
|
|
2021-06-29 11:18:41 +00:00
|
|
|
public addEncryptedLiveEvent(ev: MatrixEvent): void {
|
2020-01-22 15:21:11 +00:00
|
|
|
if (!this.state.timelineSet) return;
|
|
|
|
|
|
|
|
const timeline = this.state.timelineSet.getLiveTimeline();
|
|
|
|
if (ev.getType() !== "m.room.message") return;
|
2023-03-10 14:55:06 +00:00
|
|
|
if (!["m.file", "m.image", "m.video", "m.audio"].includes(ev.getContent().msgtype!)) {
|
2020-01-22 15:21:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-24 15:28:40 +00:00
|
|
|
if (!this.state.timelineSet.eventIdToTimeline(ev.getId()!)) {
|
2020-01-22 15:21:11 +00:00
|
|
|
this.state.timelineSet.addEventToTimeline(ev, timeline, false);
|
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2020-01-22 15:21:11 +00:00
|
|
|
|
2021-06-29 11:18:41 +00:00
|
|
|
public async componentDidMount(): Promise<void> {
|
2020-01-22 15:21:11 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
2020-01-14 10:20:56 +00:00
|
|
|
await this.updateTimelineSet(this.props.roomId);
|
2020-01-22 15:21:11 +00:00
|
|
|
|
|
|
|
if (!MatrixClientPeg.get().isRoomEncrypted(this.props.roomId)) return;
|
|
|
|
|
2020-01-24 13:23:43 +00: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 15:21:11 +00:00
|
|
|
if (EventIndexPeg.get() !== null) {
|
2022-02-22 12:18:08 +00:00
|
|
|
client.on(RoomEvent.Timeline, this.onRoomTimeline);
|
|
|
|
client.on(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
2020-01-22 15:21:11 +00:00
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2020-01-22 15:21:11 +00:00
|
|
|
|
2021-06-29 11:18:41 +00:00
|
|
|
public componentWillUnmount(): void {
|
2020-01-22 15:21:11 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (client === null) return;
|
|
|
|
|
|
|
|
if (!MatrixClientPeg.get().isRoomEncrypted(this.props.roomId)) return;
|
|
|
|
|
|
|
|
if (EventIndexPeg.get() !== null) {
|
2022-02-22 12:18:08 +00:00
|
|
|
client.removeListener(RoomEvent.Timeline, this.onRoomTimeline);
|
|
|
|
client.removeListener(MatrixEventEvent.Decrypted, this.onEventDecrypted);
|
2020-01-22 15:21:11 +00:00
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2016-09-07 14:43:14 +00:00
|
|
|
|
2021-07-01 08:53:54 +00:00
|
|
|
public async fetchFileEventsServer(room: Room): Promise<EventTimelineSet> {
|
2020-01-14 10:20:56 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
|
2020-01-22 15:26:40 +00:00
|
|
|
const filter = new Filter(client.credentials.userId);
|
2022-12-12 11:24:14 +00:00
|
|
|
filter.setDefinition({
|
|
|
|
room: {
|
|
|
|
timeline: {
|
|
|
|
contains_url: true,
|
|
|
|
types: ["m.room.message"],
|
2020-01-14 10:20:56 +00:00
|
|
|
},
|
|
|
|
},
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2020-01-14 10:20:56 +00:00
|
|
|
|
2023-04-17 09:37:29 +00:00
|
|
|
filter.filterId = await client.getOrCreateFilter("FILTER_FILES_" + client.credentials.userId, filter);
|
|
|
|
return room.getOrCreateFilteredTimelineSet(filter);
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2020-01-14 10:20:56 +00:00
|
|
|
|
2021-07-01 09:01:48 +00:00
|
|
|
private onPaginationRequest = (
|
|
|
|
timelineWindow: TimelineWindow,
|
|
|
|
direction: Direction,
|
|
|
|
limit: number,
|
|
|
|
): Promise<boolean> => {
|
2020-01-17 09:04:53 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const eventIndex = EventIndexPeg.get();
|
|
|
|
const roomId = this.props.roomId;
|
|
|
|
|
|
|
|
const room = client.getRoom(roomId);
|
|
|
|
|
2020-01-24 13:23:43 +00: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
|
2023-03-10 14:55:06 +00:00
|
|
|
if (room && client.isRoomEncrypted(roomId) && eventIndex !== null) {
|
2020-01-17 09:04:53 +00:00
|
|
|
return eventIndex.paginateTimelineWindow(room, timelineWindow, direction, limit);
|
|
|
|
} else {
|
|
|
|
return timelineWindow.paginate(direction, limit);
|
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
};
|
2020-01-17 09:04:53 +00:00
|
|
|
|
2022-02-23 14:03:46 +00:00
|
|
|
private onMeasurement = (narrow: boolean): void => {
|
|
|
|
this.setState({ narrow });
|
|
|
|
};
|
|
|
|
|
2021-06-29 11:18:41 +00:00
|
|
|
public async updateTimelineSet(roomId: string): Promise<void> {
|
2017-10-11 16:56:17 +00:00
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
const room = client.getRoom(roomId);
|
2020-01-15 11:05:43 +00:00
|
|
|
const eventIndex = EventIndexPeg.get();
|
2016-09-07 14:43:14 +00:00
|
|
|
|
2017-05-12 21:38:57 +00:00
|
|
|
this.noRoom = !room;
|
|
|
|
|
2016-09-07 14:43:14 +00:00
|
|
|
if (room) {
|
2020-01-15 11:05:43 +00:00
|
|
|
let timelineSet;
|
|
|
|
|
2020-01-14 10:20:56 +00:00
|
|
|
try {
|
2020-01-17 10:52:20 +00:00
|
|
|
timelineSet = await this.fetchFileEventsServer(room);
|
2020-01-15 11:05:43 +00:00
|
|
|
|
2020-01-24 13:23:43 +00: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 11:05:43 +00:00
|
|
|
if (client.isRoomEncrypted(roomId) && eventIndex !== null) {
|
2020-01-17 09:04:53 +00:00
|
|
|
const timeline = timelineSet.getLiveTimeline();
|
2020-01-22 15:11:54 +00:00
|
|
|
await eventIndex.populateFileTimeline(timelineSet, timeline, room, 10);
|
2020-01-15 11:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 10:20:56 +00:00
|
|
|
this.setState({ timelineSet: timelineSet });
|
|
|
|
} catch (error) {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("Failed to get or create file panel filter", error);
|
2020-01-14 10:20:56 +00:00
|
|
|
}
|
2017-05-12 21:38:57 +00:00
|
|
|
} else {
|
2021-10-15 14:30:53 +00:00
|
|
|
logger.error("Failed to add filtered timelineSet for FilePanel as no room!");
|
2016-09-05 23:59:17 +00:00
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
2016-09-05 23:59:17 +00:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2017-05-15 09:15:35 +00:00
|
|
|
if (MatrixClientPeg.get().isGuest()) {
|
2022-12-12 11:24:14 +00:00
|
|
|
return (
|
|
|
|
<BaseCard className="mx_FilePanel mx_RoomView_messageListWrapper" onClose={this.props.onClose}>
|
|
|
|
<div className="mx_RoomView_empty">
|
|
|
|
{_t(
|
|
|
|
"You must <a>register</a> to use this functionality",
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
a: (sub) => (
|
|
|
|
<a href="#/register" key="sub">
|
|
|
|
{sub}
|
|
|
|
</a>
|
|
|
|
),
|
|
|
|
},
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</BaseCard>
|
|
|
|
);
|
2017-05-15 09:15:35 +00:00
|
|
|
} else if (this.noRoom) {
|
2022-12-12 11:24:14 +00:00
|
|
|
return (
|
|
|
|
<BaseCard className="mx_FilePanel mx_RoomView_messageListWrapper" onClose={this.props.onClose}>
|
|
|
|
<div className="mx_RoomView_empty">{_t("You must join the room to see its files")}</div>
|
|
|
|
</BaseCard>
|
|
|
|
);
|
2017-05-12 21:38:57 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 10:58:06 +00:00
|
|
|
// wrap a TimelinePanel with the jump-to-event bits turned off.
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const emptyState = (
|
|
|
|
<div className="mx_RightPanel_empty mx_FilePanel_empty">
|
|
|
|
<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>
|
|
|
|
</div>
|
|
|
|
);
|
2020-07-31 16:31:22 +00:00
|
|
|
|
2020-09-16 23:23:37 +00:00
|
|
|
const isRoomEncrypted = this.noRoom ? false : MatrixClientPeg.get().isRoomEncrypted(this.props.roomId);
|
|
|
|
|
2016-09-06 00:44:55 +00:00
|
|
|
if (this.state.timelineSet) {
|
|
|
|
return (
|
2022-12-12 11:24:14 +00:00
|
|
|
<RoomContext.Provider
|
|
|
|
value={{
|
|
|
|
...this.context,
|
|
|
|
timelineRenderingType: TimelineRenderingType.File,
|
|
|
|
narrow: this.state.narrow,
|
|
|
|
}}
|
|
|
|
>
|
2021-10-11 09:26:05 +00:00
|
|
|
<BaseCard
|
|
|
|
className="mx_FilePanel"
|
|
|
|
onClose={this.props.onClose}
|
|
|
|
withoutScrollContainer
|
2022-02-23 14:03:46 +00:00
|
|
|
ref={this.card}
|
2021-10-11 09:26:05 +00:00
|
|
|
>
|
2023-04-20 08:49:10 +00:00
|
|
|
{this.card.current && (
|
|
|
|
<Measured sensor={this.card.current} onMeasurement={this.onMeasurement} />
|
|
|
|
)}
|
2021-10-03 15:38:49 +00:00
|
|
|
<SearchWarning isRoomEncrypted={isRoomEncrypted} kind={WarningKind.Files} />
|
2021-10-11 09:26:05 +00:00
|
|
|
<TimelinePanel
|
|
|
|
manageReadReceipts={false}
|
|
|
|
manageReadMarkers={false}
|
|
|
|
timelineSet={this.state.timelineSet}
|
|
|
|
showUrlPreview={false}
|
|
|
|
onPaginationRequest={this.onPaginationRequest}
|
|
|
|
resizeNotifier={this.props.resizeNotifier}
|
|
|
|
empty={emptyState}
|
|
|
|
layout={Layout.Group}
|
|
|
|
/>
|
|
|
|
</BaseCard>
|
|
|
|
</RoomContext.Provider>
|
2016-09-06 00:44:55 +00:00
|
|
|
);
|
2017-10-11 16:56:17 +00:00
|
|
|
} else {
|
2016-09-07 14:43:14 +00:00
|
|
|
return (
|
2022-12-12 11:24:14 +00:00
|
|
|
<RoomContext.Provider
|
|
|
|
value={{
|
|
|
|
...this.context,
|
|
|
|
timelineRenderingType: TimelineRenderingType.File,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<BaseCard className="mx_FilePanel" onClose={this.props.onClose}>
|
2021-10-11 09:26:05 +00:00
|
|
|
<Spinner />
|
|
|
|
</BaseCard>
|
|
|
|
</RoomContext.Provider>
|
2016-09-07 14:43:14 +00:00
|
|
|
);
|
2016-09-06 00:44:55 +00:00
|
|
|
}
|
2020-08-29 11:14:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-31 10:58:06 +00:00
|
|
|
|
2019-12-20 00:45:24 +00:00
|
|
|
export default FilePanel;
|