Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Return only the first 100 pinned messages
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed Sep 12, 2024
1 parent eae9d9e commit 52846d1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/hooks/usePinnedEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ import PinningUtils from "../utils/PinningUtils";

/**
* Get the pinned event IDs from a room.
* The number of pinned events is limited to 100.
* @param room
*/
function getPinnedEventIds(room?: Room): string[] {
return (
const eventIds: string[] =
room
?.getLiveTimeline()
.getState(EventTimeline.FORWARDS)
?.getStateEvents(EventType.RoomPinnedEvents, "")
?.getContent()?.pinned ?? []
);
?.getContent()?.pinned ?? [];
// Limit the number of pinned events to 100
return eventIds.slice(0, 100);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/components/views/right_panel/PinnedMessagesCard-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ describe("<PinnedMessagesCard />", () => {
expect(asFragment()).toMatchSnapshot();
});

it("should not show more than 100 messages", async () => {
const events = Array.from({ length: 120 }, (_, i) =>
mkMessage({
event: true,
room: "!room:example.org",
user: "@alice:example.org",
msg: `The message ${i}`,
ts: i,
}),
);
await initPinnedMessagesCard(events, []);

expect(screen.queryAllByRole("listitem")).toHaveLength(100);
});

it("should updates when messages are pinned", async () => {
// Start with nothing pinned
const { addLocalPinEvent, addNonLocalPinEvent } = await initPinnedMessagesCard([], []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ exports[`<PinnedMessagesCard /> unpin all should not allow to unpinall 1`] = `
aria-label="Open menu"
class="_icon-button_bh2qc_17"
data-state="closed"
id="radix-18"
id="radix-218"
role="button"
style="--cpd-icon-button-size: 24px;"
tabindex="0"
Expand Down Expand Up @@ -424,7 +424,7 @@ exports[`<PinnedMessagesCard /> unpin all should not allow to unpinall 1`] = `
aria-label="Open menu"
class="_icon-button_bh2qc_17"
data-state="closed"
id="radix-19"
id="radix-219"
role="button"
style="--cpd-icon-button-size: 24px;"
tabindex="0"
Expand Down

0 comments on commit 52846d1

Please sign in to comment.