Skip to content

Commit d70d448

Browse files
authored
Fix converttoroom & converttodm not working (#29705)
* Fix converttoroom & converttodm not working setAccountData uses `deepCompare` within to avoid writing no-op updates Signed-off-by: Michael Telatynski <[email protected]> * Update tests Signed-off-by: Michael Telatynski <[email protected]> * Use filterValidMDirect utility in setDMRoom Ensure we do not mutate the account data as this would then upset `setAccountData`'s deepCompare later Signed-off-by: Michael Telatynski <[email protected]> * Iterate Signed-off-by: Michael Telatynski <[email protected]> --------- Signed-off-by: Michael Telatynski <[email protected]>
1 parent 60117b9 commit d70d448

File tree

2 files changed

+12
-47
lines changed

2 files changed

+12
-47
lines changed

src/Rooms.ts

+12-27
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Please see LICENSE files in the repository root for full details.
99
import { type Room, EventType, type RoomMember, type MatrixClient } from "matrix-js-sdk/src/matrix";
1010

1111
import AliasCustomisations from "./customisations/Alias";
12+
import { filterValidMDirect } from "./utils/dm/filterValidMDirect.ts";
1213

1314
/**
1415
* Given a room object, return the alias we should use for it,
@@ -56,39 +57,23 @@ export async function setDMRoom(client: MatrixClient, roomId: string, userId: st
5657
if (client.isGuest()) return;
5758

5859
const mDirectEvent = client.getAccountData(EventType.Direct);
59-
const currentContent = mDirectEvent?.getContent() || {};
60-
61-
const dmRoomMap = new Map(Object.entries(currentContent));
62-
let modified = false;
63-
64-
// remove it from the lists of any others users
65-
// (it can only be a DM room for one person)
66-
for (const thisUserId of dmRoomMap.keys()) {
67-
const roomList = dmRoomMap.get(thisUserId) || [];
68-
69-
if (thisUserId != userId) {
70-
const indexOfRoom = roomList.indexOf(roomId);
71-
if (indexOfRoom > -1) {
72-
roomList.splice(indexOfRoom, 1);
73-
modified = true;
74-
}
75-
}
60+
const { filteredContent } = filterValidMDirect(mDirectEvent?.getContent() ?? {});
61+
62+
// remove it from the lists of all users (it can only be a DM room for one person)
63+
for (const thisUserId in filteredContent) {
64+
if (!filteredContent[thisUserId]) continue;
65+
filteredContent[thisUserId] = filteredContent[thisUserId].filter((room) => room !== roomId);
7666
}
7767

78-
// now add it, if it's not already there
68+
// now add it if the caller asked for it to be a DM room
7969
if (userId) {
80-
const roomList = dmRoomMap.get(userId) || [];
81-
if (roomList.indexOf(roomId) == -1) {
82-
roomList.push(roomId);
83-
modified = true;
70+
if (!filteredContent[userId]) {
71+
filteredContent[userId] = [];
8472
}
85-
dmRoomMap.set(userId, roomList);
73+
filteredContent[userId].push(roomId);
8674
}
8775

88-
// prevent unnecessary calls to setAccountData
89-
if (!modified) return;
90-
91-
await client.setAccountData(EventType.Direct, Object.fromEntries(dmRoomMap));
76+
await client.setAccountData(EventType.Direct, filteredContent);
9277
}
9378

9479
/**

test/unit-tests/Rooms-test.ts

-20
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@ describe("setDMRoom", () => {
7979
});
8080
});
8181

82-
describe("when trying to add a DM, that already exists", () => {
83-
beforeEach(() => {
84-
setDMRoom(client, roomId1, userId1);
85-
});
86-
87-
it("should not update the account data", () => {
88-
expect(client.setAccountData).not.toHaveBeenCalled();
89-
});
90-
});
91-
9282
describe("when removing an existing DM", () => {
9383
beforeEach(() => {
9484
setDMRoom(client, roomId1, null);
@@ -102,16 +92,6 @@ describe("setDMRoom", () => {
10292
});
10393
});
10494

105-
describe("when removing an unknown room", () => {
106-
beforeEach(() => {
107-
setDMRoom(client, roomId4, null);
108-
});
109-
110-
it("should not update the account data", () => {
111-
expect(client.setAccountData).not.toHaveBeenCalled();
112-
});
113-
});
114-
11595
describe("when the direct event is undefined", () => {
11696
beforeEach(() => {
11797
mocked(client.getAccountData).mockReturnValue(undefined);

0 commit comments

Comments
 (0)