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:
commit
94bbd37313
|
@ -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 {
|
sealed class CreateDirectRoomAction : VectorViewModelAction {
|
||||||
data class CreateRoomAndInviteSelectedUsers(
|
data class CreateRoomAndInviteSelectedUsers(
|
||||||
val selections: Set<PendingSelection>,
|
val selections: Set<PendingSelection>
|
||||||
val existingDmRoomId: String?
|
|
||||||
) : CreateDirectRoomAction()
|
) : CreateDirectRoomAction()
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -31,4 +31,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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue