Merge pull request #4157 from vector-im/feature/fre/create_dm_improvement

Load existing DM instead of creating a new one
This commit is contained in:
Benoit Marty 2021-10-05 12:26:22 +02:00 committed by GitHub
commit 94bbd37313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 10 deletions

1
changelog.d/4157.feature Normal file
View File

@ -0,0 +1 @@
Check if DM exists before creating a new one

View File

@ -21,7 +21,6 @@ import im.vector.app.features.userdirectory.PendingSelection
sealed class CreateDirectRoomAction : VectorViewModelAction {
data class CreateRoomAndInviteSelectedUsers(
val selections: Set<PendingSelection>,
val existingDmRoomId: String?
val selections: Set<PendingSelection>
) : CreateDirectRoomAction()
}

View File

@ -138,10 +138,7 @@ class CreateDirectRoomActivity : SimpleFragmentActivity(), UserListViewModel.Fac
private fun onMenuItemSelected(action: UserListSharedAction.OnMenuItemSelected) {
if (action.itemId == R.id.action_create_direct_room) {
viewModel.handle(CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers(
action.selections,
null
))
viewModel.handle(CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers(action.selections))
}
}

View File

@ -99,7 +99,6 @@ class CreateDirectRoomByQrCodeFragment @Inject constructor() : VectorBaseFragmen
Toast.makeText(requireContext(), R.string.invalid_qr_code_uri, Toast.LENGTH_SHORT).show()
requireActivity().finish()
} else {
val existingDm = viewModel.session.getExistingDirectRoomWithUser(mxid)
// The following assumes MXIDs are case insensitive
if (mxid.equals(other = viewModel.session.myUserId, ignoreCase = true)) {
Toast.makeText(requireContext(), R.string.cannot_dm_self, Toast.LENGTH_SHORT).show()
@ -109,7 +108,7 @@ class CreateDirectRoomByQrCodeFragment @Inject constructor() : VectorBaseFragmen
val qrInvitee = if (viewModel.session.getUser(mxid) != null) viewModel.session.getUser(mxid)!! else User(mxid, null, null)
viewModel.handle(
CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers(setOf(PendingSelection.UserPendingSelection(qrInvitee)), existingDm)
CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers(setOf(PendingSelection.UserPendingSelection(qrInvitee)))
)
}
}

View File

@ -71,10 +71,13 @@ class CreateDirectRoomViewModel @AssistedInject constructor(@Assisted
* If users already have a DM room then navigate to it instead of creating a new room.
*/
private fun onSubmitInvitees(action: CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers) {
if (action.existingDmRoomId != null) {
val existingRoomId = action.selections.singleOrNull()?.getMxId()?.let { userId ->
session.getExistingDirectRoomWithUser(userId)
}
if (existingRoomId != null) {
// Do not create a new DM, just tell that the creation is successful by passing the existing roomId
setState {
copy(createAndInviteState = Success(action.existingDmRoomId))
copy(createAndInviteState = Success(existingRoomId))
}
} else {
// Create the DM

View File

@ -31,4 +31,11 @@ sealed class PendingSelection {
is ThreePidPendingSelection -> threePid.value
}
}
fun getMxId(): String {
return when (this) {
is UserPendingSelection -> user.userId
is ThreePidPendingSelection -> threePid.value
}
}
}