From 69d0a5c7b15f3f8cfa328e2c2e1bcd37db776c72 Mon Sep 17 00:00:00 2001 From: ganfra Date: Wed, 29 Nov 2023 16:16:09 +0100 Subject: [PATCH] Creates a startDM method so we can reuse it for the new flow --- .../impl/root/CreateRoomRootPresenter.kt | 26 +++++------ .../libraries/matrix/api/room/StartDM.kt | 43 +++++++++++++++++++ .../src/main/res/values/localazy.xml | 4 +- 3 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/StartDM.kt diff --git a/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt index 839e4cd69e..bdccf3d625 100644 --- a/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt +++ b/features/createroom/impl/src/main/kotlin/io/element/android/features/createroom/impl/root/CreateRoomRootPresenter.kt @@ -28,10 +28,11 @@ import io.element.android.features.createroom.impl.userlist.UserListPresenter import io.element.android.features.createroom.impl.userlist.UserListPresenterArgs import io.element.android.libraries.architecture.Async import io.element.android.libraries.architecture.Presenter -import io.element.android.libraries.architecture.runCatchingUpdatingState import io.element.android.libraries.core.meta.BuildMeta import io.element.android.libraries.matrix.api.MatrixClient import io.element.android.libraries.matrix.api.core.RoomId +import io.element.android.libraries.matrix.api.room.StartDMResult +import io.element.android.libraries.matrix.api.room.startDM import io.element.android.libraries.matrix.api.user.MatrixUser import io.element.android.libraries.usersearch.api.UserRepository import io.element.android.services.analytics.api.AnalyticsService @@ -79,19 +80,18 @@ class CreateRoomRootPresenter @Inject constructor( } private fun CoroutineScope.startDm(matrixUser: MatrixUser, startDmAction: MutableState>) = launch { - suspend { - matrixClient.findDM(matrixUser.userId).use { existingDM -> - existingDM?.roomId ?: createDM(matrixUser) + startDmAction.value = Async.Loading() + when (val result = matrixClient.startDM(matrixUser)) { + is StartDMResult.Success -> { + if (result.isNew) { + analyticsService.capture(CreatedRoom(isDM = true)) + } + startDmAction.value = Async.Success(result.roomId) } - }.runCatchingUpdatingState(startDmAction) - } + is StartDMResult.Failure -> { + startDmAction.value = Async.Failure(result) + } + } - private suspend fun createDM(user: MatrixUser): RoomId { - return matrixClient - .createDM(user.userId) - .onSuccess { - analyticsService.capture(CreatedRoom(isDM = true)) - } - .getOrThrow() } } diff --git a/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/StartDM.kt b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/StartDM.kt new file mode 100644 index 0000000000..4aa280513b --- /dev/null +++ b/libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/StartDM.kt @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.element.android.libraries.matrix.api.room + +import io.element.android.libraries.matrix.api.MatrixClient +import io.element.android.libraries.matrix.api.core.RoomId +import io.element.android.libraries.matrix.api.user.MatrixUser + +/** + * Try to find an existing DM with the given user, or create one if none exists. + */ +suspend fun MatrixClient.startDM(matrixUser: MatrixUser): StartDMResult { + val existingRoomId = findDM(matrixUser.userId)?.use { existingDM -> + existingDM.roomId + } + return if (existingRoomId != null) { + StartDMResult.Success(existingRoomId, isNew = false) + } else { + createDM(matrixUser.userId).fold( + { StartDMResult.Success(it, isNew = true) }, + { StartDMResult.Failure(it.localizedMessage) } + ) + } +} + +sealed interface StartDMResult { + data class Success(val roomId: RoomId, val isNew: Boolean) : StartDMResult + data class Failure(override val message: String?) : StartDMResult, Exception(message) +} diff --git a/libraries/ui-strings/src/main/res/values/localazy.xml b/libraries/ui-strings/src/main/res/values/localazy.xml index c816850f98..638f4634e3 100644 --- a/libraries/ui-strings/src/main/res/values/localazy.xml +++ b/libraries/ui-strings/src/main/res/values/localazy.xml @@ -109,6 +109,7 @@ "Dark" "Decryption error" "Developer options" + "Direct chat" "(edited)" "Editing" "* %1$s %2$s" @@ -227,9 +228,6 @@ "%d votes" "Rageshake to report bug" - "Are you sure you want to delete this poll?" - "Delete Poll" - "Edit poll" "Failed selecting media, please try again." "Failed processing media to upload, please try again." "Failed uploading media, please try again."