From 5de9bf7f2bab79c4fa6599f3654abc6ce5b61040 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Mon, 28 Jul 2025 09:37:14 +0000 Subject: [PATCH] core: fix duplicate banner when connecting to member contact (#6118) --- src/Simplex/Chat/Library/Subscriber.hs | 5 +++-- src/Simplex/Chat/Store/Messages.hs | 9 +++++++++ .../Chat/Store/SQLite/Migrations/chat_query_plans.txt | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Chat/Library/Subscriber.hs b/src/Simplex/Chat/Library/Subscriber.hs index 5166ce7ecb..08a558bd48 100644 --- a/src/Simplex/Chat/Library/Subscriber.hs +++ b/src/Simplex/Chat/Library/Subscriber.hs @@ -581,7 +581,8 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = -- TODO [short links] get contact request by contactRequestId, check encryption (UserContactRequest.pqSupport)? when (directOrUsed ct') $ case (preparedContact ct', contactRequestId' ct') of (Nothing, Nothing) -> do - createInternalChatItem user (CDDirectSnd ct') CIChatBanner (Just epochStart) + unlessM (withStore' $ \db -> checkContactHasItems db user ct') $ + createInternalChatItem user (CDDirectSnd ct') CIChatBanner (Just epochStart) createE2EItem createFeatureEnabledItems user ct' (Just PreparedContact {connLinkToConnect = ACCL _ (CCLink cReq _)}, _) -> @@ -3089,6 +3090,7 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = connIds <- joinConn subMode -- [incognito] reuse membership incognito profile (mCt', m') <- withStore' $ \db -> createMemberContactInvited db user connIds g m mConn subMode + createInternalChatItem user (CDDirectSnd mCt') CIChatBanner (Just epochStart) createItems mCt' m' joinConn subMode = do -- [incognito] send membership incognito profile @@ -3097,7 +3099,6 @@ processAgentMessageConn vr user@User {userId} corrId agentConnId agentMessage = dm <- encodeConnInfo $ XInfo p joinAgentConnectionAsync user True connReq dm subMode createItems mCt' m' = do - createInternalChatItem user (CDDirectSnd mCt') CIChatBanner (Just epochStart) (g', m'', scopeInfo) <- mkGroupChatScope g m' createInternalChatItem user (CDGroupRcv g' scopeInfo m'') (CIRcvGroupEvent RGEMemberCreatedContact) Nothing toView $ CEvtNewMemberContactReceivedInv user mCt' g' m'' diff --git a/src/Simplex/Chat/Store/Messages.hs b/src/Simplex/Chat/Store/Messages.hs index a09ed349ef..0086fcee64 100644 --- a/src/Simplex/Chat/Store/Messages.hs +++ b/src/Simplex/Chat/Store/Messages.hs @@ -43,6 +43,7 @@ module Simplex.Chat.Store.Messages createNewChatItemNoMsg, createNewChatItem_, getChatPreviews, + checkContactHasItems, getDirectChat, getGroupChat, getGroupChatScopeInfoForItem, @@ -1130,6 +1131,14 @@ getContactConnectionChatPreviews_ db User {userId} pagination clq = case clq of aChat = AChat SCTContactConnection $ Chat (ContactConnection conn) [] emptyChatStats in ACPD SCTContactConnection $ ContactConnectionPD updatedAt aChat +checkContactHasItems :: DB.Connection -> User -> Contact -> IO Bool +checkContactHasItems db User {userId} Contact {contactId} = + fromOnly . head + <$> DB.query + db + "SELECT EXISTS (SELECT 1 FROM chat_items WHERE user_id = ? AND contact_id = ?)" + (userId, contactId) + getDirectChat :: DB.Connection -> VersionRangeChat -> User -> Int64 -> ChatPagination -> Maybe String -> ExceptT StoreError IO (Chat 'CTDirect, Maybe NavigationInfo) getDirectChat db vr user contactId pagination search_ = do let search = fromMaybe "" search_ diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/chat_query_plans.txt b/src/Simplex/Chat/Store/SQLite/Migrations/chat_query_plans.txt index ab35c46aa2..deaa68aaef 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations/chat_query_plans.txt +++ b/src/Simplex/Chat/Store/SQLite/Migrations/chat_query_plans.txt @@ -5830,6 +5830,12 @@ Query: SELECT COUNT(1), COALESCE(SUM(user_mention), 0) FROM chat_items WHERE use Plan: SEARCH chat_items USING COVERING INDEX idx_chat_items_group_scope_stats_all (user_id=? AND group_id=? AND group_scope_tag=? AND group_scope_group_member_id=? AND item_status=?) +Query: SELECT EXISTS (SELECT 1 FROM chat_items WHERE user_id = ? AND contact_id = ?) +Plan: +SCAN CONSTANT ROW +SCALAR SUBQUERY 1 +SEARCH chat_items USING COVERING INDEX idx_chat_items_contacts_created_at (user_id=? AND contact_id=?) + Query: SELECT accepted_at FROM operator_usage_conditions WHERE server_operator_id = ? AND conditions_commit = ? Plan: SEARCH operator_usage_conditions USING INDEX idx_operator_usage_conditions_conditions_commit (conditions_commit=? AND server_operator_id=?)