From a7a56ea1d9ddc5a9a211e5e44e09267cfd1b72ef Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 24 Jan 2023 17:58:08 +0400 Subject: [PATCH] core: use batch delete api when deleting unused group contacts (#1830) --- src/Simplex/Chat.hs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 71fdf7a92..4a6a008b6 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -630,18 +630,24 @@ processChatCommand = \case withStore' $ \db -> deleteGroupItemsAndMembers db user gInfo members withStore' $ \db -> deleteGroup db user gInfo let contactIds = mapMaybe memberContactId members - forM_ contactIds $ \ctId -> - deleteUnusedContact ctId `catchError` (toView . CRChatError (Just user)) + deleteAgentConnectionsAsync user . concat =<< mapM deleteUnusedContact contactIds pure $ CRGroupDeletedUser user gInfo where - deleteUnusedContact contactId = do - ct <- withStore $ \db -> getContact db user contactId - unless (directOrUsed ct) $ do - ctGroupId <- withStore' $ \db -> checkContactHasGroups db user ct - when (isNothing ctGroupId) $ do - conns <- withStore $ \db -> getContactConnections db userId ct - deleteAgentConnectionsAsync user $ map aConnId conns - withStore' $ \db -> deleteContactWithoutGroups db user ct + deleteUnusedContact :: ContactId -> m [ConnId] + deleteUnusedContact contactId = + (withStore (\db -> getContact db user contactId) >>= delete) + `catchError` (\e -> toView (CRChatError (Just user) e) >> pure []) + where + delete ct + | directOrUsed ct = pure [] + | otherwise = + withStore' (\db -> checkContactHasGroups db user ct) >>= \case + Just _ -> pure [] + Nothing -> do + conns <- withStore $ \db -> getContactConnections db userId ct + withStore' (\db -> deleteContactWithoutGroups db user ct) + `catchError` (toView . CRChatError (Just user)) + pure $ map aConnId conns CTContactRequest -> pure $ chatCmdError (Just user) "not supported" APIClearChat (ChatRef cType chatId) -> withUser $ \user -> case cType of CTDirect -> do