Skip to content

Commit

Permalink
Merge pull request #28398 from element-hq/dbkr/stateafter
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Nov 27, 2024
2 parents 2b883a8 + e10ecc9 commit c34cbd0
Show file tree
Hide file tree
Showing 35 changed files with 194 additions and 124 deletions.
6 changes: 5 additions & 1 deletion src/components/structures/FilePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ class FilePanel extends React.Component<IProps, IState> {
}

if (!this.state.timelineSet.eventIdToTimeline(ev.getId()!)) {
this.state.timelineSet.addEventToTimeline(ev, timeline, false);
this.state.timelineSet.addEventToTimeline(ev, timeline, {
fromCache: false,
addToState: false,
toStartOfTimeline: false,
});
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/indexing/EventIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,11 @@ export default class EventIndex extends EventEmitter {
// Add the events to the timeline of the file panel.
matrixEvents.forEach((e) => {
if (!timelineSet.eventIdToTimeline(e.getId()!)) {
timelineSet.addEventToTimeline(e, timeline, direction == EventTimeline.BACKWARDS);
timelineSet.addEventToTimeline(e, timeline, {
toStartOfTimeline: direction == EventTimeline.BACKWARDS,
fromCache: false,
addToState: false,
});
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/utils/dm/createDmLocalRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export async function createDmLocalRoom(client: MatrixClient, targets: Member[])

localRoom.targets = targets;
localRoom.updateMyMembership(KnownMembership.Join);
localRoom.addLiveEvents(events);
localRoom.addLiveEvents(events, { addToState: true });
localRoom.currentState.setStateEvents(events);
localRoom.name = localRoom.getDefaultRoomName(client.getUserId()!);
client.store.storeRoom(localRoom);
Expand Down
50 changes: 28 additions & 22 deletions test/test-utils/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,20 @@ export class MockedCall extends Call {
}

public static create(room: Room, id: string) {
room.addLiveEvents([
mkEvent({
event: true,
type: this.EVENT_TYPE,
room: room.roomId,
user: "@alice:example.org",
content: { "m.type": "m.video", "m.intent": "m.prompt" },
skey: id,
ts: Date.now(),
}),
]);
room.addLiveEvents(
[
mkEvent({
event: true,
type: this.EVENT_TYPE,
room: room.roomId,
user: "@alice:example.org",
content: { "m.type": "m.video", "m.intent": "m.prompt" },
skey: id,
ts: Date.now(),
}),
],
{ addToState: true },
);
// @ts-ignore deliberately calling a private method
// Let CallStore know that a call might now exist
CallStore.instance.updateRoom(room);
Expand All @@ -81,17 +84,20 @@ export class MockedCall extends Call {

public destroy() {
// Terminate the call for good measure
this.room.addLiveEvents([
mkEvent({
event: true,
type: MockedCall.EVENT_TYPE,
room: this.room.roomId,
user: "@alice:example.org",
content: { ...this.event.getContent(), "m.terminated": "Call ended" },
skey: this.widget.id,
ts: Date.now(),
}),
]);
this.room.addLiveEvents(
[
mkEvent({
event: true,
type: MockedCall.EVENT_TYPE,
room: this.room.roomId,
user: "@alice:example.org",
content: { ...this.event.getContent(), "m.terminated": "Call ended" },
skey: this.widget.id,
ts: Date.now(),
}),
],
{ addToState: true },
);

super.destroy();
}
Expand Down
2 changes: 1 addition & 1 deletion test/test-utils/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,6 @@ export const populateThread = async ({
// that it is already loaded, and send the events again to the room
// so they are added to the thread timeline.
ret.thread.initialEventsFetched = true;
await room.addLiveEvents(ret.events);
await room.addLiveEvents(ret.events, { addToState: false });
return ret;
};
3 changes: 1 addition & 2 deletions test/unit-tests/Notifier-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,7 @@ describe("Notifier", () => {
content: { body: "this is a thread root" },
}),
testRoom.threadsTimelineSets[0]!.getLiveTimeline(),
false,
false,
{ toStartOfTimeline: false, fromCache: false, addToState: true },
);

expect(fn).not.toHaveBeenCalled();
Expand Down
10 changes: 5 additions & 5 deletions test/unit-tests/RoomNotifs-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe("RoomNotifs test", () => {

const itShouldCountPredecessorHighlightWhenThereIsAPredecessorInTheCreateEvent = (): void => {
it("and there is a predecessor in the create event, it should count predecessor highlight", () => {
room.addLiveEvents([mkCreateEvent(OLD_ROOM_ID)]);
room.addLiveEvents([mkCreateEvent(OLD_ROOM_ID)], { addToState: true });

expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(8);
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false)).toBe(7);
Expand All @@ -157,7 +157,7 @@ describe("RoomNotifs test", () => {
const itShouldCountPredecessorHighlightWhenThereIsAPredecessorEvent = (): void => {
it("and there is a predecessor event, it should count predecessor highlight", () => {
client.getVisibleRooms();
room.addLiveEvents([mkCreateEvent(OLD_ROOM_ID)]);
room.addLiveEvents([mkCreateEvent(OLD_ROOM_ID)], { addToState: true });
upsertRoomStateEvents(room, [mkPredecessorEvent(OLD_ROOM_ID)]);

expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(8);
Expand Down Expand Up @@ -185,7 +185,7 @@ describe("RoomNotifs test", () => {
itShouldCountPredecessorHighlightWhenThereIsAPredecessorEvent();

it("and there is only a predecessor event, it should not count predecessor highlight", () => {
room.addLiveEvents([mkCreateEvent()]);
room.addLiveEvents([mkCreateEvent()], { addToState: true });
upsertRoomStateEvents(room, [mkPredecessorEvent(OLD_ROOM_ID)]);

expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(2);
Expand All @@ -204,15 +204,15 @@ describe("RoomNotifs test", () => {
itShouldCountPredecessorHighlightWhenThereIsAPredecessorEvent();

it("and there is only a predecessor event, it should count predecessor highlight", () => {
room.addLiveEvents([mkCreateEvent()]);
room.addLiveEvents([mkCreateEvent()], { addToState: true });
upsertRoomStateEvents(room, [mkPredecessorEvent(OLD_ROOM_ID)]);

expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(8);
expect(getUnreadNotificationCount(room, NotificationCountType.Highlight, false)).toBe(7);
});

it("and there is an unknown room in the predecessor event, it should not count predecessor highlight", () => {
room.addLiveEvents([mkCreateEvent()]);
room.addLiveEvents([mkCreateEvent()], { addToState: true });
upsertRoomStateEvents(room, [mkPredecessorEvent("!unknon:example.com")]);

expect(getUnreadNotificationCount(room, NotificationCountType.Total, false)).toBe(2);
Expand Down
14 changes: 7 additions & 7 deletions test/unit-tests/Unread-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe("Unread", () => {
room: roomId,
content: {},
});
room.addLiveEvents([event]);
room.addLiveEvents([event], { addToState: true });

// Don't care about the code path of hidden events.
mocked(haveRendererForEvent).mockClear().mockReturnValue(true);
Expand All @@ -157,7 +157,7 @@ describe("Unread", () => {
content: {},
});
// Only for timeline events.
room.addLiveEvents([event]);
room.addLiveEvents([event], { addToState: true });

expect(doesRoomHaveUnreadMessages(room, false)).toBe(false);
});
Expand Down Expand Up @@ -201,7 +201,7 @@ describe("Unread", () => {
content: {},
});
// Only for timeline events.
room.addLiveEvents([event2]);
room.addLiveEvents([event2], { addToState: true });

expect(doesRoomHaveUnreadMessages(room, false)).toBe(true);
});
Expand Down Expand Up @@ -403,7 +403,7 @@ describe("Unread", () => {
redactedEvent.makeRedacted(redactedEvent, room);
console.log("Event Id", redactedEvent.getId());
// Only for timeline events.
room.addLiveEvents([redactedEvent]);
room.addLiveEvents([redactedEvent], { addToState: true });

expect(doesRoomHaveUnreadMessages(room, true)).toBe(true);
expect(logger.warn).toHaveBeenCalledWith(
Expand Down Expand Up @@ -448,7 +448,7 @@ describe("Unread", () => {
room: roomId,
content: {},
});
room.addLiveEvents([event]);
room.addLiveEvents([event], { addToState: true });
});

it("an unthreaded receipt for the event makes the room read", () => {
Expand Down Expand Up @@ -502,7 +502,7 @@ describe("Unread", () => {
ts: 100,
currentUserId: myId,
});
room.addLiveEvents(events);
room.addLiveEvents(events, { addToState: true });
threadEvent = events[1];
});

Expand Down Expand Up @@ -555,7 +555,7 @@ describe("Unread", () => {
room: roomId,
content: {},
});
room.addLiveEvents([event]);
room.addLiveEvents([event], { addToState: true });

// It still returns false
expect(doesRoomHaveUnreadThreads(room)).toBe(false);
Expand Down
43 changes: 41 additions & 2 deletions test/unit-tests/components/structures/FilePanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Please see LICENSE files in the repository root for full details.
*/

import React from "react";
import { EventTimelineSet, PendingEventOrdering, Room } from "matrix-js-sdk/src/matrix";
import { EventTimelineSet, PendingEventOrdering, Room, RoomEvent } from "matrix-js-sdk/src/matrix";
import { screen, render, waitFor } from "jest-matrix-react";
import { mocked } from "jest-mock";

import FilePanel from "../../../../src/components/structures/FilePanel";
import ResizeNotifier from "../../../../src/utils/ResizeNotifier";
import { stubClient } from "../../../test-utils";
import { mkEvent, stubClient } from "../../../test-utils";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";

jest.mock("matrix-js-sdk/src/matrix", () => ({
Expand Down Expand Up @@ -47,4 +47,43 @@ describe("FilePanel", () => {
});
expect(asFragment()).toMatchSnapshot();
});

describe("addEncryptedLiveEvent", () => {
it("should add file msgtype event to filtered timelineSet", async () => {
const cli = MatrixClientPeg.safeGet();
const room = new Room("!room:server", cli, cli.getSafeUserId(), {
pendingEventOrdering: PendingEventOrdering.Detached,
});
cli.reEmitter.reEmit(room, [RoomEvent.Timeline]);
const timelineSet = new EventTimelineSet(room);
room.getOrCreateFilteredTimelineSet = jest.fn().mockReturnValue(timelineSet);
mocked(cli.getRoom).mockReturnValue(room);

let filePanel: FilePanel | null;
render(
<FilePanel
roomId={room.roomId}
onClose={jest.fn()}
resizeNotifier={new ResizeNotifier()}
ref={(ref) => (filePanel = ref)}
/>,
);
await screen.findByText("No files visible in this room");

const event = mkEvent({
type: "m.room.message",
user: cli.getSafeUserId(),
room: room.roomId,
content: {
body: "hello",
url: "mxc://matrix.org/1234",
msgtype: "m.file",
},
event: true,
});
filePanel!.addEncryptedLiveEvent(event);

expect(timelineSet.getLiveTimeline().getEvents()).toContain(event);
});
});
});
2 changes: 1 addition & 1 deletion test/unit-tests/components/structures/RoomView-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ describe("RoomView", () => {
skey: id,
ts,
});
room.addLiveEvents([widgetEvent]);
room.addLiveEvents([widgetEvent], { addToState: false });
room.currentState.setStateEvents([widgetEvent]);
cli.emit(RoomStateEvent.Events, widgetEvent, room.currentState, null);
await flushPromises();
Expand Down
12 changes: 6 additions & 6 deletions test/unit-tests/components/structures/ThreadPanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ describe("ThreadPanel", () => {
return event ? Promise.resolve(event) : Promise.reject();
});
const [allThreads, myThreads] = room.threadsTimelineSets;
allThreads!.addLiveEvent(otherThread.rootEvent);
allThreads!.addLiveEvent(mixedThread.rootEvent);
allThreads!.addLiveEvent(ownThread.rootEvent);
myThreads!.addLiveEvent(mixedThread.rootEvent);
myThreads!.addLiveEvent(ownThread.rootEvent);
allThreads!.addLiveEvent(otherThread.rootEvent, { addToState: true });
allThreads!.addLiveEvent(mixedThread.rootEvent, { addToState: true });
allThreads!.addLiveEvent(ownThread.rootEvent, { addToState: true });
myThreads!.addLiveEvent(mixedThread.rootEvent, { addToState: true });
myThreads!.addLiveEvent(ownThread.rootEvent, { addToState: true });

const renderResult = render(<TestThreadPanel />);
await waitFor(() => expect(renderResult.container.querySelector(".mx_AutoHideScrollbar")).toBeFalsy());
Expand Down Expand Up @@ -258,7 +258,7 @@ describe("ThreadPanel", () => {
return event ? Promise.resolve(event) : Promise.reject();
});
const [allThreads] = room.threadsTimelineSets;
allThreads!.addLiveEvent(otherThread.rootEvent);
allThreads!.addLiveEvent(otherThread.rootEvent, { addToState: true });

const renderResult = render(<TestThreadPanel />);
await waitFor(() => expect(renderResult.container.querySelector(".mx_AutoHideScrollbar")).toBeFalsy());
Expand Down
Loading

0 comments on commit c34cbd0

Please sign in to comment.