@@ -21,17 +21,8 @@ import { isLocalRoom } from "../localRoom/isLocalRoom";
2121import { isJoinedOrNearlyJoined } from "../membership" ;
2222import { getFunctionalMembers } from "../room/getFunctionalMembers" ;
2323
24- /**
25- * Tries to find a DM room with a specific user.
26- *
27- * @param {MatrixClient } client
28- * @param {string } userId ID of the user to find the DM for
29- * @returns {Room } Room if found
30- */
31- export function findDMForUser ( client : MatrixClient , userId : string ) : Room {
32- const roomIds = DMRoomMap . shared ( ) . getDMRoomsForUserId ( userId ) ;
33- const rooms = roomIds . map ( ( id ) => client . getRoom ( id ) ) ;
34- const suitableDMRooms = rooms
24+ function extractSuitableRoom ( rooms : Room [ ] , userId : string ) : Room | undefined {
25+ const suitableRooms = rooms
3526 . filter ( ( r ) => {
3627 // Validate that we are joined and the other person is also joined. We'll also make sure
3728 // that the room also looks like a DM (until we have canonical DMs to tell us). For now,
@@ -44,7 +35,7 @@ export function findDMForUser(client: MatrixClient, userId: string): Room {
4435 const functionalUsers = getFunctionalMembers ( r ) ;
4536 const members = r . currentState . getMembers ( ) ;
4637 const joinedMembers = members . filter (
47- ( m ) => ! functionalUsers . includes ( m . userId ) && isJoinedOrNearlyJoined ( m . membership ) ,
38+ ( m ) => ! functionalUsers . includes ( m . userId ) && m . membership && isJoinedOrNearlyJoined ( m . membership ) ,
4839 ) ;
4940 const otherMember = joinedMembers . find ( ( m ) => m . userId === userId ) ;
5041 return otherMember && joinedMembers . length === 2 ;
@@ -54,7 +45,34 @@ export function findDMForUser(client: MatrixClient, userId: string): Room {
5445 . sort ( ( r1 , r2 ) => {
5546 return r2 . getLastActiveTimestamp ( ) - r1 . getLastActiveTimestamp ( ) ;
5647 } ) ;
57- if ( suitableDMRooms . length ) {
58- return suitableDMRooms [ 0 ] ;
48+
49+ if ( suitableRooms . length ) {
50+ return suitableRooms [ 0 ] ;
5951 }
52+
53+ return undefined ;
54+ }
55+
56+ /**
57+ * Tries to find a DM room with a specific user.
58+ *
59+ * @param {MatrixClient } client
60+ * @param {string } userId ID of the user to find the DM for
61+ * @returns {Room | undefined } Room if found
62+ */
63+ export function findDMForUser ( client : MatrixClient , userId : string ) : Room | undefined {
64+ const roomIdsForUserId = DMRoomMap . shared ( ) . getDMRoomsForUserId ( userId ) ;
65+ const roomsForUserId = roomIdsForUserId . map ( ( id ) => client . getRoom ( id ) ) . filter ( ( r ) : r is Room => r !== null ) ;
66+ const suitableRoomForUserId = extractSuitableRoom ( roomsForUserId , userId ) ;
67+
68+ if ( suitableRoomForUserId ) {
69+ return suitableRoomForUserId ;
70+ }
71+
72+ // Try to find in all rooms as a fallback
73+ const allRoomIds = DMRoomMap . shared ( ) . getRoomIds ( ) ;
74+ const allRooms = Array . from ( allRoomIds )
75+ . map ( ( id ) => client . getRoom ( id ) )
76+ . filter ( ( r ) : r is Room => r !== null ) ;
77+ return extractSuitableRoom ( allRooms , userId ) ;
6078}
0 commit comments