Skip to content

Commit

Permalink
Update calls to addEventToTimeline and addLiveEvents for new signature
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
  • Loading branch information
t3chguy committed Nov 6, 2024
1 parent a355292 commit 044eaf7
Show file tree
Hide file tree
Showing 34 changed files with 166 additions and 131 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 @@ -820,7 +820,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
24 changes: 14 additions & 10 deletions test/unit-tests/components/structures/RoomView-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,19 @@ describe("RoomView", () => {
cli.isRoomEncrypted.mockReturnValue(true);

// and fake an encryption event into the room to prompt it to re-check
room.addLiveEvents([
new MatrixEvent({
type: "m.room.encryption",
sender: cli.getUserId()!,
content: {},
event_id: "someid",
room_id: room.roomId,
}),
]);
room.addLiveEvents(
[
new MatrixEvent({
type: "m.room.encryption",
sender: cli.getUserId()!,
content: {},
event_id: "someid",
room_id: room.roomId,
state_key: "",
}),
],
{ addToState: true },
);

// URL previews should now be disabled
expect(roomViewInstance.state.showUrlPreview).toBe(false);
Expand Down Expand Up @@ -440,7 +444,7 @@ describe("RoomView", () => {
skey: id,
ts,
});
room.addLiveEvents([widgetEvent]);
room.addLiveEvents([widgetEvent], { addToState: true });
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 });

let events: EventData[] = [];
const renderResult = render(<TestThreadPanel />);
Expand Down Expand Up @@ -259,7 +259,7 @@ describe("ThreadPanel", () => {
return event ? Promise.resolve(event) : Promise.reject();
});
const [allThreads] = room.threadsTimelineSets;
allThreads!.addLiveEvent(otherThread.rootEvent);
allThreads!.addLiveEvent(otherThread.rootEvent, { addToState: true });

let events: EventData[] = [];
const renderResult = render(<TestThreadPanel />);
Expand Down
35 changes: 23 additions & 12 deletions test/unit-tests/components/structures/TimelinePanel-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const mkTimeline = (room: Room, events: MatrixEvent[]): [EventTimeline, EventTim
getPendingEvents: () => [] as MatrixEvent[],
} as unknown as EventTimelineSet;
const timeline = new EventTimeline(timelineSet);
events.forEach((event) => timeline.addEvent(event, { toStartOfTimeline: false }));
events.forEach((event) => timeline.addEvent(event, { toStartOfTimeline: false, addToState: true }));

return [timeline, timelineSet];
};
Expand Down Expand Up @@ -150,9 +150,11 @@ const setupPagination = (
mocked(client).paginateEventTimeline.mockImplementation(async (tl, { backwards }) => {
if (tl === timeline) {
if (backwards) {
forEachRight(previousPage ?? [], (event) => tl.addEvent(event, { toStartOfTimeline: true }));
forEachRight(previousPage ?? [], (event) =>
tl.addEvent(event, { toStartOfTimeline: true, addToState: true }),
);
} else {
(nextPage ?? []).forEach((event) => tl.addEvent(event, { toStartOfTimeline: false }));
(nextPage ?? []).forEach((event) => tl.addEvent(event, { toStartOfTimeline: false, addToState: true }));
}
// Prevent any further pagination attempts in this direction
tl.setPaginationToken(null, backwards ? EventTimeline.BACKWARDS : EventTimeline.FORWARDS);
Expand Down Expand Up @@ -256,7 +258,7 @@ describe("TimelinePanel", () => {
describe("and reading the timeline", () => {
beforeEach(async () => {
await renderTimelinePanel();
timelineSet.addLiveEvent(ev1, {});
timelineSet.addLiveEvent(ev1, { addToState: true });
await flushPromises();

// @ts-ignore
Expand Down Expand Up @@ -285,11 +287,11 @@ describe("TimelinePanel", () => {
});

it("and forgetting the read markers, should send the stored marker again", async () => {
timelineSet.addLiveEvent(ev2, {});
timelineSet.addLiveEvent(ev2, { addToState: true });
// Add the event to the room as well as the timeline, so we can find it when we
// call findEventById in getEventReadUpTo. This is odd because in our test
// setup, timelineSet is not actually the timelineSet of the room.
await room.addLiveEvents([ev2], {});
await room.addLiveEvents([ev2], { addToState: true });
room.addEphemeralEvents([newReceipt(ev2.getId()!, userId, 222, 200)]);
await timelinePanel.forgetReadMarker();
expect(client.setRoomReadMarkers).toHaveBeenCalledWith(roomId, ev2.getId());
Expand All @@ -315,7 +317,7 @@ describe("TimelinePanel", () => {

it("should send a fully read marker and a private receipt", async () => {
await renderTimelinePanel();
timelineSet.addLiveEvent(ev1, {});
timelineSet.addLiveEvent(ev1, { addToState: true });
await flushPromises();

// @ts-ignore
Expand Down Expand Up @@ -361,7 +363,7 @@ describe("TimelinePanel", () => {

it("should send receipts but no fully_read when reading the thread timeline", async () => {
await renderTimelinePanel();
timelineSet.addLiveEvent(threadEv1, {});
timelineSet.addLiveEvent(threadEv1, { addToState: true });
await flushPromises();

// @ts-ignore
Expand Down Expand Up @@ -871,7 +873,9 @@ describe("TimelinePanel", () => {
// @ts-ignore
thread.fetchEditsWhereNeeded = () => Promise.resolve();
await thread.addEvent(reply1, false, true);
await allThreads.getLiveTimeline().addEvent(thread.rootEvent!, { toStartOfTimeline: true });
await allThreads
.getLiveTimeline()
.addEvent(thread.rootEvent!, { toStartOfTimeline: true, addToState: true });
const replyToEvent = jest.spyOn(thread, "replyToEvent", "get");

const dom = render(
Expand Down Expand Up @@ -907,7 +911,9 @@ describe("TimelinePanel", () => {
// @ts-ignore
realThread.fetchEditsWhereNeeded = () => Promise.resolve();
await realThread.addEvent(reply1, true);
await allThreads.getLiveTimeline().addEvent(realThread.rootEvent!, { toStartOfTimeline: true });
await allThreads
.getLiveTimeline()
.addEvent(realThread.rootEvent!, { toStartOfTimeline: true, addToState: true });
const replyToEvent = jest.spyOn(realThread, "replyToEvent", "get");

// @ts-ignore
Expand Down Expand Up @@ -968,7 +974,9 @@ describe("TimelinePanel", () => {

events.push(rootEvent);

events.forEach((event) => timelineSet.getLiveTimeline().addEvent(event, { toStartOfTimeline: true }));
events.forEach((event) =>
timelineSet.getLiveTimeline().addEvent(event, { toStartOfTimeline: true, addToState: true }),
);

const roomMembership = mkMembership({
mship: KnownMembership.Join,
Expand All @@ -988,7 +996,10 @@ describe("TimelinePanel", () => {
jest.spyOn(roomState, "getMember").mockReturnValue(member);

jest.spyOn(timelineSet.getLiveTimeline(), "getState").mockReturnValue(roomState);
timelineSet.addEventToTimeline(roomMembership, timelineSet.getLiveTimeline(), { toStartOfTimeline: false });
timelineSet.addEventToTimeline(roomMembership, timelineSet.getLiveTimeline(), {
toStartOfTimeline: false,
addToState: true,
});

for (const event of events) {
jest.spyOn(event, "isDecryptionFailure").mockReturnValue(true);
Expand Down
Loading

0 comments on commit 044eaf7

Please sign in to comment.