@@ -21,7 +21,20 @@ import { isLocalRoom } from "../localRoom/isLocalRoom";
21
21
import { isJoinedOrNearlyJoined } from "../membership" ;
22
22
import { getFunctionalMembers } from "../room/getFunctionalMembers" ;
23
23
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 {
25
38
const suitableRooms = rooms
26
39
. filter ( ( r ) => {
27
40
// 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 {
46
59
const thirdPartyInvites = r . currentState . getStateEvents ( "m.room.third_party_invite" ) || [ ] ;
47
60
48
61
// match room with pending third-party invite
49
- return joinedMembers . length === 1 && thirdPartyInvites . length === 1 ;
62
+ return findRoomWithThirdpartyInvites && joinedMembers . length === 1 && thirdPartyInvites . length === 1 ;
50
63
}
51
64
return false ;
52
65
} )
@@ -71,7 +84,10 @@ function extractSuitableRoom(rooms: Room[], userId: string): Room | undefined {
71
84
export function findDMForUser ( client : MatrixClient , userId : string ) : Room | undefined {
72
85
const roomIdsForUserId = DMRoomMap . shared ( ) . getDMRoomsForUserId ( userId ) ;
73
86
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 ) ;
75
91
76
92
if ( suitableRoomForUserId ) {
77
93
return suitableRoomForUserId ;
@@ -82,5 +98,5 @@ export function findDMForUser(client: MatrixClient, userId: string): Room | unde
82
98
const allRooms = Array . from ( allRoomIds )
83
99
. map ( ( id ) => client . getRoom ( id ) )
84
100
. filter ( ( r ) : r is Room => r !== null ) ;
85
- return extractSuitableRoom ( allRooms , userId ) ;
101
+ return extractSuitableRoom ( allRooms , userId , false ) ;
86
102
}
0 commit comments