mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 05:31:21 +03:00
Load existing DM instead of creating a new one
This commit is contained in:
parent
4fadc84d83
commit
17bcf9039d
6 changed files with 16 additions and 10 deletions
1
changelog.d/4157.feature
Normal file
1
changelog.d/4157.feature
Normal file
|
@ -0,0 +1 @@
|
|||
Check if DM exists before creating a new one
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue