check chat is running

This commit is contained in:
Evgeny Poberezkin
2024-07-29 18:44:34 +01:00
parent 6834138270
commit 4b382841a1
3 changed files with 26 additions and 6 deletions
@@ -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"
+2
View File
@@ -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,
+1
View File
@@ -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}