From 60fedbf5d2fd6a39d960ecbfa007fa0aca9b91e4 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Fri, 25 Nov 2022 15:37:36 +0000 Subject: [PATCH] core: only create feature items in used contacts (#1421) * core: only create feature items in used contacts * fix, test --- src/Simplex/Chat.hs | 13 +++++-------- src/Simplex/Chat/Types.hs | 4 ++++ tests/ChatTests.hs | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 0c0c568726..717ed1c727 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -831,7 +831,7 @@ processChatCommand = \case contacts <- withStore' (`getUserContacts` user) withChatLock "sendMessageBroadcast" . procCmd $ do let mc = MCText $ safeDecodeUtf8 msg - cts = filter (\ct -> isReady ct && isDirect ct) contacts + cts = filter (\ct -> isReady ct && directContact ct) contacts forM_ cts $ \ct -> void ( do @@ -840,9 +840,6 @@ processChatCommand = \case ) `catchError` (toView . CRChatError) CRBroadcastSent mc (length cts) <$> liftIO getZonedTime - where - isDirect Contact {contactUsed, activeConn = Connection {connLevel, viaGroupLink}} = - (connLevel == 0 && not viaGroupLink) || contactUsed SendMessageQuote cName (AMsgDirection msgDir) quotedMsg msg -> withUser $ \user@User {userId} -> do contactId <- withStore $ \db -> getContactIdByName db user cName quotedItemId <- withStore $ \db -> getDirectChatItemIdByText db userId contactId msgDir (safeDecodeUtf8 quotedMsg) @@ -1191,7 +1188,7 @@ processChatCommand = \case let mergedProfile = userProfileToSend user' Nothing $ Just ct ct' = updateMergedPreferences user' ct void (sendDirectContactMessage ct $ XInfo mergedProfile) `catchError` (toView . CRChatError) - createFeatureChangedItems user' ct ct' CDDirectSnd CISndChatFeature + when (directContact ct) $ createFeatureChangedItems user' ct ct' CDDirectSnd CISndChatFeature pure $ CRUserProfileUpdated (fromLocalProfile p) p' updateContactPrefs :: User -> Contact -> Preferences -> m ChatResponse updateContactPrefs user@User {userId} ct@Contact {activeConn = Connection {customUserProfileId}, userPreferences = contactUserPrefs} contactUserPrefs' @@ -1202,7 +1199,7 @@ processChatCommand = \case let p' = userProfileToSend user (fromLocalProfile <$> incognitoProfile) (Just ct') withChatLock "updateProfile" . procCmd $ do void (sendDirectContactMessage ct' $ XInfo p') `catchError` (toView . CRChatError) - createFeatureChangedItems user ct ct' CDDirectSnd CISndChatFeature + when (directContact ct) $ createFeatureChangedItems user ct ct' CDDirectSnd CISndChatFeature pure $ CRContactPrefsUpdated ct ct' isReady :: Contact -> Bool isReady ct = @@ -1762,7 +1759,7 @@ processAgentMessage (Just user@User {userId}) corrId agentConnId agentMessage = -- [incognito] print incognito profile used for this contact incognitoProfile <- forM customUserProfileId $ \profileId -> withStore (\db -> getProfileById db userId profileId) toView $ CRContactConnected ct (fmap fromLocalProfile incognitoProfile) - createFeatureEnabledItems ct + when (directContact ct) $ createFeatureEnabledItems ct setActive $ ActiveC c showToast (c <> "> ") "connected" forM_ groupLinkId $ \_ -> probeMatchingContacts ct $ contactConnIncognito ct @@ -2503,7 +2500,7 @@ processAgentMessage (Just user@User {userId}) corrId agentConnId agentMessage = xInfo c@Contact {profile = p} p' = unless (fromLocalProfile p == p') $ do c' <- withStore $ \db -> updateContactProfile db user c p' toView $ CRContactUpdated c c' - createFeatureChangedItems user c c' CDDirectRcv CIRcvChatFeature + when (directContact c) $ createFeatureChangedItems user c c' CDDirectRcv CIRcvChatFeature createFeatureEnabledItems :: Contact -> m () createFeatureEnabledItems ct@Contact {mergedPreferences} = diff --git a/src/Simplex/Chat/Types.hs b/src/Simplex/Chat/Types.hs index 3f8db5259f..564f112aec 100644 --- a/src/Simplex/Chat/Types.hs +++ b/src/Simplex/Chat/Types.hs @@ -119,6 +119,10 @@ contactConnId = aConnId . contactConn contactConnIncognito :: Contact -> Bool contactConnIncognito = connIncognito . contactConn +directContact :: Contact -> Bool +directContact Contact {contactUsed, activeConn = Connection {connLevel, viaGroupLink}} = + (connLevel == 0 && not viaGroupLink) || contactUsed + data ContactRef = ContactRef { contactId :: ContactId, localDisplayName :: ContactName diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index e8fe6ba589..991ca8ecc1 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -3678,8 +3678,8 @@ testGroupLink = alice @@@ [("#team", "connected")] bob @@@ [("#team", "connected")] -- calling /_get chat api marks it as used and adds it to chat previews - alice #$> ("/_get chat @2 count=100", chat, chatFeatures) - alice @@@ [("@bob", "Voice messages: enabled"), ("#team", "connected")] + alice #$> ("/_get chat @2 count=100", chat, []) + alice @@@ [("@bob", ""), ("#team", "connected")] alice <##> bob alice @@@ [("@bob", "hey"), ("#team", "connected")]