add note updates

This commit is contained in:
IC Rainbow
2023-12-26 16:28:36 +02:00
parent d0429f51c4
commit 3907886991
3 changed files with 45 additions and 2 deletions
+16 -2
View File
@@ -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
+24
View File
@@ -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
+5
View File
@@ -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"