Update the algorithm to find an existing DM: simplify and make sure there is only 2 people in the room

This commit is contained in:
Benoit Marty 2020-10-29 13:25:21 +01:00
parent a393c4dfae
commit 1bc726abff
2 changed files with 10 additions and 4 deletions

View file

@ -131,6 +131,14 @@ interface RoomService {
/**
* Return the roomId of an existing DM with the other user, or null if such room does not exist
* A room is a DM if:
* - it is listed in the `m.direct` account data
* - the current user has joined the room
* - the other user is invited or has joined the room
* - it has exactly 2 members
* Note:
* - the returning room can be encrypted or not
* - the power level of the users are not taken into account. Normally in a DM, the 2 members are admins of the room
*/
fun getExistingDirectRoomWithUser(otherUserId: String): String?
}

View file

@ -25,7 +25,6 @@ import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity
import org.matrix.android.sdk.internal.database.model.RoomSummaryEntityFields
import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.android.sdk.internal.session.room.membership.RoomMemberHelper
import javax.inject.Inject
internal interface RoomGetter {
@ -52,9 +51,8 @@ internal class DefaultRoomGetter @Inject constructor(
.equalTo(RoomSummaryEntityFields.IS_DIRECT, true)
.equalTo(RoomSummaryEntityFields.MEMBERSHIP_STR, Membership.JOIN.name)
.findAll()
.filter { dm -> dm.otherMemberIds.contains(otherUserId) }
.map { it.roomId }
.firstOrNull { roomId -> otherUserId in RoomMemberHelper(realm, roomId).getActiveRoomMemberIds() }
.firstOrNull { dm -> dm.otherMemberIds.size == 1 && dm.otherMemberIds.first() == otherUserId }
?.roomId
}
}