From cf3b4a5e4423e73815962b567f2c510e4271162d Mon Sep 17 00:00:00 2001 From: Arnei Date: Wed, 16 Nov 2022 17:01:32 +0100 Subject: [PATCH] Add a URL panel to the right panel Adds a new panel the right menu that, akin to "Files", shows all messages in the room containing at least one URL. This would help users find previously posted URLs without having to come up with the correct search term(s) to find them. Code-wise this more of a proof of concept. `UrlPanel.tsx` is almost completely copy and pasted from `FilePanel.tsx`. `UrlPanel.tsx` should probably either be stripped down or both of them should be abstracted into a generic class. Furthermore, the type of `filter` necessary for this PR does not seem to exist (the one suggested by this PR is probably quite a hack), and I am unsure what the best course of action in creating such a filter would be. Any advice on that and on how/why synapse should be involved would be appreciated. Signed-off-by: Arne Wilken arnepokemon@yahoo.de --- .../views/right_panel/_RoomSummaryCard.pcss | 4 + src/components/structures/RightPanel.tsx | 4 + src/components/structures/UrlPanel.tsx | 323 ++++++++++++++++++ .../views/context_menus/RoomContextMenu.tsx | 17 + .../views/right_panel/RoomHeaderButtons.tsx | 1 + .../views/right_panel/RoomSummaryCard.tsx | 7 + src/i18n/strings/en_EN.json | 7 + .../right-panel/RightPanelStorePhases.ts | 1 + 8 files changed, 364 insertions(+) create mode 100644 src/components/structures/UrlPanel.tsx diff --git a/res/css/views/right_panel/_RoomSummaryCard.pcss b/res/css/views/right_panel/_RoomSummaryCard.pcss index b81f1b64b08..b98dd3703a1 100644 --- a/res/css/views/right_panel/_RoomSummaryCard.pcss +++ b/res/css/views/right_panel/_RoomSummaryCard.pcss @@ -247,6 +247,10 @@ limitations under the License. mask-image: url('$(res)/img/element-icons/room/files.svg'); } +.mx_RoomSummaryCard_icon_urls::before { + mask-image: url('$(res)/img/element-icons/link.svg'); +} + .mx_RoomSummaryCard_icon_pins::before { mask-image: url('$(res)/img/element-icons/room/pin-upright.svg'); } diff --git a/src/components/structures/RightPanel.tsx b/src/components/structures/RightPanel.tsx index f0c0eb87864..822098596ad 100644 --- a/src/components/structures/RightPanel.tsx +++ b/src/components/structures/RightPanel.tsx @@ -44,6 +44,7 @@ import TimelineCard from '../views/right_panel/TimelineCard'; import { UPDATE_EVENT } from '../../stores/AsyncStore'; import { IRightPanelCard, IRightPanelCardState } from '../../stores/right-panel/RightPanelStoreIPanelState'; import { Action } from '../../dispatcher/actions'; +import UrlPanel from './UrlPanel'; interface IProps { room?: Room; // if showing panels for a given room, this is set @@ -220,6 +221,9 @@ export default class RightPanel extends React.Component { case RightPanelPhases.FilePanel: card = ; break; + case RightPanelPhases.UrlPanel: + card = ; + break; case RightPanelPhases.ThreadView: card = void; + resizeNotifier: ResizeNotifier; +} + +interface IState { + timelineSet: EventTimelineSet; + narrow: boolean; +} + +/* + * Component which shows the filtered urls using a TimelinePanel + */ +class UrlPanel extends React.Component { + static contextType = RoomContext; + + // This is used to track if a decrypted event was a live event and should be + // added to the timeline. + private decryptingEvents = new Set(); + public noRoom: boolean; + private card = createRef(); + + state = { + timelineSet: null, + narrow: false, + }; + + private onRoomTimeline = ( + ev: MatrixEvent, + room: Room | null, + toStartOfTimeline: boolean, + removed: boolean, + data: IRoomTimelineData, + ): void => { + if (room?.roomId !== this.props?.roomId) return; + if (toStartOfTimeline || !data || !data.liveEvent || ev.isRedacted()) return; + + const client = MatrixClientPeg.get(); + client.decryptEventIfNeeded(ev); + + if (ev.isBeingDecrypted()) { + this.decryptingEvents.add(ev.getId()); + } else { + this.addEncryptedLiveEvent(ev); + } + }; + + private onEventDecrypted = (ev: MatrixEvent, err?: any): void => { + if (ev.getRoomId() !== this.props.roomId) return; + const eventId = ev.getId(); + + if (!this.decryptingEvents.delete(eventId)) return; + if (err) return; + + this.addEncryptedLiveEvent(ev); + }; + + public addEncryptedLiveEvent(ev: MatrixEvent): void { + 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); + } + } + + public async componentDidMount(): Promise { + const client = MatrixClientPeg.get(); + + await this.updateTimelineSet(this.props.roomId); + + if (!MatrixClientPeg.get().isRoomEncrypted(this.props.roomId)) return; + + // 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. + if (EventIndexPeg.get() !== null) { + client.on(RoomEvent.Timeline, this.onRoomTimeline); + client.on(MatrixEventEvent.Decrypted, this.onEventDecrypted); + } + } + + public componentWillUnmount(): void { + const client = MatrixClientPeg.get(); + if (client === null) return; + + if (!MatrixClientPeg.get().isRoomEncrypted(this.props.roomId)) return; + + if (EventIndexPeg.get() !== null) { + client.removeListener(RoomEvent.Timeline, this.onRoomTimeline); + client.removeListener(MatrixEventEvent.Decrypted, this.onEventDecrypted); + } + } + + public async fetchFileEventsServer(room: Room): Promise { + const client = MatrixClientPeg.get(); + + const filter = new Filter(client.credentials.userId); + filter.setDefinition( + { + "room": { + "timeline": { + "types": [ + "m.room.message", + ], + }, + }, + }, + ); + // The "message_with_url" filter does not exist in synapse. + // So we just add it after the fact, since we have to do the filtering + // here in the frontend again anyway. + const filterId = await client.getOrCreateFilter("FILTER_URLS_" + client.credentials.userId, filter); + filter.setDefinition( + { + "room": { + "timeline": { + "message_with_url": true, + "contains_url": false, // exclude files + "types": [ + "m.room.message", + ], + }, + }, + }, + ); + filter.filterId = filterId; + const timelineSet = room.getOrCreateFilteredTimelineSet(filter); + + return timelineSet; + } + + private onPaginationRequest = ( + timelineWindow: TimelineWindow, + direction: Direction, + limit: number, + ): Promise => { + const client = MatrixClientPeg.get(); + const eventIndex = EventIndexPeg.get(); + const roomId = this.props.roomId; + + const room = client.getRoom(roomId); + + // 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 + if (client.isRoomEncrypted(roomId) && eventIndex !== null) { + return eventIndex.paginateTimelineWindow(room, timelineWindow, direction, limit); + } else { + return timelineWindow.paginate(direction, limit); + } + }; + + private onMeasurement = (narrow: boolean): void => { + this.setState({ narrow }); + }; + + public async updateTimelineSet(roomId: string): Promise { + const client = MatrixClientPeg.get(); + const room = client.getRoom(roomId); + const eventIndex = EventIndexPeg.get(); + + this.noRoom = !room; + + if (room) { + let timelineSet; + + try { + timelineSet = await this.fetchFileEventsServer(room); + + // 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. + if (client.isRoomEncrypted(roomId) && eventIndex !== null) { + const timeline = timelineSet.getLiveTimeline(); + await eventIndex.populateFileTimeline(timelineSet, timeline, room, 10); + } + + this.setState({ timelineSet: timelineSet }); + } catch (error) { + logger.error("Failed to get or create url panel filter", error); + } + } else { + logger.error("Failed to add filtered timelineSet for UrlPanel as no room!"); + } + } + + public render() { + if (MatrixClientPeg.get().isGuest()) { + return +
+ { _t("You must register to use this functionality", + {}, + { 'a': (sub) => { sub } }) + } +
+
; + } else if (this.noRoom) { + return +
{ _t("You must join the room to see its urls") }
+
; + } + + // wrap a TimelinePanel with the jump-to-event bits turned off. + + const emptyState = (
+

{ _t('No urls visible in this room') }

+
); + + const isRoomEncrypted = this.noRoom ? false : MatrixClientPeg.get().isRoomEncrypted(this.props.roomId); + + if (this.state.timelineSet) { + return ( + + + + + + + + ); + } else { + return ( + + + + + + ); + } + } +} + +export default UrlPanel; diff --git a/src/components/views/context_menus/RoomContextMenu.tsx b/src/components/views/context_menus/RoomContextMenu.tsx index b9923d92782..91073938fb6 100644 --- a/src/components/views/context_menus/RoomContextMenu.tsx +++ b/src/components/views/context_menus/RoomContextMenu.tsx @@ -254,6 +254,22 @@ const RoomContextMenu = ({ room, onFinished, ...props }: IProps) => { />; } + let urlsOption: JSX.Element; + if (!isVideoRoom) { + urlsOption = { + ev.preventDefault(); + ev.stopPropagation(); + + ensureViewingRoom(ev); + RightPanelStore.instance.pushCard({ phase: RightPanelPhases.UrlPanel }, false); + onFinished(); + }} + label={_t("Urls")} + iconClassName=".mx_RoomTile_iconCopyLink" + />; + } + const pinningEnabled = useFeatureEnabled("feature_pinning"); const pinCount = usePinnedEvents(pinningEnabled && room)?.length; @@ -348,6 +364,7 @@ const RoomContextMenu = ({ room, onFinished, ...props }: IProps) => { { favouriteOption } { peopleOption } { filesOption } + { urlsOption } { pinsOption } { widgetsOption } { lowPriorityOption } diff --git a/src/components/views/right_panel/RoomHeaderButtons.tsx b/src/components/views/right_panel/RoomHeaderButtons.tsx index 262b8fc38d6..536f3949923 100644 --- a/src/components/views/right_panel/RoomHeaderButtons.tsx +++ b/src/components/views/right_panel/RoomHeaderButtons.tsx @@ -48,6 +48,7 @@ const ROOM_INFO_PHASES = [ RightPanelPhases.RoomSummary, RightPanelPhases.Widget, RightPanelPhases.FilePanel, + RightPanelPhases.UrlPanel, RightPanelPhases.RoomMemberList, RightPanelPhases.RoomMemberInfo, RightPanelPhases.EncryptionPanel, diff --git a/src/components/views/right_panel/RoomSummaryCard.tsx b/src/components/views/right_panel/RoomSummaryCard.tsx index 006a00f3a2f..75602c439fe 100644 --- a/src/components/views/right_panel/RoomSummaryCard.tsx +++ b/src/components/views/right_panel/RoomSummaryCard.tsx @@ -237,6 +237,10 @@ const onRoomFilesClick = () => { RightPanelStore.instance.pushCard({ phase: RightPanelPhases.FilePanel }, true); }; +const onRoomUrlsClick = () => { + RightPanelStore.instance.pushCard({ phase: RightPanelPhases.UrlPanel }, true); +}; + const onRoomPinsClick = () => { RightPanelStore.instance.pushCard({ phase: RightPanelPhases.PinnedMessages }, true); }; @@ -311,6 +315,9 @@ const RoomSummaryCard: React.FC = ({ room, onClose }) => { { !isVideoRoom && } + { !isVideoRoom && } { pinningEnabled && !isVideoRoom &&