From 7a5ea866d2c8a7b18b22c955279a8e941068ad36 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 28 Jul 2023 10:15:19 +0100 Subject: [PATCH 1/2] Fix clicking on home all rooms space notification not working --- src/stores/spaces/SpaceStore.ts | 42 +++++++++++++++------------------ 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/stores/spaces/SpaceStore.ts b/src/stores/spaces/SpaceStore.ts index e91b3c5a45c..5e79b5aef08 100644 --- a/src/stores/spaces/SpaceStore.ts +++ b/src/stores/spaces/SpaceStore.ts @@ -193,35 +193,31 @@ export class SpaceStoreClass extends AsyncStoreWithClient { if (!isMetaSpace(space) && !this.matrixClient?.getRoom(space)?.isSpaceRoom()) return; if (space !== this.activeSpace) this.setActiveSpace(space, false); - if (space) { - const roomId = this.getNotificationState(space).getFirstRoomWithNotifications(); + let roomId: string | undefined; + if (space === MetaSpace.Home && this.allRoomsInHome) { + const hasMentions = RoomNotificationStateStore.instance.globalState.hasMentions; + const lists = RoomListStore.instance.orderedLists; + tagLoop: for (let i = 0; i < TAG_ORDER.length; i++) { + const t = TAG_ORDER[i]; + for (const room of lists[t]) { + const state = RoomNotificationStateStore.instance.getRoomState(room); + if (hasMentions ? state.hasMentions : state.isUnread) { + roomId = room.roomId; + break tagLoop; + } + } + } + } else { + roomId = this.getNotificationState(space).getFirstRoomWithNotifications(); + } + + if (!!roomId) { defaultDispatcher.dispatch({ action: Action.ViewRoom, room_id: roomId, context_switch: true, metricsTrigger: "WebSpacePanelNotificationBadge", }); - } else { - const lists = RoomListStore.instance.orderedLists; - for (let i = 0; i < TAG_ORDER.length; i++) { - const t = TAG_ORDER[i]; - const listRooms = lists[t]; - const unreadRoom = listRooms.find((r: Room) => { - if (this.showInHomeSpace(r)) { - const state = RoomNotificationStateStore.instance.getRoomState(r); - return state.isUnread; - } - }); - if (unreadRoom) { - defaultDispatcher.dispatch({ - action: Action.ViewRoom, - room_id: unreadRoom.roomId, - context_switch: true, - metricsTrigger: "WebSpacePanelNotificationBadge", - }); - break; - } - } } } From 4456bdcc9bc94c2bd62dba041f715d730680cd55 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Fri, 28 Jul 2023 10:29:01 +0100 Subject: [PATCH 2/2] Add test --- src/stores/spaces/SpaceStore.ts | 1 + test/stores/SpaceStore-test.ts | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/stores/spaces/SpaceStore.ts b/src/stores/spaces/SpaceStore.ts index 5e79b5aef08..69ded900483 100644 --- a/src/stores/spaces/SpaceStore.ts +++ b/src/stores/spaces/SpaceStore.ts @@ -199,6 +199,7 @@ export class SpaceStoreClass extends AsyncStoreWithClient { const lists = RoomListStore.instance.orderedLists; tagLoop: for (let i = 0; i < TAG_ORDER.length; i++) { const t = TAG_ORDER[i]; + if (!lists[t]) continue; for (const room of lists[t]) { const state = RoomNotificationStateStore.instance.getRoomState(room); if (hasMentions ? state.hasMentions : state.isUnread) { diff --git a/test/stores/SpaceStore-test.ts b/test/stores/SpaceStore-test.ts index 2989ade34ea..c38100cf548 100644 --- a/test/stores/SpaceStore-test.ts +++ b/test/stores/SpaceStore-test.ts @@ -20,7 +20,7 @@ import { EventType } from "matrix-js-sdk/src/@types/event"; import { RoomMember } from "matrix-js-sdk/src/models/room-member"; import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state"; import { defer } from "matrix-js-sdk/src/utils"; -import { ClientEvent, RoomEvent, MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; +import { ClientEvent, MatrixEvent, Room, RoomEvent } from "matrix-js-sdk/src/matrix"; import SpaceStore from "../../src/stores/spaces/SpaceStore"; import { @@ -38,6 +38,10 @@ import SettingsStore from "../../src/settings/SettingsStore"; import { SettingLevel } from "../../src/settings/SettingLevel"; import { Action } from "../../src/dispatcher/actions"; import { MatrixClientPeg } from "../../src/MatrixClientPeg"; +import RoomListStore from "../../src/stores/room-list/RoomListStore"; +import { DefaultTagID } from "../../src/stores/room-list/models"; +import { RoomNotificationStateStore } from "../../src/stores/notifications/RoomNotificationStateStore"; +import { NotificationColor } from "../../src/stores/notifications/NotificationColor"; jest.useFakeTimers(); @@ -1452,4 +1456,29 @@ describe("SpaceStore", () => { expect(client.getVisibleRooms).not.toHaveBeenCalledWith(); }); }); + + describe("setActiveRoomInSpace", () => { + it("should work with Home as all rooms space", async () => { + const room = mkRoom(room1); + const state = RoomNotificationStateStore.instance.getRoomState(room); + // @ts-ignore + state._color = NotificationColor.Grey; + jest.spyOn(RoomListStore.instance, "orderedLists", "get").mockReturnValue({ + [DefaultTagID.Untagged]: [room], + }); + + // init the store + await run(); + await setShowAllRooms(true); + + store.setActiveRoomInSpace(MetaSpace.Home); + + expect(spyDispatcher).toHaveBeenCalledWith( + expect.objectContaining({ + action: "view_room", + room_id: room.roomId, + }), + ); + }); + }); });