@@ -15,20 +15,21 @@ See the License for the specific language governing permissions and
1515limitations under the License.
1616*/
1717
18- import { MatrixClient } from "matrix-js-sdk/src/client" ;
19- import { Room } from "matrix-js-sdk/src/models/room" ;
18+ import { MatrixClient } from "matrix-js-sdk/src/client" ;
19+ import { Room } from "matrix-js-sdk/src/models/room" ;
2020
21- import { MatrixClientPeg } from './MatrixClientPeg' ;
21+ import { MatrixClientPeg } from './MatrixClientPeg' ;
2222import Modal from './Modal' ;
2323import * as sdk from './index' ;
2424import { _t } from './languageHandler' ;
2525import dis from "./dispatcher/dispatcher" ;
2626import * as Rooms from "./Rooms" ;
2727import DMRoomMap from "./utils/DMRoomMap" ;
28- import { getAddressType } from "./UserAddress" ;
28+ import { getAddressType } from "./UserAddress" ;
2929import { getE2EEWellKnown } from "./utils/WellKnownUtils" ;
3030import GroupStore from "./stores/GroupStore" ;
3131import CountlyAnalytics from "./CountlyAnalytics" ;
32+ import { isJoinedOrNearlyJoined } from "./utils/membership" ;
3233
3334// we define a number of interfaces which take their names from the js-sdk
3435/* eslint-disable camelcase */
@@ -236,9 +237,16 @@ export function findDMForUser(client: MatrixClient, userId: string): Room {
236237 const roomIds = DMRoomMap . shared ( ) . getDMRoomsForUserId ( userId ) ;
237238 const rooms = roomIds . map ( id => client . getRoom ( id ) ) ;
238239 const suitableDMRooms = rooms . filter ( r => {
240+ // Validate that we are joined and the other person is also joined. We'll also make sure
241+ // that the room also looks like a DM (until we have canonical DMs to tell us). For now,
242+ // a DM is a room of two people that contains those two people exactly. This does mean
243+ // that bots, assistants, etc will ruin a room's DM-ness, though this is a problem for
244+ // canonical DMs to solve.
239245 if ( r && r . getMyMembership ( ) === "join" ) {
240- const member = r . getMember ( userId ) ;
241- return member && ( member . membership === "invite" || member . membership === "join" ) ;
246+ const members = r . currentState . getMembers ( ) ;
247+ const joinedMembers = members . filter ( m => isJoinedOrNearlyJoined ( m . membership ) ) ;
248+ const otherMember = joinedMembers . find ( m => m . userId === userId ) ;
249+ return otherMember && joinedMembers . length === 2 ;
242250 }
243251 return false ;
244252 } ) . sort ( ( r1 , r2 ) => {
0 commit comments