From ef15dca0b4404e75b56ab78c4d911a71b523ea19 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Fri, 20 Jan 2023 15:02:27 +0400 Subject: [PATCH] core: don't filter out non active user connections on UP & DOWN agent events; use agent connection id instead of db connection id for ContactRef (#1807) --- src/Simplex/Chat.hs | 12 +++++------- src/Simplex/Chat/Controller.hs | 4 ++-- src/Simplex/Chat/Store.hs | 15 +++++++-------- src/Simplex/Chat/Types.hs | 1 + src/Simplex/Chat/View.hs | 4 ++-- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 275e3b4966..42e57e19df 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -1991,16 +1991,14 @@ expireChatItems user@User {userId} ttl sync = do processAgentMessage :: forall m. ChatMonad m => ACorrId -> ConnId -> ACommand 'Agent -> m () processAgentMessage _ "" msg = - asks currentUser >>= readTVarIO >>= \case - Just user -> processAgentMessageNoConn user msg `catchError` (toView . CRChatError (Just user)) - _ -> throwChatError CENoActiveUser + processAgentMessageNoConn msg `catchError` (toView . CRChatError Nothing) processAgentMessage corrId connId msg = withStore' (`getUserByAConnId` AgentConnId connId) >>= \case Just user -> processAgentMessageConn user corrId connId msg `catchError` (toView . CRChatError (Just user)) _ -> throwChatError $ CENoConnectionUser (AgentConnId connId) -processAgentMessageNoConn :: forall m. ChatMonad m => User -> ACommand 'Agent -> m () -processAgentMessageNoConn user@User {userId} = \case +processAgentMessageNoConn :: forall m. ChatMonad m => ACommand 'Agent -> m () +processAgentMessageNoConn = \case CONNECT p h -> hostEvent $ CRHostConnected p h DISCONNECT p h -> hostEvent $ CRHostDisconnected p h DOWN srv conns -> serverEvent srv conns CRContactsDisconnected "disconnected" @@ -2010,8 +2008,8 @@ processAgentMessageNoConn user@User {userId} = \case where hostEvent = whenM (asks $ hostEvents . config) . toView serverEvent srv@(SMPServer host _ _) conns event str = do - cs <- withStore' $ \db -> getConnectionsContacts db userId conns - toView $ event user srv cs + cs <- withStore' $ \db -> getConnectionsContacts db conns + toView $ event srv cs showToast ("server " <> str) (safeDecodeUtf8 $ strEncode host) processAgentMessageConn :: forall m. ChatMonad m => User -> ACorrId -> ConnId -> ACommand 'Agent -> m () diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index 146bd8bd35..934addfb5c 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -396,8 +396,8 @@ data ChatResponse | CRContactConnected {user :: User, contact :: Contact, userCustomProfile :: Maybe Profile} | CRContactAnotherClient {user :: User, contact :: Contact} | CRSubscriptionEnd {user :: User, connectionEntity :: ConnectionEntity} - | CRContactsDisconnected {user :: User, server :: SMPServer, contactRefs :: [ContactRef]} - | CRContactsSubscribed {user :: User, server :: SMPServer, contactRefs :: [ContactRef]} + | CRContactsDisconnected {server :: SMPServer, contactRefs :: [ContactRef]} + | CRContactsSubscribed {server :: SMPServer, contactRefs :: [ContactRef]} | CRContactSubError {contact :: Contact, chatError :: ChatError} -- TODO delete | CRContactSubSummary {user :: User, contactSubscriptions :: [ContactSubStatus]} | CRUserContactSubSummary {user :: User, userContactSubscriptions :: [UserContactSubStatus]} diff --git a/src/Simplex/Chat/Store.hs b/src/Simplex/Chat/Store.hs index 587b87b186..f1d6546433 100644 --- a/src/Simplex/Chat/Store.hs +++ b/src/Simplex/Chat/Store.hs @@ -1715,8 +1715,8 @@ getConnectionById db User {userId} connId = ExceptT $ do |] (userId, connId) -getConnectionsContacts :: DB.Connection -> UserId -> [ConnId] -> IO [ContactRef] -getConnectionsContacts db userId agentConnIds = do +getConnectionsContacts :: DB.Connection -> [ConnId] -> IO [ContactRef] +getConnectionsContacts db agentConnIds = do DB.execute_ db "DROP TABLE IF EXISTS temp.conn_ids" DB.execute_ db "CREATE TABLE temp.conn_ids (conn_id BLOB)" DB.executeMany db "INSERT INTO temp.conn_ids (conn_id) VALUES (?)" $ map Only agentConnIds @@ -1725,19 +1725,18 @@ getConnectionsContacts db userId agentConnIds = do <$> DB.query db [sql| - SELECT ct.contact_id, c.connection_id, ct.local_display_name + SELECT ct.contact_id, c.connection_id, c.agent_conn_id, ct.local_display_name FROM contacts ct JOIN connections c ON c.contact_id = ct.contact_id - WHERE ct.user_id = ? - AND c.agent_conn_id IN (SELECT conn_id FROM temp.conn_ids) + WHERE c.agent_conn_id IN (SELECT conn_id FROM temp.conn_ids) AND c.conn_type = ? |] - (userId, ConnContact) + (Only ConnContact) DB.execute_ db "DROP TABLE temp.conn_ids" pure conns where - toContactRef :: (ContactId, Int64, ContactName) -> ContactRef - toContactRef (contactId, connId, localDisplayName) = ContactRef {contactId, connId, localDisplayName} + toContactRef :: (ContactId, Int64, ConnId, ContactName) -> ContactRef + toContactRef (contactId, connId, acId, localDisplayName) = ContactRef {contactId, connId, agentConnId = AgentConnId acId, localDisplayName} getGroupAndMember :: DB.Connection -> User -> Int64 -> ExceptT StoreError IO (GroupInfo, GroupMember) getGroupAndMember db User {userId, userContactId} groupMemberId = diff --git a/src/Simplex/Chat/Types.hs b/src/Simplex/Chat/Types.hs index 7999f8f341..f79d37211f 100644 --- a/src/Simplex/Chat/Types.hs +++ b/src/Simplex/Chat/Types.hs @@ -173,6 +173,7 @@ contactSecurityCode Contact {activeConn} = connectionCode activeConn data ContactRef = ContactRef { contactId :: ContactId, connId :: Int64, + agentConnId :: AgentConnId, localDisplayName :: ContactName } deriving (Eq, Show, Generic) diff --git a/src/Simplex/Chat/View.hs b/src/Simplex/Chat/View.hs index 753670f00e..8d74601d35 100644 --- a/src/Simplex/Chat/View.hs +++ b/src/Simplex/Chat/View.hs @@ -152,8 +152,8 @@ responseToView user_ ChatConfig {logLevel, testView} liveItems ts = \case CRContactConnected u ct userCustomProfile -> ttyUser u $ viewContactConnected ct userCustomProfile testView CRContactAnotherClient u c -> ttyUser u [ttyContact' c <> ": contact is connected to another client"] CRSubscriptionEnd u acEntity -> ttyUser u [sShow (connId (entityConnection acEntity :: Connection)) <> ": END"] - CRContactsDisconnected u srv cs -> ttyUser u [plain $ "server disconnected " <> showSMPServer srv <> " (" <> contactList cs <> ")"] - CRContactsSubscribed u srv cs -> ttyUser u [plain $ "server connected " <> showSMPServer srv <> " (" <> contactList cs <> ")"] + CRContactsDisconnected srv cs -> [plain $ "server disconnected " <> showSMPServer srv <> " (" <> contactList cs <> ")"] + CRContactsSubscribed srv cs -> [plain $ "server connected " <> showSMPServer srv <> " (" <> contactList cs <> ")"] CRContactSubError c e -> [ttyContact' c <> ": contact error " <> sShow e] CRContactSubSummary u summary -> ttyUser u $ [sShow (length subscribed) <> " contacts connected (use " <> highlight' "/cs" <> " for the list)" | not (null subscribed)] <> viewErrorsSummary errors " contact errors"