@@ -21,7 +21,20 @@ import { isLocalRoom } from "../localRoom/isLocalRoom";
2121import { isJoinedOrNearlyJoined } from "../membership" ;
2222import { 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 {
7184export 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}
0 commit comments