From 1a6245fe51bf04a625ee042d39973e52bee17b93 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Wed, 7 Aug 2024 23:42:31 +0900 Subject: [PATCH] android, desktop: prevent migration when network conf wasn't applied (#4615) * android, desktop: prevent migration when network conf wasn't applied * name of param --- .../kotlin/chat/simplex/common/model/SimpleXAPI.kt | 5 ++++- .../chat/simplex/common/views/chatlist/ChatListView.kt | 2 +- .../chat/simplex/common/views/migration/MigrateFromDevice.kt | 5 ++++- .../chat/simplex/common/views/migration/MigrateToDevice.kt | 4 +++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt index 5419ef70fe..5f14153b14 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt @@ -529,7 +529,10 @@ object ChatController { suspend fun startChatWithTemporaryDatabase(ctrl: ChatCtrl, netCfg: NetCfg): User? { Log.d(TAG, "startChatWithTemporaryDatabase") val migrationActiveUser = apiGetActiveUser(null, ctrl) ?: apiCreateActiveUser(null, Profile(displayName = "Temp", fullName = ""), ctrl = ctrl) - apiSetNetworkConfig(netCfg, ctrl) + if (!apiSetNetworkConfig(netCfg, ctrl)) { + Log.e(TAG, "Error setting network config, stopping migration") + return null + } apiSetAppFilePaths( getMigrationTempFilesDirectory().absolutePath, getMigrationTempFilesDirectory().absolutePath, diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListView.kt index 207f8a4b26..c75b897e77 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chatlist/ChatListView.kt @@ -337,7 +337,7 @@ fun SubscriptionStatusIndicator(click: (() -> Unit)) { val scope = rememberCoroutineScope() suspend fun setSubsTotal() { - if (chatModel.currentUser.value != null) { + if (chatModel.currentUser.value != null && chatModel.controller.hasChatCtrl() && chatModel.chatRunning.value == true) { val r = chatModel.controller.getAgentSubsTotal(chatModel.remoteHostId()) if (r != null) { subs = r.first diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/migration/MigrateFromDevice.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/migration/MigrateFromDevice.kt index c60723ee36..152befb82d 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/migration/MigrateFromDevice.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/migration/MigrateFromDevice.kt @@ -516,8 +516,11 @@ suspend fun initTemporaryDatabase(tempDatabaseFile: File, netCfg: NetCfg): Pair< try { if (ctrl != null) { val user = startChatWithTemporaryDatabase(ctrl, netCfg) - return if (user != null) ctrl to user else null + if (user != null) return ctrl to user + chatCloseStore(ctrl) } + File(tempDatabaseFile.absolutePath + "_chat.db").delete() + File(tempDatabaseFile.absolutePath + "_agent.db").delete() } catch (e: Throwable) { Log.e(TAG, "Error while starting chat in temporary database: ${e.stackTraceToString()}") } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/migration/MigrateToDevice.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/migration/MigrateToDevice.kt index 1a1e8426d4..f8a82d010d 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/migration/MigrateToDevice.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/migration/MigrateToDevice.kt @@ -515,7 +515,9 @@ private fun MutableState.prepareDatabase( withLongRunningApi { val ctrlAndUser = initTemporaryDatabase(tempDatabaseFile, netCfg) if (ctrlAndUser == null) { - state = MigrationToState.DownloadFailed(0, link, archivePath(), netCfg) + // Probably, something wrong with network config or database initialization, let's start from scratch + state = MigrationToState.PasteOrScanLink + MigrationToDeviceState.save(null) return@withLongRunningApi }