Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 22cb2db

Browse files
committed
Examine all m.direct rooms to find a DM
1 parent 072003d commit 22cb2db

File tree

5 files changed

+112
-39
lines changed

5 files changed

+112
-39
lines changed

src/utils/DMRoomMap.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ export default class DMRoomMap {
192192
.reduce((obj, r) => (obj[r.userId] = r.room) && obj, {});
193193
}
194194

195+
/**
196+
* @returns all room Ids from m.direct
197+
*/
198+
public getRoomIds(): Set<string> {
199+
return Object.values(this.mDirectEvent).reduce((prevRoomIds: Set<string>, roomIds: string[]): Set<string> => {
200+
roomIds.forEach((roomId) => prevRoomIds.add(roomId));
201+
return prevRoomIds;
202+
}, new Set<string>());
203+
}
204+
195205
private getUserToRooms(): { [key: string]: string[] } {
196206
if (!this.userToRooms) {
197207
const userToRooms = this.mDirectEvent;

src/utils/dm/findDMForUser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import { getFunctionalMembers } from "../room/getFunctionalMembers";
2929
* @returns {Room} Room if found
3030
*/
3131
export function findDMForUser(client: MatrixClient, userId: string): Room {
32-
const roomIds = DMRoomMap.shared().getDMRoomsForUserId(userId);
33-
const rooms = roomIds.map((id) => client.getRoom(id));
32+
const roomIds = DMRoomMap.shared().getRoomIds();
33+
const rooms = Array.from(roomIds).map((id) => client.getRoom(id));
3434
const suitableDMRooms = rooms
3535
.filter((r) => {
3636
// Validate that we are joined and the other person is also joined. We'll also make sure

test/LegacyCallHandler-test.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -225,20 +225,10 @@ describe("LegacyCallHandler", () => {
225225
return null;
226226
}
227227
},
228-
getDMRoomsForUserId: (userId: string) => {
229-
if (userId === NATIVE_ALICE) {
230-
return [NATIVE_ROOM_ALICE];
231-
} else if (userId === NATIVE_BOB) {
232-
return [NATIVE_ROOM_BOB];
233-
} else if (userId === NATIVE_CHARLIE) {
234-
return [NATIVE_ROOM_CHARLIE];
235-
} else if (userId === VIRTUAL_BOB) {
236-
return [VIRTUAL_ROOM_BOB];
237-
} else {
238-
return [];
239-
}
228+
getRoomIds: () => {
229+
return [NATIVE_ROOM_ALICE, NATIVE_ROOM_BOB, NATIVE_ROOM_CHARLIE, VIRTUAL_ROOM_BOB];
240230
},
241-
} as DMRoomMap;
231+
} as unknown as DMRoomMap;
242232
DMRoomMap.setShared(dmRoomMap);
243233

244234
pstnLookup = null;

test/utils/DMRoomMap-test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import { mocked, Mocked } from "jest-mock";
18+
import { EventType, IContent, MatrixClient } from "matrix-js-sdk/src/matrix";
19+
20+
import DMRoomMap from "../../src/utils/DMRoomMap";
21+
import { mkEvent, stubClient } from "../test-utils";
22+
23+
describe("DMRoomMap", () => {
24+
const roomId1 = "!room1:example.com";
25+
const roomId2 = "!room2:example.com";
26+
const roomId3 = "!room3:example.com";
27+
const roomId4 = "!room4:example.com";
28+
29+
const mDirectContent = {
30+
"user@example.com": [roomId1, roomId2],
31+
"@user:example.com": [roomId1, roomId3, roomId4],
32+
"@user2:example.com": [] as string[],
33+
} satisfies IContent;
34+
35+
let client: Mocked<MatrixClient>;
36+
let dmRoomMap: DMRoomMap;
37+
38+
beforeEach(() => {
39+
client = mocked(stubClient());
40+
41+
const mDirectEvent = mkEvent({
42+
event: true,
43+
type: EventType.Direct,
44+
user: client.getSafeUserId(),
45+
content: mDirectContent,
46+
});
47+
client.getAccountData.mockReturnValue(mDirectEvent);
48+
dmRoomMap = new DMRoomMap(client);
49+
});
50+
51+
it("getRoomIds should return the room Ids", () => {
52+
expect(dmRoomMap.getRoomIds()).toEqual(new Set([roomId1, roomId2, roomId3, roomId4]));
53+
});
54+
});

test/utils/dm/findDMForUser-test.ts

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ limitations under the License.
1515
*/
1616

1717
import { mocked } from "jest-mock";
18-
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
18+
import { EventType, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
1919

2020
import DMRoomMap from "../../../src/utils/DMRoomMap";
21-
import { createTestClient, makeMembershipEvent } from "../../test-utils";
21+
import { createTestClient, makeMembershipEvent, mkEvent } from "../../test-utils";
2222
import { LocalRoom } from "../../../src/models/LocalRoom";
2323
import { findDMForUser } from "../../../src/utils/dm/findDMForUser";
2424
import { getFunctionalMembers } from "../../../src/utils/room/getFunctionalMembers";
@@ -39,6 +39,19 @@ describe("findDMForUser", () => {
3939
let dmRoomMap: DMRoomMap;
4040
let mockClient: MatrixClient;
4141

42+
const setUpMDirect = (mDirect: { [key: string]: string[] }) => {
43+
const mDirectEvent = mkEvent({
44+
event: true,
45+
type: EventType.Direct,
46+
user: mockClient.getSafeUserId(),
47+
content: mDirect,
48+
});
49+
mocked(mockClient).getAccountData.mockReturnValue(mDirectEvent);
50+
51+
dmRoomMap = new DMRoomMap(mockClient);
52+
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
53+
};
54+
4255
beforeEach(() => {
4356
mockClient = createTestClient();
4457

@@ -87,42 +100,48 @@ describe("findDMForUser", () => {
87100
[room5.roomId]: room5,
88101
}[roomId];
89102
});
103+
});
90104

91-
dmRoomMap = {
92-
getDMRoomForIdentifiers: jest.fn(),
93-
getDMRoomsForUserId: jest.fn(),
94-
} as unknown as DMRoomMap;
95-
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
96-
mocked(dmRoomMap.getDMRoomsForUserId).mockReturnValue([
97-
room1.roomId,
98-
room2.roomId,
99-
room3.roomId,
100-
room4.roomId,
101-
room5.roomId,
102-
]);
105+
afterAll(() => {
106+
jest.restoreAllMocks();
103107
});
104108

105109
describe("for an empty DM room list", () => {
106110
beforeEach(() => {
107-
mocked(dmRoomMap.getDMRoomsForUserId).mockReturnValue([]);
111+
setUpMDirect({});
108112
});
109113

110114
it("should return undefined", () => {
111115
expect(findDMForUser(mockClient, userId1)).toBeUndefined();
112116
});
113117
});
114118

115-
it("should find a room ordered by last activity 1", () => {
116-
room1.getLastActiveTimestamp = () => 2;
117-
room3.getLastActiveTimestamp = () => 1;
119+
describe("when there are soom rooms", () => {
120+
beforeEach(() => {
121+
setUpMDirect({
122+
[userId1]: [room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId],
123+
});
124+
});
125+
126+
it("should find a room ordered by last activity 1", () => {
127+
room1.getLastActiveTimestamp = () => 2;
128+
room3.getLastActiveTimestamp = () => 1;
118129

119-
expect(findDMForUser(mockClient, userId1)).toBe(room1);
120-
});
130+
expect(findDMForUser(mockClient, userId1)).toBe(room1);
131+
});
121132

122-
it("should find a room ordered by last activity 2", () => {
123-
room1.getLastActiveTimestamp = () => 1;
124-
room3.getLastActiveTimestamp = () => 2;
133+
it("should find a room ordered by last activity 2", () => {
134+
room1.getLastActiveTimestamp = () => 1;
135+
room3.getLastActiveTimestamp = () => 2;
125136

126-
expect(findDMForUser(mockClient, userId1)).toBe(room3);
137+
expect(findDMForUser(mockClient, userId1)).toBe(room3);
138+
});
139+
140+
it("should find a room for a user without an m.direct entry but a DM-like room exists", () => {
141+
room1.getLastActiveTimestamp = () => 1;
142+
room3.getLastActiveTimestamp = () => 2;
143+
144+
expect(findDMForUser(mockClient, userId2)).toBe(room3);
145+
});
127146
});
128147
});

0 commit comments

Comments
 (0)