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

Commit

Permalink
Fix strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 committed Feb 10, 2023
1 parent 6171f1a commit 163a46e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
8 changes: 5 additions & 3 deletions src/utils/dm/findDMForUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function extractSuitableRoom(rooms: Room[], userId: string): Room | undefined {
const functionalUsers = getFunctionalMembers(r);
const members = r.currentState.getMembers();
const joinedMembers = members.filter(
(m) => !functionalUsers.includes(m.userId) && isJoinedOrNearlyJoined(m.membership),
(m) => !functionalUsers.includes(m.userId) && m.membership && isJoinedOrNearlyJoined(m.membership),
);
const otherMember = joinedMembers.find((m) => m.userId === userId);
return otherMember && joinedMembers.length === 2;
Expand All @@ -62,7 +62,7 @@ function extractSuitableRoom(rooms: Room[], userId: string): Room | undefined {
*/
export function findDMForUser(client: MatrixClient, userId: string): Room | undefined {
const roomIdsForUserId = DMRoomMap.shared().getDMRoomsForUserId(userId);
const roomsForUserId = roomIdsForUserId.map((id) => client.getRoom(id));
const roomsForUserId = roomIdsForUserId.map((id) => client.getRoom(id)).filter((r): r is Room => r !== null);
const suitableRoomForUserId = extractSuitableRoom(roomsForUserId, userId);

if (suitableRoomForUserId) {
Expand All @@ -71,6 +71,8 @@ export function findDMForUser(client: MatrixClient, userId: string): Room | unde

// Try to find in all rooms as a fallback
const allRoomIds = DMRoomMap.shared().getRoomIds();
const allRooms = Array.from(allRoomIds).map((id) => client.getRoom(id));
const allRooms = Array.from(allRoomIds)
.map((id) => client.getRoom(id))
.filter((r): r is Room => r !== null);
return extractSuitableRoom(allRooms, userId);
}
37 changes: 23 additions & 14 deletions test/utils/dm/findDMForUser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("findDMForUser", () => {
let room4: Room;
let room5: Room;
let room6: Room;
const room7Id = "!room7:example.com";
let dmRoomMap: DMRoomMap;
let mockClient: MatrixClient;

Expand Down Expand Up @@ -89,29 +90,37 @@ describe("findDMForUser", () => {
]);

mocked(mockClient.getRoom).mockImplementation((roomId: string) => {
return {
[room1.roomId]: room1,
[room2.roomId]: room2,
[room3.roomId]: room3,
[room4.roomId]: room4,
[room5.roomId]: room5,
[room6.roomId]: room6,
}[roomId];
return (
{
[room1.roomId]: room1,
[room2.roomId]: room2,
[room3.roomId]: room3,
[room4.roomId]: room4,
[room5.roomId]: room5,
[room6.roomId]: room6,
}[roomId] || null
);
});

dmRoomMap = {
getDMRoomForIdentifiers: jest.fn(),
getDMRoomsForUserId: jest.fn(),
getRoomIds: jest
.fn()
.mockReturnValue(
new Set([room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId, room6.roomId]),
),
getRoomIds: jest.fn().mockReturnValue(
new Set([
room1.roomId,
room2.roomId,
room3.roomId,
room4.roomId,
room5.roomId,
room6.roomId,
room7Id, // this room does not exist in client
]),
),
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
mocked(dmRoomMap.getDMRoomsForUserId).mockImplementation((userId: string) => {
if (userId === userId1) {
return [room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId];
return [room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId, room7Id];
}

return [];
Expand Down

0 comments on commit 163a46e

Please sign in to comment.