From 17bcf9039d837c181b913ce9b48b5a6b679f8055 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Mon, 4 Oct 2021 15:24:51 +0200 Subject: [PATCH] Load existing DM instead of creating a new one --- changelog.d/4157.feature | 1 + .../app/features/createdirect/CreateDirectRoomAction.kt | 3 +-- .../app/features/createdirect/CreateDirectRoomActivity.kt | 5 +---- .../createdirect/CreateDirectRoomByQrCodeFragment.kt | 3 +-- .../app/features/createdirect/CreateDirectRoomViewModel.kt | 7 +++++-- .../vector/app/features/userdirectory/PendingSelection.kt | 7 +++++++ 6 files changed, 16 insertions(+), 10 deletions(-) create mode 100644 changelog.d/4157.feature diff --git a/changelog.d/4157.feature b/changelog.d/4157.feature new file mode 100644 index 0000000000..71cbe60b1f --- /dev/null +++ b/changelog.d/4157.feature @@ -0,0 +1 @@ +Check if DM exists before creating a new one \ No newline at end of file diff --git a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomAction.kt b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomAction.kt index ffc25210e9..da3425d326 100644 --- a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomAction.kt +++ b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomAction.kt @@ -21,7 +21,6 @@ import im.vector.app.features.userdirectory.PendingSelection sealed class CreateDirectRoomAction : VectorViewModelAction { data class CreateRoomAndInviteSelectedUsers( - val selections: Set, - val existingDmRoomId: String? + val selections: Set ) : CreateDirectRoomAction() } diff --git a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomActivity.kt b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomActivity.kt index 68123d5e82..b91ecf0cfe 100644 --- a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomActivity.kt +++ b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomActivity.kt @@ -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)) } } diff --git a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomByQrCodeFragment.kt b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomByQrCodeFragment.kt index 8da0147a43..5f089c6448 100644 --- a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomByQrCodeFragment.kt +++ b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomByQrCodeFragment.kt @@ -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))) ) } } diff --git a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomViewModel.kt b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomViewModel.kt index 7e2671ed7b..f192ea3019 100644 --- a/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/createdirect/CreateDirectRoomViewModel.kt @@ -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 diff --git a/vector/src/main/java/im/vector/app/features/userdirectory/PendingSelection.kt b/vector/src/main/java/im/vector/app/features/userdirectory/PendingSelection.kt index 57f950b2c8..ff3fe5154f 100644 --- a/vector/src/main/java/im/vector/app/features/userdirectory/PendingSelection.kt +++ b/vector/src/main/java/im/vector/app/features/userdirectory/PendingSelection.kt @@ -29,4 +29,11 @@ sealed class PendingSelection { is ThreePidPendingSelection -> threePid.value } } + + fun getMxId(): String { + return when (this) { + is UserPendingSelection -> user.userId + is ThreePidPendingSelection -> threePid.value + } + } }