From bc1d86e303283e58ddb60edffedf1541c50c66fd Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 24 Jan 2023 20:07:35 +0400 Subject: [PATCH] core: send agent DEL events to view (#1832) --- src/Simplex/Chat.hs | 5 +++++ src/Simplex/Chat/Controller.hs | 17 ++++++++++++++++- src/Simplex/Chat/View.hs | 9 +++++++++ tests/ChatTests.hs | 2 +- 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 497b3b2549..f338fb26ff 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -2013,6 +2013,10 @@ expireChatItems user@User {userId} ttl sync = do processAgentMessage :: forall m. ChatMonad m => ACorrId -> ConnId -> ACommand 'Agent -> m () processAgentMessage _ "" msg = processAgentMessageNoConn msg `catchError` (toView . CRChatError Nothing) +processAgentMessage _ connId (DEL_RCVQ srv qId err_) = + toView $ CRAgentRcvQueueDeleted (AgentConnId connId) srv (AgentQueueId qId) err_ +processAgentMessage _ connId DEL_CONN = + toView $ CRAgentConnDeleted (AgentConnId connId) processAgentMessage corrId connId msg = withStore' (`getUserByAConnId` AgentConnId connId) >>= \case Just user -> processAgentMessageConn user corrId connId msg `catchError` (toView . CRChatError (Just user)) @@ -2025,6 +2029,7 @@ processAgentMessageNoConn = \case DOWN srv conns -> serverEvent srv conns CRContactsDisconnected "disconnected" UP srv conns -> serverEvent srv conns CRContactsSubscribed "connected" SUSPENDED -> toView CRChatSuspended + DEL_USER agentUserId -> toView $ CRAgentUserDeleted agentUserId _ -> pure () where hostEvent = whenM (asks $ hostEvents . config) . toView diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index 0372ad86a2..c1afd0e781 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -53,7 +53,7 @@ import qualified Simplex.Messaging.Crypto as C import Simplex.Messaging.Encoding.String import Simplex.Messaging.Notifications.Protocol (DeviceToken (..), NtfTknStatus) import Simplex.Messaging.Parsers (dropPrefix, enumJSON, parseAll, parseString, sumTypeJSON) -import Simplex.Messaging.Protocol (AProtocolType, CorrId, MsgFlags, NtfServer) +import Simplex.Messaging.Protocol (AProtocolType, CorrId, MsgFlags, NtfServer, QueueId) import Simplex.Messaging.TMap (TMap) import Simplex.Messaging.Transport (simplexMQVersion) import Simplex.Messaging.Transport.Client (TransportHost) @@ -477,6 +477,9 @@ data ChatResponse | CRDebugLocks {chatLockName :: Maybe String, agentLocks :: AgentLocks} | CRAgentStats {agentStats :: [[String]]} | CRConnectionDisabled {connectionEntity :: ConnectionEntity} + | CRAgentRcvQueueDeleted {agentConnId :: AgentConnId, server :: SMPServer, agentQueueId :: AgentQueueId, agentError_ :: Maybe AgentErrorType} + | CRAgentConnDeleted {agentConnId :: AgentConnId} + | CRAgentUserDeleted {agentUserId :: Int64} | CRMessageError {user :: User, severity :: Text, errorMessage :: Text} | CRChatCmdError {user_ :: Maybe User, chatError :: ChatError} | CRChatError {user_ :: Maybe User, chatError :: ChatError} @@ -486,6 +489,18 @@ instance ToJSON ChatResponse where toJSON = J.genericToJSON . sumTypeJSON $ dropPrefix "CR" toEncoding = J.genericToEncoding . sumTypeJSON $ dropPrefix "CR" +newtype AgentQueueId = AgentQueueId QueueId + deriving (Eq, Show) + +instance StrEncoding AgentQueueId where + strEncode (AgentQueueId qId) = strEncode qId + strDecode s = AgentQueueId <$> strDecode s + strP = AgentQueueId <$> strP + +instance ToJSON AgentQueueId where + toJSON = strToJSON + toEncoding = strToJEncoding + data SMPServersConfig = SMPServersConfig {smpServers :: [ServerCfg]} deriving (Show, Generic, FromJSON) diff --git a/src/Simplex/Chat/View.hs b/src/Simplex/Chat/View.hs index 654b81d09f..802a4cebb4 100644 --- a/src/Simplex/Chat/View.hs +++ b/src/Simplex/Chat/View.hs @@ -217,6 +217,15 @@ responseToView user_ ChatConfig {logLevel, testView} liveItems ts = \case ] CRAgentStats stats -> map (plain . intercalate ",") stats CRConnectionDisabled entity -> viewConnectionEntityDisabled entity + CRAgentRcvQueueDeleted acId srv aqId err_ -> + [ "completed deleting rcv queue, agent connection id: " <> sShow acId + <> (", server: " <> sShow srv) + <> (", agent queue id: " <> sShow aqId) + <> maybe "" (\e -> ", error: " <> sShow e) err_ + | logLevel <= CLLInfo + ] + CRAgentConnDeleted acId -> ["completed deleting connection, agent connection id: " <> sShow acId | logLevel <= CLLInfo] + CRAgentUserDeleted auId -> ["completed deleting user" <> if logLevel <= CLLInfo then ", agent user id: " <> sShow auId else ""] CRMessageError u prefix err -> ttyUser u [plain prefix <> ": " <> plain err | prefix == "error" || logLevel <= CLLWarning] CRChatCmdError u e -> ttyUser' u $ viewChatError logLevel e CRChatError u e -> ttyUser' u $ viewChatError logLevel e diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index bb13882130..04b76e7440 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -4631,7 +4631,7 @@ testDeleteUser = alice <## "alisa2 (active)" alice ##> "/delete user alisa" - alice <## "ok" + alice <### ["ok", "completed deleting user"] alice ##> "/users" alice <## "alisa2 (active)"