From a668bd57361f86c7cb419c3f7ce62bdb5ece61b0 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 17 Jan 2023 16:58:36 +0400 Subject: [PATCH] core: cleanup obsolete chat item deletion code (see #1625) (#1787) --- src/Simplex/Chat.hs | 36 ++---------------------------- src/Simplex/Chat/Store.hs | 46 --------------------------------------- 2 files changed, 2 insertions(+), 80 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 8a1cec3958..b75939b6ed 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -631,33 +631,17 @@ processChatCommand = \case CTDirect -> do ct <- withStore $ \db -> getContact db user chatId filesInfo <- withStore' $ \db -> getContactFileInfo db user ct - -- TODO delete - maxItemTs_ <- withStore' $ \db -> getContactMaxItemTs db user ct forM_ filesInfo $ \fileInfo -> deleteFile user fileInfo withStore' $ \db -> deleteContactCIs db user ct - -- TODO delete - ct' <- case maxItemTs_ of - Just ts -> do - withStore' $ \db -> updateContactTs db user ct ts - pure (ct :: Contact) {updatedAt = ts} - _ -> pure ct - pure $ CRChatCleared user (AChatInfo SCTDirect (DirectChat ct')) + pure $ CRChatCleared user (AChatInfo SCTDirect $ DirectChat ct) CTGroup -> do gInfo <- withStore $ \db -> getGroupInfo db user chatId filesInfo <- withStore' $ \db -> getGroupFileInfo db user gInfo - -- TODO delete - maxItemTs_ <- withStore' $ \db -> getGroupMaxItemTs db user gInfo forM_ filesInfo $ \fileInfo -> deleteFile user fileInfo withStore' $ \db -> deleteGroupCIs db user gInfo membersToDelete <- withStore' $ \db -> getGroupMembersForExpiration db user gInfo forM_ membersToDelete $ \m -> withStore' $ \db -> deleteGroupMember db user m - -- TODO delete - gInfo' <- case maxItemTs_ of - Just ts -> do - withStore' $ \db -> updateGroupTs db user gInfo ts - pure (gInfo :: GroupInfo) {updatedAt = ts} - _ -> pure gInfo - pure $ CRChatCleared user (AChatInfo SCTGroup (GroupChat gInfo')) + pure $ CRChatCleared user (AChatInfo SCTGroup $ GroupChat gInfo) CTContactConnection -> pure $ chatCmdError (Just user) "not supported" CTContactRequest -> pure $ chatCmdError (Just user) "not supported" APIAcceptContact connReqId -> withUser $ \user@User {userId} -> withChatLock "acceptContact" $ do @@ -1978,31 +1962,15 @@ expireChatItems user@User {userId} ttl sync = do processContact :: UTCTime -> Contact -> m () processContact expirationDate ct = do filesInfo <- withStore' $ \db -> getContactExpiredFileInfo db user ct expirationDate - -- TODO delete - maxItemTs_ <- withStore' $ \db -> getContactMaxItemTs db user ct forM_ filesInfo $ \fileInfo -> deleteFile user fileInfo withStore' $ \db -> deleteContactExpiredCIs db user ct expirationDate - -- TODO delete - withStore' $ \db -> do - ciCount_ <- getContactCICount db user ct - case (maxItemTs_, ciCount_) of - (Just ts, Just count) -> when (count == 0) $ updateContactTs db user ct ts - _ -> pure () processGroup :: UTCTime -> UTCTime -> GroupInfo -> m () processGroup expirationDate createdAtCutoff gInfo = do filesInfo <- withStore' $ \db -> getGroupExpiredFileInfo db user gInfo expirationDate createdAtCutoff - -- TODO delete - maxItemTs_ <- withStore' $ \db -> getGroupMaxItemTs db user gInfo forM_ filesInfo $ \fileInfo -> deleteFile user fileInfo withStore' $ \db -> deleteGroupExpiredCIs db user gInfo expirationDate createdAtCutoff membersToDelete <- withStore' $ \db -> getGroupMembersForExpiration db user gInfo forM_ membersToDelete $ \m -> withStore' $ \db -> deleteGroupMember db user m - -- TODO delete - withStore' $ \db -> do - ciCount_ <- getGroupCICount db user gInfo - case (maxItemTs_, ciCount_) of - (Just ts, Just count) -> when (count == 0) $ updateGroupTs db user gInfo ts - _ -> pure () processAgentMessage :: forall m. ChatMonad m => ACorrId -> ConnId -> ACommand 'Agent -> m () processAgentMessage _ "" msg = diff --git a/src/Simplex/Chat/Store.hs b/src/Simplex/Chat/Store.hs index 42f0f03f3a..8f9a281aa2 100644 --- a/src/Simplex/Chat/Store.hs +++ b/src/Simplex/Chat/Store.hs @@ -176,13 +176,9 @@ module Simplex.Chat.Store getFileTransferMeta, getSndFileTransfer, getContactFileInfo, - getContactMaxItemTs, -- TODO delete deleteContactCIs, - updateContactTs, -- TODO delete getGroupFileInfo, - getGroupMaxItemTs, -- TODO delete deleteGroupCIs, - updateGroupTs, -- TODO delete createNewSndMessage, createSndMsgDelivery, createNewMessageAndRcvMsgDelivery, @@ -243,10 +239,8 @@ module Simplex.Chat.Store setChatItemTTL, getContactExpiredFileInfo, deleteContactExpiredCIs, - getContactCICount, -- TODO delete getGroupExpiredFileInfo, deleteGroupExpiredCIs, - getGroupCICount, -- TODO delete getPendingContactConnection, deletePendingContactConnection, updateContactSettings, @@ -3050,12 +3044,6 @@ getContactFileInfo db User {userId} Contact {contactId} = toFileInfo :: (Int64, Maybe ACIFileStatus, Maybe FilePath) -> CIFileInfo toFileInfo (fileId, fileStatus, filePath) = CIFileInfo {fileId, fileStatus, filePath} --- TODO delete -getContactMaxItemTs :: DB.Connection -> User -> Contact -> IO (Maybe UTCTime) -getContactMaxItemTs db User {userId} Contact {contactId} = - fmap join . maybeFirstRow fromOnly $ - DB.query db "SELECT MAX(item_ts) FROM chat_items WHERE user_id = ? AND contact_id = ?" (userId, contactId) - deleteContactCIs :: DB.Connection -> User -> Contact -> IO () deleteContactCIs db user@User {userId} ct@Contact {contactId} = do connIds <- getContactConnIds_ db user ct @@ -3068,14 +3056,6 @@ getContactConnIds_ db User {userId} Contact {contactId} = map fromOnly <$> DB.query db "SELECT connection_id FROM connections WHERE user_id = ? AND contact_id = ?" (userId, contactId) --- TODO delete -updateContactTs :: DB.Connection -> User -> Contact -> UTCTime -> IO () -updateContactTs db User {userId} Contact {contactId} updatedAt = - DB.execute - db - "UPDATE contacts SET chat_ts = ? WHERE user_id = ? AND contact_id = ?" - (updatedAt, userId, contactId) - getGroupFileInfo :: DB.Connection -> User -> GroupInfo -> IO [CIFileInfo] getGroupFileInfo db User {userId} GroupInfo {groupId} = map toFileInfo @@ -3089,25 +3069,11 @@ getGroupFileInfo db User {userId} GroupInfo {groupId} = |] (userId, groupId) --- TODO delete -getGroupMaxItemTs :: DB.Connection -> User -> GroupInfo -> IO (Maybe UTCTime) -getGroupMaxItemTs db User {userId} GroupInfo {groupId} = - fmap join . maybeFirstRow fromOnly $ - DB.query db "SELECT MAX(item_ts) FROM chat_items WHERE user_id = ? AND group_id = ?" (userId, groupId) - deleteGroupCIs :: DB.Connection -> User -> GroupInfo -> IO () deleteGroupCIs db User {userId} GroupInfo {groupId} = do DB.execute db "DELETE FROM messages WHERE group_id = ?" (Only groupId) DB.execute db "DELETE FROM chat_items WHERE user_id = ? AND group_id = ?" (userId, groupId) --- TODO delete -updateGroupTs :: DB.Connection -> User -> GroupInfo -> UTCTime -> IO () -updateGroupTs db User {userId} GroupInfo {groupId} updatedAt = - DB.execute - db - "UPDATE groups SET chat_ts = ? WHERE user_id = ? AND group_id = ?" - (updatedAt, userId, groupId) - createNewSndMessage :: MsgEncodingI e => DB.Connection -> TVar ChaChaDRG -> ConnOrGroupId -> (SharedMsgId -> NewMessage e) -> ExceptT StoreError IO SndMessage createNewSndMessage db gVar connOrGroupId mkMessage = createWithRandomId gVar $ \sharedMsgId -> do @@ -4791,12 +4757,6 @@ deleteContactExpiredCIs db user@User {userId} ct@Contact {contactId} expirationD DB.execute db "DELETE FROM messages WHERE connection_id = ? AND created_at <= ?" (connId, expirationDate) DB.execute db "DELETE FROM chat_items WHERE user_id = ? AND contact_id = ? AND created_at <= ?" (userId, contactId, expirationDate) --- TODO delete -getContactCICount :: DB.Connection -> User -> Contact -> IO (Maybe Int64) -getContactCICount db User {userId} Contact {contactId} = - fmap join . maybeFirstRow fromOnly $ - DB.query db "SELECT COUNT(1) FROM chat_items WHERE user_id = ? AND contact_id = ?" (userId, contactId) - getGroupExpiredFileInfo :: DB.Connection -> User -> GroupInfo -> UTCTime -> UTCTime -> IO [CIFileInfo] getGroupExpiredFileInfo db User {userId} GroupInfo {groupId} expirationDate createdAtCutoff = map toFileInfo @@ -4815,12 +4775,6 @@ deleteGroupExpiredCIs db User {userId} GroupInfo {groupId} expirationDate create DB.execute db "DELETE FROM messages WHERE group_id = ? AND created_at <= ?" (groupId, min expirationDate createdAtCutoff) DB.execute db "DELETE FROM chat_items WHERE user_id = ? AND group_id = ? AND item_ts <= ? AND created_at <= ?" (userId, groupId, expirationDate, createdAtCutoff) --- TODO delete -getGroupCICount :: DB.Connection -> User -> GroupInfo -> IO (Maybe Int64) -getGroupCICount db User {userId} GroupInfo {groupId} = - fmap join . maybeFirstRow fromOnly $ - DB.query db "SELECT COUNT(1) FROM chat_items WHERE user_id = ? AND group_id = ?" (userId, groupId) - -- | Saves unique local display name based on passed displayName, suffixed with _N if required. -- This function should be called inside transaction. withLocalDisplayName :: forall a. DB.Connection -> UserId -> Text -> (Text -> IO (Either StoreError a)) -> IO (Either StoreError a)