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

Commit a916198

Browse files
weeman1337justjanne
authored andcommitted
Fix start DM with pending third party invite (#10347)
* Fix start DM with pending third party invite * Make the fix more clearly visible --------- Co-authored-by: Janne Mareike Koschinski <[email protected]> Co-authored-by: Janne Mareike Koschinski <[email protected]> (cherry picked from commit d53e918)
1 parent ad28d12 commit a916198

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/utils/dm/findDMForUser.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,20 @@ import { isLocalRoom } from "../localRoom/isLocalRoom";
2121
import { isJoinedOrNearlyJoined } from "../membership";
2222
import { getFunctionalMembers } from "../room/getFunctionalMembers";
2323

24-
function extractSuitableRoom(rooms: Room[], userId: string): Room | undefined {
24+
/**
25+
* Iterates the rooms and tries to find a DM room with the user identified by UserId.
26+
* A DM room is assumed if one of the following matches:
27+
* - Has two members and contains a membership for the user identified by userId
28+
* - findRoomWithThirdpartyInvites is true and has one member and a third pending third party invite
29+
*
30+
* If multiple rooms match it will return the one with the most recent event.
31+
*
32+
* @param rooms - Rooms to iterate
33+
* @param userId - User Id of the other user
34+
* @param [findRoomWithThirdpartyInvites] - Whether to find a DM for a pending thirdparty invite
35+
* @returns DM room if found or undefined if not
36+
*/
37+
function extractSuitableRoom(rooms: Room[], userId: string, findRoomWithThirdpartyInvites: boolean): Room | undefined {
2538
const suitableRooms = rooms
2639
.filter((r) => {
2740
// Validate that we are joined and the other person is also joined. We'll also make sure
@@ -46,7 +59,7 @@ function extractSuitableRoom(rooms: Room[], userId: string): Room | undefined {
4659
const thirdPartyInvites = r.currentState.getStateEvents("m.room.third_party_invite") || [];
4760

4861
// match room with pending third-party invite
49-
return joinedMembers.length === 1 && thirdPartyInvites.length === 1;
62+
return findRoomWithThirdpartyInvites && joinedMembers.length === 1 && thirdPartyInvites.length === 1;
5063
}
5164
return false;
5265
})
@@ -71,7 +84,10 @@ function extractSuitableRoom(rooms: Room[], userId: string): Room | undefined {
7184
export function findDMForUser(client: MatrixClient, userId: string): Room | undefined {
7285
const roomIdsForUserId = DMRoomMap.shared().getDMRoomsForUserId(userId);
7386
const roomsForUserId = roomIdsForUserId.map((id) => client.getRoom(id)).filter((r): r is Room => r !== null);
74-
const suitableRoomForUserId = extractSuitableRoom(roomsForUserId, userId);
87+
// Call with findRoomWithThirdpartyInvites = true to also include rooms with pending thirdparty invites.
88+
// roomsForUserId can only contain rooms with the other user here,
89+
// because they have been queried by getDMRoomsForUserId().
90+
const suitableRoomForUserId = extractSuitableRoom(roomsForUserId, userId, true);
7591

7692
if (suitableRoomForUserId) {
7793
return suitableRoomForUserId;
@@ -82,5 +98,5 @@ export function findDMForUser(client: MatrixClient, userId: string): Room | unde
8298
const allRooms = Array.from(allRoomIds)
8399
.map((id) => client.getRoom(id))
84100
.filter((r): r is Room => r !== null);
85-
return extractSuitableRoom(allRooms, userId);
101+
return extractSuitableRoom(allRooms, userId, false);
86102
}

test/utils/dm/findDMForUser-test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ describe("findDMForUser", () => {
135135
return [room1.roomId, room2.roomId, room3.roomId, room4.roomId, room5.roomId, unknownRoomId];
136136
}
137137

138+
if (userId === thirdPartyId) {
139+
return [room7.roomId];
140+
}
141+
138142
return [];
139143
});
140144
});
@@ -174,4 +178,8 @@ describe("findDMForUser", () => {
174178
it("should find a room with a pending third-party invite", () => {
175179
expect(findDMForUser(mockClient, thirdPartyId)).toBe(room7);
176180
});
181+
182+
it("should not find a room for an unknown Id", () => {
183+
expect(findDMForUser(mockClient, "@unknown:example.com")).toBe(undefined);
184+
});
177185
});

0 commit comments

Comments
 (0)