From 78ec757f1103b2679e2dd5c06de6cee1e63644ab Mon Sep 17 00:00:00 2001 From: R Midhun Suresh Date: Fri, 16 May 2025 19:22:39 +0530 Subject: [PATCH] RoomListStoreV3: Only add new rooms that pass `VisibilityProvider` check (#29974) * Add new rooms only after checking VisibilityProvider Otherwise we might end up adding space rooms and other rooms that must be hidden. * Write test --- src/stores/room-list-v3/RoomListStoreV3.ts | 13 ++++++++-- .../room-list-v3/RoomListStoreV3-test.ts | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/stores/room-list-v3/RoomListStoreV3.ts b/src/stores/room-list-v3/RoomListStoreV3.ts index e613f15959..92de63285e 100644 --- a/src/stores/room-list-v3/RoomListStoreV3.ts +++ b/src/stores/room-list-v3/RoomListStoreV3.ts @@ -321,8 +321,17 @@ export class RoomListStoreV3Class extends AsyncStoreWithClient { */ private addRoomAndEmit(room: Room, isNewRoom = false): void { if (!this.roomSkipList) throw new Error("roomSkipList hasn't been created yet!"); - if (isNewRoom) this.roomSkipList.addNewRoom(room); - else this.roomSkipList.reInsertRoom(room); + if (isNewRoom) { + if (!VisibilityProvider.instance.isRoomVisible(room)) { + logger.info( + `RoomListStoreV3: Refusing to add new room ${room.roomId} because isRoomVisible returned false.`, + ); + return; + } + this.roomSkipList.addNewRoom(room); + } else { + this.roomSkipList.reInsertRoom(room); + } this.emit(LISTS_UPDATE_EVENT); } diff --git a/test/unit-tests/stores/room-list-v3/RoomListStoreV3-test.ts b/test/unit-tests/stores/room-list-v3/RoomListStoreV3-test.ts index 0f6c4203e6..0c0cdfc091 100644 --- a/test/unit-tests/stores/room-list-v3/RoomListStoreV3-test.ts +++ b/test/unit-tests/stores/room-list-v3/RoomListStoreV3-test.ts @@ -416,6 +416,32 @@ describe("RoomListStoreV3", () => { } describe("Spaces", () => { + it("Newly created space is not added by the store", async () => { + const { client, rooms } = getClientAndRooms(); + const infoSpy = jest.spyOn(logger, "info"); + + const store = new RoomListStoreV3Class(dispatcher); + await store.start(); + + // Create a space and let the store know about it + const { spaceRoom } = createSpace(rooms, [6, 8, 13, 27, 75], client); + dispatcher.dispatch( + { + action: "MatrixActions.Room.myMembership", + oldMembership: KnownMembership.Leave, + membership: KnownMembership.Invite, + room: spaceRoom, + }, + true, + ); + + // Space room should not be added + expect(store.getSortedRooms()).not.toContain(spaceRoom); + expect(infoSpy).toHaveBeenCalledWith( + expect.stringContaining("RoomListStoreV3: Refusing to add new room"), + ); + }); + it("Filtering by spaces work", async () => { const { client, rooms } = getClientAndRooms(); // Let's choose 5 rooms to put in space