diff --git a/src/Modal.tsx b/src/Modal.tsx index d40f286da7..ba5c6702bd 100644 --- a/src/Modal.tsx +++ b/src/Modal.tsx @@ -18,6 +18,7 @@ import defaultDispatcher from "./dispatcher/dispatcher"; import AsyncWrapper from "./AsyncWrapper"; import { type Defaultize } from "./@types/common"; import { type ActionPayload } from "./dispatcher/payloads"; +import { filterBoolean } from "./utils/arrays.ts"; const DIALOG_CONTAINER_ID = "mx_Dialog_Container"; const STATIC_DIALOG_CONTAINER_ID = "mx_Dialog_StaticContainer"; @@ -160,13 +161,16 @@ export class ModalManager extends TypedEventEmitter -1) { - roomList.splice(indexOfRoom, 1); - modified = true; - } - } + // remove it from the lists of all users (it can only be a DM room for one person) + for (const thisUserId in filteredContent) { + if (!filteredContent[thisUserId]) continue; + filteredContent[thisUserId] = filteredContent[thisUserId].filter((room) => room !== roomId); } - // now add it, if it's not already there + // now add it if the caller asked for it to be a DM room if (userId) { - const roomList = dmRoomMap.get(userId) || []; - if (roomList.indexOf(roomId) == -1) { - roomList.push(roomId); - modified = true; + if (!filteredContent[userId]) { + filteredContent[userId] = []; } - dmRoomMap.set(userId, roomList); + filteredContent[userId].push(roomId); } - // prevent unnecessary calls to setAccountData - if (!modified) return; - - await client.setAccountData(EventType.Direct, Object.fromEntries(dmRoomMap)); + await client.setAccountData(EventType.Direct, filteredContent); } /** diff --git a/test/unit-tests/Rooms-test.ts b/test/unit-tests/Rooms-test.ts index a9266d7e79..841b6f3722 100644 --- a/test/unit-tests/Rooms-test.ts +++ b/test/unit-tests/Rooms-test.ts @@ -79,16 +79,6 @@ describe("setDMRoom", () => { }); }); - describe("when trying to add a DM, that already exists", () => { - beforeEach(() => { - setDMRoom(client, roomId1, userId1); - }); - - it("should not update the account data", () => { - expect(client.setAccountData).not.toHaveBeenCalled(); - }); - }); - describe("when removing an existing DM", () => { beforeEach(() => { setDMRoom(client, roomId1, null); @@ -102,16 +92,6 @@ describe("setDMRoom", () => { }); }); - describe("when removing an unknown room", () => { - beforeEach(() => { - setDMRoom(client, roomId4, null); - }); - - it("should not update the account data", () => { - expect(client.setAccountData).not.toHaveBeenCalled(); - }); - }); - describe("when the direct event is undefined", () => { beforeEach(() => { mocked(client.getAccountData).mockReturnValue(undefined);