From 4b382841a12336d826d75f13766711ec3f171516 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Mon, 29 Jul 2024 18:44:34 +0100 Subject: [PATCH] check chat is running --- .../chat/simplex/common/model/SimpleXAPI.kt | 29 +++++++++++++++---- src/Simplex/Chat.hs | 2 ++ src/Simplex/Chat/Controller.hs | 1 + 3 files changed, 26 insertions(+), 6 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 c854560d4b..14481ed235 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 @@ -461,15 +461,14 @@ object ChatController { Log.d(TAG, "user: $user") try { apiSetNetworkConfig(getNetCfg()) + val chatRunning = apiCheckChatRunning() val users = listUsers(null) chatModel.users.clear() chatModel.users.addAll(users) - chatModel.currentUser.value = user - chatModel.localUserCreated.value = true - getUserChatData(null) - val justStarted = apiStartChat() - appPrefs.chatStopped.set(false) - if (justStarted) { + if (!chatRunning) { + chatModel.currentUser.value = user + chatModel.localUserCreated.value = true + getUserChatData(null) appPrefs.chatLastStart.set(Clock.System.now()) chatModel.chatRunning.value = true startReceiver() @@ -479,8 +478,14 @@ object ChatController { } Log.d(TAG, "startChat: started") } else { + updatingChatsMutex.withLock { + val chats = apiGetChats(null) + chatModel.updateChats(chats) + } Log.d(TAG, "startChat: running") } + apiStartChat() + appPrefs.chatStopped.set(false) } catch (e: Throwable) { Log.e(TAG, "failed starting chat $e") throw e @@ -734,6 +739,15 @@ object ChatController { } } + suspend fun apiCheckChatRunning(): Boolean { + val r = sendCmd(null, CC.CheckChatRunning()) + when (r) { + is CR.ChatRunning -> return true + is CR.ChatStopped -> return false + else -> throw Exception("failed check chat running: ${r.responseType} ${r.details}") + } + } + suspend fun apiStopChat(): Boolean { val r = sendCmd(null, CC.ApiStopChat()) when (r) { @@ -2705,6 +2719,7 @@ sealed class CC { class ApiUnmuteUser(val userId: Long): CC() class ApiDeleteUser(val userId: Long, val delSMPQueues: Boolean, val viewPwd: String?): CC() class StartChat(val mainApp: Boolean): CC() + class CheckChatRunning: CC() class ApiStopChat: CC() @Serializable class ApiSetAppFilePaths(val appFilesFolder: String, val appTempFolder: String, val appAssetsFolder: String, val appRemoteHostsFolder: String): CC() @@ -2850,6 +2865,7 @@ sealed class CC { is ApiUnmuteUser -> "/_unmute user $userId" is ApiDeleteUser -> "/_delete user $userId del_smp=${onOff(delSMPQueues)}${maybePwd(viewPwd)}" is StartChat -> "/_start main=${onOff(mainApp)}" + is CheckChatRunning -> "/_check running" is ApiStopChat -> "/_stop" is ApiSetAppFilePaths -> "/set file paths ${json.encodeToString(this)}" is ApiSetEncryptLocalFiles -> "/_files_encrypt ${onOff(enable)}" @@ -3003,6 +3019,7 @@ sealed class CC { is ApiUnmuteUser -> "apiUnmuteUser" is ApiDeleteUser -> "apiDeleteUser" is StartChat -> "startChat" + is CheckChatRunning -> "checkChatRunning" is ApiStopChat -> "apiStopChat" is ApiSetAppFilePaths -> "apiSetAppFilePaths" is ApiSetEncryptLocalFiles -> "apiSetEncryptLocalFiles" diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 2afe0d13ee..08532bcb6d 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -625,6 +625,7 @@ processChatCommand' vr = \case asks agentAsync >>= readTVarIO >>= \case Just _ -> pure CRChatRunning _ -> checkStoreNotChanged . lift $ startChatController mainApp enableSndFiles $> CRChatStarted + CheckChatRunning -> maybe CRChatStopped (const CRChatRunning) <$> chatReadVar agentAsync APIStopChat -> do ask >>= liftIO . stopChatController pure CRChatStopped @@ -7393,6 +7394,7 @@ chatCommandP = enableSndFiles <- " snd_files=" *> onOffP <|> pure mainApp pure StartChat {mainApp, enableSndFiles}, "/_start" $> StartChat True True, + "/_check running" $> CheckChatRunning, "/_stop" $> APIStopChat, "/_app activate restore=" *> (APIActivateChat <$> onOffP), "/_app activate" $> APIActivateChat True, diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index b7eff9101a..0c34ea2beb 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -264,6 +264,7 @@ data ChatCommand | APIDeleteUser UserId Bool (Maybe UserPwd) | DeleteUser UserName Bool (Maybe UserPwd) | StartChat {mainApp :: Bool, enableSndFiles :: Bool} -- enableSndFiles has no effect when mainApp is True + | CheckChatRunning | APIStopChat | APIActivateChat {restoreChat :: Bool} | APISuspendChat {suspendTimeout :: Int}