diff --git a/CHANGES.md b/CHANGES.md index 685d8c01bd..e63cbac561 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -38,6 +38,7 @@ Bugfix 🐛: - Render image event even if thumbnail_info does not have mimetype defined (#1209) - RiotX now uses as many threads as it needs to do work and send messages (#1221) - Fix issue with media path (#1227) + - Add user to direct chat by user id (#1065) Translations 🗣: - diff --git a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomActivity.kt b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomActivity.kt index 12674e5cd2..3ae206cd21 100644 --- a/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/createdirect/CreateDirectRoomActivity.kt @@ -28,6 +28,7 @@ import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Success import com.airbnb.mvrx.viewModel +import im.vector.matrix.android.api.failure.Failure import im.vector.matrix.android.api.session.room.failure.CreateRoomFailure import im.vector.riotx.R import im.vector.riotx.core.di.ScreenComponent @@ -37,6 +38,7 @@ import im.vector.riotx.core.extensions.addFragmentToBackstack import im.vector.riotx.core.platform.SimpleFragmentActivity import im.vector.riotx.core.platform.WaitingViewData import kotlinx.android.synthetic.main.activity.* +import java.net.HttpURLConnection import javax.inject.Inject class CreateDirectRoomActivity : SimpleFragmentActivity() { @@ -91,8 +93,14 @@ class CreateDirectRoomActivity : SimpleFragmentActivity() { if (error is CreateRoomFailure.CreatedWithTimeout) { finish() } else { + val message = if (error is Failure.ServerError && error.httpCode == HttpURLConnection.HTTP_INTERNAL_ERROR /*500*/) { + // This error happen if the invited userId does not exist. + getString(R.string.create_room_dm_failure) + } else { + errorFormatter.toHumanReadable(error) + } AlertDialog.Builder(this) - .setMessage(errorFormatter.toHumanReadable(error)) + .setMessage(message) .setPositiveButton(R.string.ok, null) .show() } diff --git a/vector/src/main/java/im/vector/riotx/features/createdirect/DirectoryUsersController.kt b/vector/src/main/java/im/vector/riotx/features/createdirect/DirectoryUsersController.kt index 016806f319..1c38e6f723 100644 --- a/vector/src/main/java/im/vector/riotx/features/createdirect/DirectoryUsersController.kt +++ b/vector/src/main/java/im/vector/riotx/features/createdirect/DirectoryUsersController.kt @@ -23,6 +23,7 @@ import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Success import com.airbnb.mvrx.Uninitialized +import im.vector.matrix.android.api.MatrixPatterns import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.user.model.User import im.vector.matrix.android.api.util.toMatrixItem @@ -56,15 +57,29 @@ class DirectoryUsersController @Inject constructor(private val session: Session, override fun buildModels() { val currentState = state ?: return val hasSearch = currentState.directorySearchTerm.isNotBlank() - val asyncUsers = currentState.directoryUsers - when (asyncUsers) { + when (val asyncUsers = currentState.directoryUsers) { is Uninitialized -> renderEmptyState(false) is Loading -> renderLoading() - is Success -> renderSuccess(asyncUsers(), currentState.selectedUsers.map { it.userId }, hasSearch) + is Success -> renderSuccess( + computeUsersList(asyncUsers(), currentState.directorySearchTerm), + currentState.selectedUsers.map { it.userId }, + hasSearch + ) is Fail -> renderFailure(asyncUsers.error) } } + /** + * Eventually add the searched terms, if it is a userId, and if not already present in the result + */ + private fun computeUsersList(directoryUsers: List, searchTerms: String): List { + return directoryUsers + + searchTerms + .takeIf { terms -> MatrixPatterns.isUserId(terms) && !directoryUsers.any { it.userId == terms } } + ?.let { listOf(User(it)) } + .orEmpty() + } + private fun renderLoading() { loadingItem { id("loading") diff --git a/vector/src/main/res/values/strings_riotX.xml b/vector/src/main/res/values/strings_riotX.xml index 932feba541..cb1c8323f2 100644 --- a/vector/src/main/res/values/strings_riotX.xml +++ b/vector/src/main/res/values/strings_riotX.xml @@ -30,4 +30,5 @@ + "We couldn't create your DM. Please check the users you want to invite and try again."