From 957f3b3eb0421096c59058f7870e7c16507956ae Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Thu, 28 Sep 2023 13:16:03 +0400 Subject: [PATCH] core: delete unused contact silently (#3140) --- src/Simplex/Chat.hs | 26 ++++++++++++++++---------- tests/ChatTests/Direct.hs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index a16cb98ae..822d6cd4b 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -4252,16 +4252,22 @@ processAgentMessageConn user@User {userId} corrId agentConnId agentMessage = do xInfo c p' = void $ processContactProfileUpdate c p' True xDirectDel :: Contact -> RcvMessage -> MsgMeta -> m () - xDirectDel c msg msgMeta = do - checkIntegrityCreateItem (CDDirectRcv c) msgMeta - ct' <- withStore' $ \db -> updateContactStatus db user c CSDeleted - contactConns <- withStore $ \db -> getContactConnections db userId ct' - deleteAgentConnectionsAsync user $ map aConnId contactConns - forM_ contactConns $ \conn -> withStore' $ \db -> updateConnectionStatus db conn ConnDeleted - let ct'' = ct' {activeConn = (contactConn ct') {connStatus = ConnDeleted}} :: Contact - ci <- saveRcvChatItem user (CDDirectRcv ct'') msg msgMeta (CIRcvDirectEvent RDEContactDeleted) - toView $ CRNewChatItem user (AChatItem SCTDirect SMDRcv (DirectChat ct'') ci) - toView $ CRContactDeletedByContact user ct'' + xDirectDel c msg msgMeta = + if directOrUsed c + then do + checkIntegrityCreateItem (CDDirectRcv c) msgMeta + ct' <- withStore' $ \db -> updateContactStatus db user c CSDeleted + contactConns <- withStore $ \db -> getContactConnections db userId ct' + deleteAgentConnectionsAsync user $ map aConnId contactConns + forM_ contactConns $ \conn -> withStore' $ \db -> updateConnectionStatus db conn ConnDeleted + let ct'' = ct' {activeConn = (contactConn ct') {connStatus = ConnDeleted}} :: Contact + ci <- saveRcvChatItem user (CDDirectRcv ct'') msg msgMeta (CIRcvDirectEvent RDEContactDeleted) + toView $ CRNewChatItem user (AChatItem SCTDirect SMDRcv (DirectChat ct'') ci) + toView $ CRContactDeletedByContact user ct'' + else do + contactConns <- withStore $ \db -> getContactConnections db userId c + deleteAgentConnectionsAsync user $ map aConnId contactConns + withStore' $ \db -> deleteContact db user c processContactProfileUpdate :: Contact -> Profile -> Bool -> m Contact processContactProfileUpdate c@Contact {profile = p} p' createItems diff --git a/tests/ChatTests/Direct.hs b/tests/ChatTests/Direct.hs index d9e8bac2f..445a5ab99 100644 --- a/tests/ChatTests/Direct.hs +++ b/tests/ChatTests/Direct.hs @@ -31,6 +31,7 @@ chatDirectTests = do describe "direct messages" $ do describe "add contact and send/receive message" testAddContact it "deleting contact deletes profile" testDeleteContactDeletesProfile + it "unused contact is deleted silently" testDeleteUnusedContactSilent it "direct message quoted replies" testDirectMessageQuotedReply it "direct message update" testDirectMessageUpdate it "direct message edit history" testDirectMessageEditHistory @@ -214,6 +215,42 @@ testDeleteContactDeletesProfile = (bob FilePath -> IO () +testDeleteUnusedContactSilent = + testChatCfg3 testCfgCreateGroupDirect aliceProfile bobProfile cathProfile $ + \alice bob cath -> do + createGroup3 "team" alice bob cath + bob ##> "/contacts" + bob <### ["alice (Alice)", "cath (Catherine)"] + bob `hasContactProfiles` ["bob", "alice", "cath"] + cath ##> "/contacts" + cath <### ["alice (Alice)", "bob (Bob)"] + cath `hasContactProfiles` ["cath", "alice", "bob"] + -- bob deletes cath, cath's bob contact is deleted silently + bob ##> "/d cath" + bob <## "cath: contact is deleted" + bob ##> "/contacts" + bob <## "alice (Alice)" + threadDelay 50000 + cath ##> "/contacts" + cath <## "alice (Alice)" + -- group messages work + alice #> "#team hello" + concurrentlyN_ + [ bob <# "#team alice> hello", + cath <# "#team alice> hello" + ] + bob #> "#team hi there" + concurrentlyN_ + [ alice <# "#team bob> hi there", + cath <# "#team bob> hi there" + ] + cath #> "#team hey" + concurrentlyN_ + [ alice <# "#team cath> hey", + bob <# "#team cath> hey" + ] + testDirectMessageQuotedReply :: HasCallStack => FilePath -> IO () testDirectMessageQuotedReply = testChat2 aliceProfile bobProfile $