Load existing DM instead of creating a new one

This commit is contained in:
Florian Renaud 2021-10-04 15:24:51 +02:00
parent 4fadc84d83
commit 17bcf9039d
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 { sealed class CreateDirectRoomAction : VectorViewModelAction {
data class CreateRoomAndInviteSelectedUsers( data class CreateRoomAndInviteSelectedUsers(
val selections: Set<PendingSelection>, val selections: Set<PendingSelection>
val existingDmRoomId: String?
) : CreateDirectRoomAction() ) : CreateDirectRoomAction()
} }

View file

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

View file

@ -99,7 +99,6 @@ class CreateDirectRoomByQrCodeFragment @Inject constructor() : VectorBaseFragmen
Toast.makeText(requireContext(), R.string.invalid_qr_code_uri, Toast.LENGTH_SHORT).show() Toast.makeText(requireContext(), R.string.invalid_qr_code_uri, Toast.LENGTH_SHORT).show()
requireActivity().finish() requireActivity().finish()
} else { } else {
val existingDm = viewModel.session.getExistingDirectRoomWithUser(mxid)
// The following assumes MXIDs are case insensitive // The following assumes MXIDs are case insensitive
if (mxid.equals(other = viewModel.session.myUserId, ignoreCase = true)) { if (mxid.equals(other = viewModel.session.myUserId, ignoreCase = true)) {
Toast.makeText(requireContext(), R.string.cannot_dm_self, Toast.LENGTH_SHORT).show() 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) val qrInvitee = if (viewModel.session.getUser(mxid) != null) viewModel.session.getUser(mxid)!! else User(mxid, null, null)
viewModel.handle( 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. * If users already have a DM room then navigate to it instead of creating a new room.
*/ */
private fun onSubmitInvitees(action: CreateDirectRoomAction.CreateRoomAndInviteSelectedUsers) { 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 // Do not create a new DM, just tell that the creation is successful by passing the existing roomId
setState { setState {
copy(createAndInviteState = Success(action.existingDmRoomId)) copy(createAndInviteState = Success(existingRoomId))
} }
} else { } else {
// Create the DM // Create the DM

View file

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