Skip to content

Commit

Permalink
core: cleanup obsolete chat item deletion code (see #1625) (#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
spaced4ndy committed Jan 17, 2023
1 parent e8cab01 commit a668bd5
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 80 deletions.
36 changes: 2 additions & 34 deletions src/Simplex/Chat.hs
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
46 changes: 0 additions & 46 deletions src/Simplex/Chat/Store.hs
Expand Up @@ -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,
Expand Down Expand Up @@ -243,10 +239,8 @@ module Simplex.Chat.Store
setChatItemTTL,
getContactExpiredFileInfo,
deleteContactExpiredCIs,
getContactCICount, -- TODO delete
getGroupExpiredFileInfo,
deleteGroupExpiredCIs,
getGroupCICount, -- TODO delete
getPendingContactConnection,
deletePendingContactConnection,
updateContactSettings,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit a668bd5

Please sign in to comment.