From 97fde7ecd0efc264d9b78bbf8009f558e6c37722 Mon Sep 17 00:00:00 2001 From: Efim Poberezkin <8711996+efim-poberezkin@users.noreply.github.com> Date: Sat, 28 Aug 2021 20:54:53 +1000 Subject: [PATCH] subscribe pending connections on chat start (#95) --- src/Simplex/Chat.hs | 5 +++++ src/Simplex/Chat/Store.hs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 68bb15f167..4db11757e8 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -278,6 +278,7 @@ subscribeUserConnections = void . runExceptT $ do user <- readTVarIO =<< asks currentUser subscribeContacts user subscribeGroups user + subscribePendingConnections user where subscribeContacts user = do contacts <- withStore (`getUserContacts` user) @@ -296,6 +297,10 @@ subscribeUserConnections = void . runExceptT $ do forM_ connectedMembers $ \(GroupMember {localDisplayName = c}, cId) -> subscribe cId `catchError` showMemberSubError g c showGroupSubscribed g + subscribePendingConnections user = do + connections <- withStore (`getPendingConnections` user) + forM_ connections $ \Connection {agentConnId} -> + subscribe agentConnId `catchError` \_ -> pure () subscribe cId = withAgent (`subscribeConnection` cId) processAgentMessage :: forall m. ChatMonad m => User -> ConnId -> ACommand 'Agent -> m () diff --git a/src/Simplex/Chat/Store.hs b/src/Simplex/Chat/Store.hs index 95f6dae0ef..a5f529eb50 100644 --- a/src/Simplex/Chat/Store.hs +++ b/src/Simplex/Chat/Store.hs @@ -27,6 +27,7 @@ module Simplex.Chat.Store updateUserProfile, updateContactProfile, getUserContacts, + getPendingConnections, getContactConnections, getConnectionChatDirection, updateConnectionStatus, @@ -336,6 +337,22 @@ getUserContacts st User {userId} = contactNames <- liftIO $ map fromOnly <$> DB.query db "SELECT local_display_name FROM contacts WHERE user_id = ?" (Only userId) rights <$> mapM (runExceptT . getContact_ db userId) contactNames +getPendingConnections :: MonadUnliftIO m => SQLiteStore -> User -> m [Connection] +getPendingConnections st User {userId} = + liftIO . withTransaction st $ \db -> + map toConnection + <$> DB.queryNamed + db + [sql| + SELECT c.connection_id, c.agent_conn_id, c.conn_level, c.via_contact, + c.conn_status, c.conn_type, c.contact_id, c.group_member_id, c.created_at + FROM connections c + WHERE c.user_id = :user_id + AND c.conn_type = :conn_type + AND c.contact_id IS NULL + |] + [":user_id" := userId, ":conn_type" := ConnContact] + getContactConnections :: StoreMonad m => SQLiteStore -> UserId -> ContactName -> m [Connection] getContactConnections st userId displayName = liftIOEither . withTransaction st $ \db ->