From 305052ecaf95858630f6667dcaf34026e98622b0 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Sat, 30 Apr 2022 12:47:50 +0100 Subject: [PATCH] core: api to resubscribe connections (#586) * core: api to resubscribe connections * update simplexmq --- cabal.project | 2 +- scripts/nix/sha256map.nix | 2 +- src/Simplex/Chat.hs | 16 +++++++++++----- src/Simplex/Chat/Controller.hs | 1 + stack.yaml | 2 +- tests/ChatClient.hs | 4 ++-- 6 files changed, 17 insertions(+), 10 deletions(-) diff --git a/cabal.project b/cabal.project index dba79c9c94..fa47eee7b2 100644 --- a/cabal.project +++ b/cabal.project @@ -3,7 +3,7 @@ packages: . source-repository-package type: git location: https://github.com/simplex-chat/simplexmq.git - tag: f8ec838912579159ca0ebd956a559fc20bf1d7ea + tag: 1064e9c315bb4b9c09ba62141f86e21690876d76 source-repository-package type: git diff --git a/scripts/nix/sha256map.nix b/scripts/nix/sha256map.nix index f13c239d0e..523616273e 100644 --- a/scripts/nix/sha256map.nix +++ b/scripts/nix/sha256map.nix @@ -1,5 +1,5 @@ { - "https://github.com/simplex-chat/simplexmq.git"."f8ec838912579159ca0ebd956a559fc20bf1d7ea" = "0kywhk2q92h3j24pp9lxycs062myl8ch7wdwlyd6v82lcm14w8pd"; + "https://github.com/simplex-chat/simplexmq.git"."1064e9c315bb4b9c09ba62141f86e21690876d76" = "10acs17k6rp6xx58rsfwz0xgq6yhfkiy2rxj86pyxan7867dnawr"; "https://github.com/simplex-chat/aeson.git"."3eb66f9a68f103b5f1489382aad89f5712a64db7" = "0kilkx59fl6c3qy3kjczqvm8c3f4n3p0bdk9biyflf51ljnzp4yp"; "https://github.com/simplex-chat/haskell-terminal.git"."f708b00009b54890172068f168bf98508ffcd495" = "0zmq7lmfsk8m340g47g5963yba7i88n4afa6z93sg9px5jv1mijj"; "https://github.com/zw3rk/android-support.git"."3c3a5ab0b8b137a072c98d3d0937cbdc96918ddb" = "1r6jyxbim3dsvrmakqfyxbd6ms6miaghpbwyl0sr6dzwpgaprz97"; diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 3d8ad8da35..bc322ec2cd 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -183,6 +183,7 @@ processChatCommand = \case asks agentAsync >>= readTVarIO >>= \case Just _ -> pure CRChatRunning _ -> startChatController user $> CRChatStarted + ResubscribeAllConnections -> withUser (subscribeUserConnections resubscribeConnection) $> CRCmdOk SetFilesFolder filesFolder' -> withUser $ \_ -> do createDirectoryIfMissing True filesFolder' ff <- asks filesFolder @@ -824,15 +825,19 @@ agentSubscriber :: (MonadUnliftIO m, MonadReader ChatController m) => User -> m agentSubscriber user = do q <- asks $ subQ . smpAgent l <- asks chatLock - subscribeUserConnections user + subscribeUserConnections subscribeConnection user forever $ do (_, connId, msg) <- atomically $ readTBQueue q u <- readTVarIO =<< asks currentUser withLock l . void . runExceptT $ processAgentMessage u connId msg `catchError` (toView . CRChatError) -subscribeUserConnections :: (MonadUnliftIO m, MonadReader ChatController m) => User -> m () -subscribeUserConnections user@User {userId} = do +subscribeUserConnections :: + (MonadUnliftIO m, MonadReader ChatController m) => + (forall m'. ChatMonad m' => AgentClient -> ConnId -> ExceptT AgentErrorType m' ()) -> + User -> + m () +subscribeUserConnections agentSubscribe user@User {userId} = do n <- asks $ subscriptionConcurrency . config ce <- asks $ subscriptionEvents . config void . runExceptT $ do @@ -902,10 +907,10 @@ subscribeUserConnections user@User {userId} = do cs <- withStore (`getUserContactLinkConnections` userId) (subscribeConns n cs >> toView CRUserContactLinkSubscribed) `catchError` (toView . CRUserContactLinkSubError) - subscribe cId = withAgent (`subscribeConnection` cId) + subscribe cId = withAgent (`agentSubscribe` cId) subscribeConns n conns = withAgent $ \a -> - pooledForConcurrentlyN_ n conns $ \c -> subscribeConnection a (aConnId c) + pooledForConcurrentlyN_ n conns $ \c -> agentSubscribe a (aConnId c) processAgentMessage :: forall m. ChatMonad m => Maybe User -> ConnId -> ACommand 'Agent -> m () processAgentMessage Nothing _ _ = throwChatError CENoActiveUser @@ -1914,6 +1919,7 @@ chatCommandP = ("/user " <|> "/u ") *> (CreateActiveUser <$> userProfile) <|> ("/user" <|> "/u") $> ShowActiveUser <|> "/_start" $> StartChat + <|> "/_resubscribe all" $> ResubscribeAllConnections <|> "/_files_folder " *> (SetFilesFolder <$> filePath) <|> "/_get chats" *> (APIGetChats <$> (" pcc=on" $> True <|> " pcc=off" $> False <|> pure False)) <|> "/_get chat " *> (APIGetChat <$> chatRefP <* A.space <*> chatPaginationP) diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index a3365f8ead..774d357aa8 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -95,6 +95,7 @@ data ChatCommand = ShowActiveUser | CreateActiveUser Profile | StartChat + | ResubscribeAllConnections | SetFilesFolder FilePath | APIGetChats {pendingConnections :: Bool} | APIGetChat ChatRef ChatPagination diff --git a/stack.yaml b/stack.yaml index 1ed52e5f44..cfa84cd45c 100644 --- a/stack.yaml +++ b/stack.yaml @@ -49,7 +49,7 @@ extra-deps: # - simplexmq-1.0.0@sha256:34b2004728ae396e3ae449cd090ba7410781e2b3cefc59259915f4ca5daa9ea8,8561 # - ../simplexmq - github: simplex-chat/simplexmq - commit: f8ec838912579159ca0ebd956a559fc20bf1d7ea + commit: 1064e9c315bb4b9c09ba62141f86e21690876d76 # - terminal-0.2.0.0@sha256:de6770ecaae3197c66ac1f0db5a80cf5a5b1d3b64a66a05b50f442de5ad39570,2977 - github: simplex-chat/aeson commit: 3eb66f9a68f103b5f1489382aad89f5712a64db7 diff --git a/tests/ChatClient.hs b/tests/ChatClient.hs index 9716632e9c..d4a419623e 100644 --- a/tests/ChatClient.hs +++ b/tests/ChatClient.hs @@ -209,8 +209,8 @@ serverCfg = msgIdBytes = 6, storeLogFile = Nothing, allowNewQueues = True, - messageTTL = Just $ 7 * 86400, -- 7 days - expireMessagesInterval = Just 21600_000000, -- microseconds, 6 hours + messageExpiration = Just defaultMessageExpiration, + inactiveClientExpiration = Just defaultInactiveClientExpiration, caCertificateFile = "tests/fixtures/tls/ca.crt", privateKeyFile = "tests/fixtures/tls/server.key", certificateFile = "tests/fixtures/tls/server.crt"