diff --git a/apps/web/src/components/structures/TimelinePanel.tsx b/apps/web/src/components/structures/TimelinePanel.tsx index e2e5d8ab50..3072ff406a 100644 --- a/apps/web/src/components/structures/TimelinePanel.tsx +++ b/apps/web/src/components/structures/TimelinePanel.tsx @@ -1609,8 +1609,16 @@ class TimelinePanel extends React.Component { // if we're at the end of the live timeline, append the pending events if (!this.timelineWindow!.canPaginate(EventTimeline.FORWARDS)) { const pendingEvents = this.props.timelineSet.getPendingEvents(); + // Pending events are the whole room's, so a filtered timeline set is handed events its + // own filter would never accept. Without this every message being sent flickers through + // panels like the file panel until its remote echo arrives and the filter rejects it. + const filter = this.props.timelineSet.getFilter?.(); events.push( ...pendingEvents.filter((event) => { + if (filter && !filter.filterRoomTimeline([event]).length) { + return false; + } + const { shouldLiveInRoom, threadId } = this.props.timelineSet.room!.eventShouldLiveIn( event, pendingEvents, diff --git a/apps/web/test/unit-tests/components/structures/FilePanel-test.tsx b/apps/web/test/unit-tests/components/structures/FilePanel-test.tsx index acbf699c42..ea0e4f0287 100644 --- a/apps/web/test/unit-tests/components/structures/FilePanel-test.tsx +++ b/apps/web/test/unit-tests/components/structures/FilePanel-test.tsx @@ -7,7 +7,7 @@ Please see LICENSE files in the repository root for full details. */ import React from "react"; -import { EventTimelineSet, PendingEventOrdering, Room, RoomEvent } from "matrix-js-sdk/src/matrix"; +import { EventStatus, EventTimelineSet, Filter, PendingEventOrdering, Room, RoomEvent } from "matrix-js-sdk/src/matrix"; import { screen, render, waitFor } from "jest-matrix-react"; import { mocked } from "jest-mock"; @@ -49,6 +49,39 @@ describe("FilePanel", () => { expect(asFragment()).toMatchSnapshot(); }); + it("does not show a pending message that its filter rejects", async () => { + const cli = MatrixClientPeg.safeGet(); + const room = new Room("!room:server", cli, cli.getSafeUserId(), { + pendingEventOrdering: PendingEventOrdering.Detached, + }); + + const filter = new Filter(cli.getSafeUserId()); + filter.setDefinition({ room: { timeline: { contains_url: true, types: ["m.room.message"] } } }); + const timelineSet = new EventTimelineSet(room, { filter }); + room.getOrCreateFilteredTimelineSet = jest.fn().mockReturnValue(timelineSet); + mocked(cli.getRoom).mockReturnValue(room); + + // A text message being sent carries no url, so the file filter rejects it. It is still one of + // the room's pending events, which the panel is handed in full. + const pending = mkEvent({ + event: true, + type: "m.room.message", + user: cli.getSafeUserId(), + room: room.roomId, + content: { msgtype: "m.text", body: "a message being sent" }, + }); + pending.setStatus(EventStatus.SENDING); + room.addPendingEvent(pending, "txn-text"); + + render( + , + clientAndSDKContextRenderOptions(cli, SDKContextClass.instance), + ); + await screen.findByText("No files visible in this room"); + + expect(screen.queryByText("a message being sent")).not.toBeInTheDocument(); + }); + describe("addEncryptedLiveEvent", () => { it("should add file msgtype event to filtered timelineSet", async () => { const cli = MatrixClientPeg.safeGet();