From abc96972dc126c42fcae9d3327e7239a5074466d Mon Sep 17 00:00:00 2001 From: IC Rainbow Date: Fri, 22 Dec 2023 19:54:13 +0200 Subject: [PATCH] add note deletion (api-only) --- src/Simplex/Chat.hs | 12 +++++++++++- src/Simplex/Chat/Store/Messages.hs | 20 ++++++++++++++++++++ tests/ChatTests/Local.hs | 4 ++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 4a7756400e..0f49507e03 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -894,7 +894,9 @@ processChatCommand = \case (SndMessage {msgId}, _) <- sendGroupMessage user gInfo ms $ XMsgDel itemSharedMId Nothing delGroupChatItem user gInfo ci msgId Nothing (CIDMBroadcast, _, _, _) -> throwChatError CEInvalidChatItemDelete - CTLocal -> pure $ chatCmdError (Just user) "TODO: APIDeleteChatItem.Local" + CTLocal -> do + (nf, CChatItem _ ci) <- withStore $ \db -> (,) <$> getNoteFolder db user chatId <*> getLocalChatItem db user chatId itemId + deleteLocalCI user nf ci True False CTContactRequest -> pure $ chatCmdError (Just user) "not supported" CTContactConnection -> pure $ chatCmdError (Just user) "not supported" APIDeleteMemberChatItem gId mId itemId -> withUser $ \user -> withChatLock "deleteChatItem" $ do @@ -5726,6 +5728,12 @@ deleteGroupCI user gInfo ci@ChatItem {file} byUser timed byGroupMember_ deletedT where gItem = AChatItem SCTGroup msgDirection (GroupChat gInfo) +deleteLocalCI :: (ChatMonad m, MsgDirectionI d) => User -> NoteFolder -> ChatItem 'CTLocal d -> Bool -> Bool -> m ChatResponse +deleteLocalCI user nf ci@ChatItem {file} byUser timed = do + deleteCIFile user file + withStoreCtx' (Just "deleteLocalCI, deleteLocalChatItem") $ \db -> deleteLocalChatItem db user nf ci + pure $ CRChatItemDeleted user (AChatItem SCTLocal msgDirection (LocalChat nf) ci) Nothing byUser timed + deleteCIFile :: (ChatMonad m, MsgDirectionI d) => User -> Maybe (CIFile d) -> m () deleteCIFile user file_ = forM_ file_ $ \file -> do @@ -6145,7 +6153,9 @@ chatCommandP = ("/remove " <|> "/rm ") *> char_ '#' *> (RemoveMember <$> displayName <* A.space <* char_ '@' <*> displayName), ("/leave " <|> "/l ") *> char_ '#' *> (LeaveGroup <$> displayName), ("/delete #" <|> "/d #") *> (DeleteGroup <$> displayName), + -- TODO: ("/delete $" <|> "/d $") *> (DeleteNoteFolder <$> displayName), ("/delete " <|> "/d ") *> char_ '@' *> (DeleteContact <$> displayName), + -- TODO: "/clear $" *> (ClearNoteFolder <$> displayName), "/clear #" *> (ClearGroup <$> displayName), "/clear " *> char_ '@' *> (ClearContact <$> displayName), ("/members " <|> "/ms ") *> char_ '#' *> (ListMembers <$> displayName), diff --git a/src/Simplex/Chat/Store/Messages.hs b/src/Simplex/Chat/Store/Messages.hs index 7dd541150b..2004ab2388 100644 --- a/src/Simplex/Chat/Store/Messages.hs +++ b/src/Simplex/Chat/Store/Messages.hs @@ -53,6 +53,7 @@ module Simplex.Chat.Store.Messages updateGroupChatItemModerated, markGroupChatItemDeleted, markGroupChatItemBlocked, + deleteLocalChatItem, updateDirectChatItemsRead, getDirectUnreadTimedItems, setDirectChatItemDeleteAt, @@ -78,6 +79,7 @@ module Simplex.Chat.Store.Messages getGroupMemberCIBySharedMsgId, getGroupChatItemByAgentMsgId, getGroupMemberChatItemLast, + getLocalChatItem, getDirectChatItemIdByText, getDirectChatItemIdByText', getGroupChatItemIdByText, @@ -1901,6 +1903,19 @@ getLocalChatItem db User {userId} folderId itemId = ExceptT $ do |] (userId, folderId, itemId) +deleteLocalChatItem :: DB.Connection -> User -> NoteFolder -> ChatItem 'CTLocal d -> IO () +deleteLocalChatItem db User {userId} NoteFolder {noteFolderId} ci = do + let itemId = chatItemId' ci + deleteChatItemVersions_ db itemId + deleteLocalCIReactions_ db noteFolderId ci + DB.execute + db + [sql| + DELETE FROM chat_items + WHERE user_id = ? AND note_folder_id = ? AND chat_item_id = ? + |] + (userId, noteFolderId, itemId) + getChatItemByFileId :: DB.Connection -> User -> Int64 -> ExceptT StoreError IO AChatItem getChatItemByFileId db user@User {userId} fileId = do (chatRef, itemId) <- @@ -2055,6 +2070,11 @@ deleteGroupCIReactions_ db g@GroupInfo {groupId} ci@ChatItem {meta = CIMeta {ite "DELETE FROM chat_item_reactions WHERE group_id = ? AND shared_msg_id = ? AND item_member_id = ?" (groupId, itemSharedMId, memberId) +deleteLocalCIReactions_ :: DB.Connection -> NoteFolderId -> ChatItem 'CTLocal d -> IO () +deleteLocalCIReactions_ db folderId ChatItem {meta = CIMeta {itemSharedMsgId}} = + forM_ itemSharedMsgId $ \itemSharedMId -> + DB.execute db "DELETE FROM chat_item_reactions WHERE note_folder_id = ? AND shared_msg_id = ?" (folderId, itemSharedMId) + toCIReaction :: (MsgReaction, Bool, Int) -> CIReactionCount toCIReaction (reaction, userReacted, totalReacted) = CIReactionCount {reaction, userReacted, totalReacted} diff --git a/tests/ChatTests/Local.hs b/tests/ChatTests/Local.hs index 7c8d118b5c..b185b21aee 100644 --- a/tests/ChatTests/Local.hs +++ b/tests/ChatTests/Local.hs @@ -22,6 +22,10 @@ testNotes tmp = withNewTestChat tmp "alice" aliceProfile $ \alice -> do alice ##> "/tail" alice <# "$self keep in mind" + alice ##> "/_delete item $1 1 internal" + alice <## "message deleted" + alice ##> "/tail" + testUserNotes :: FilePath -> IO () testUserNotes tmp = withNewTestChat tmp "alice" aliceProfile $ \alice -> do alice ##> "/note folder self"