From 3907886991bd12678165ff282871d6b01329fb8c Mon Sep 17 00:00:00 2001 From: IC Rainbow Date: Tue, 26 Dec 2023 16:28:36 +0200 Subject: [PATCH] add note updates --- src/Simplex/Chat.hs | 18 ++++++++++++++++-- src/Simplex/Chat/Store/Messages.hs | 24 ++++++++++++++++++++++++ tests/ChatTests/Local.hs | 5 +++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 246c6a0fe7..ffbe2748bd 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -848,7 +848,21 @@ processChatCommand' vr = \case else pure $ CRChatItemNotChanged user (AChatItem SCTGroup SMDSnd (GroupChat gInfo) ci) _ -> throwChatError CEInvalidChatItemUpdate CChatItem SMDRcv _ -> throwChatError CEInvalidChatItemUpdate - CTLocal -> pure $ chatCmdError (Just user) "TODO: APIUpdateChatItem.CTLocal" + CTLocal -> do + (nf@NoteFolder {noteFolderId}, cci) <- withStore $ \db -> (,) <$> getNoteFolder db user chatId <*> getLocalChatItem db user chatId itemId + case cci of + CChatItem SMDRcv _ -> throwChatError CEInvalidChatItemUpdate + CChatItem SMDSnd ci@ChatItem {meta = CIMeta {editable}, content = ciContent} -> do + case (ciContent, editable) of + (CISndMsgContent oldMC, True) -> + if mc /= oldMC + then withStore' $ \db -> do + currentTs <- getCurrentTime + addInitialAndNewCIVersions db itemId (chatItemTs' ci, oldMC) (currentTs, mc) + ci' <- updateLocalChatItem' db user noteFolderId ci (CISndMsgContent mc) + pure $ CRChatItemUpdated user (AChatItem SCTLocal SMDSnd (LocalChat nf) ci') + else pure $ CRChatItemNotChanged user (AChatItem SCTLocal SMDSnd (LocalChat nf) ci) + _ -> throwChatError CEInvalidChatItemUpdate CTContactRequest -> pure $ chatCmdError (Just user) "not supported" CTContactConnection -> pure $ chatCmdError (Just user) "not supported" APIDeleteChatItem (ChatRef cType chatId) itemId mode -> withUser $ \user -> withChatLock "deleteChatItem" $ case cType of @@ -6125,7 +6139,7 @@ createLocalChatItem user cd content itemTs_ = do when (ciRequiresAttention content) . liftIO $ updateChatTs db user cd createdAt createWithRandomId gVar $ \sharedMsgId -> let smi_ = Just (SharedMsgId sharedMsgId) - in createNewChatItem_ db user cd Nothing smi_ content (Nothing, Nothing, Nothing, Nothing, Nothing) Nothing False itemTs Nothing createdAt + in createNewChatItem_ db user cd Nothing smi_ content (Nothing, Nothing, Nothing, Nothing, Nothing) Nothing False itemTs Nothing createdAt liftIO $ mkChatItem cd ciId content Nothing Nothing Nothing Nothing False itemTs Nothing createdAt getCreateActiveUser :: SQLiteStore -> Bool -> IO User diff --git a/src/Simplex/Chat/Store/Messages.hs b/src/Simplex/Chat/Store/Messages.hs index cdbe98a0e6..eb87b4e5a6 100644 --- a/src/Simplex/Chat/Store/Messages.hs +++ b/src/Simplex/Chat/Store/Messages.hs @@ -85,6 +85,7 @@ module Simplex.Chat.Store.Messages getGroupChatItemByAgentMsgId, getGroupMemberChatItemLast, getLocalChatItem, + updateLocalChatItem', getDirectChatItemIdByText, getDirectChatItemIdByText', getGroupChatItemIdByText, @@ -2015,6 +2016,29 @@ getLocalChatItemIdByText' db User {userId} noteFolderId msg = |] (userId, noteFolderId, msg <> "%") +updateLocalChatItem' :: forall d. MsgDirectionI d => DB.Connection -> User -> NoteFolderId -> ChatItem 'CTLocal d -> CIContent d -> IO (ChatItem 'CTLocal d) +updateLocalChatItem' db User {userId} noteFolderId ci newContent = do + currentTs <- liftIO getCurrentTime + let ci' = updatedChatItem ci newContent False currentTs + liftIO $ updateLocalChatItem_ db userId noteFolderId ci' + pure ci' + +-- this function assumes that direct item with correct chat direction already exists, +-- it should be checked before calling it +updateLocalChatItem_ :: forall d. MsgDirectionI d => DB.Connection -> UserId -> NoteFolderId -> ChatItem 'CTLocal d -> IO () +updateLocalChatItem_ db userId noteFolderId ChatItem {meta, content} = do + let CIMeta {itemId, itemText, itemStatus, itemDeleted, itemEdited, updatedAt} = meta + itemDeleted' = isJust itemDeleted + itemDeletedTs' = itemDeletedTs =<< itemDeleted + DB.execute + db + [sql| + UPDATE chat_items + SET item_content = ?, item_text = ?, item_status = ?, item_deleted = ?, item_deleted_ts = ?, item_edited = ?, updated_at = ? + WHERE user_id = ? AND note_folder_id = ? AND chat_item_id = ? + |] + ((content, itemText, itemStatus, itemDeleted', itemDeletedTs', itemEdited, updatedAt) :. (userId, noteFolderId, itemId)) + deleteLocalChatItem :: DB.Connection -> User -> NoteFolder -> ChatItem 'CTLocal d -> IO () deleteLocalChatItem db User {userId} NoteFolder {noteFolderId} ci = do let itemId = chatItemId' ci diff --git a/tests/ChatTests/Local.hs b/tests/ChatTests/Local.hs index 5e5a2bb7ab..87ad3187ff 100644 --- a/tests/ChatTests/Local.hs +++ b/tests/ChatTests/Local.hs @@ -42,6 +42,11 @@ testNotes tmp = withNewTestChat tmp "alice" aliceProfile $ \alice -> do alice ##> "/tail" alice ##> "/chats" + alice #> "$self ahoy!" + alice ##> "/_update item $1 1 text Greetings." + alice ##> "/tail $self" + alice <# "$self Greetings." + alice ##> "/delete $self" alice <## "note folder self deleted"