From d0429f51c49cc0ff25b5639af5a038f2e396a769 Mon Sep 17 00:00:00 2001 From: IC Rainbow Date: Tue, 26 Dec 2023 15:17:41 +0200 Subject: [PATCH] add read/unread --- src/Simplex/Chat.hs | 11 +++++++++-- src/Simplex/Chat/Store/Direct.hs | 1 + src/Simplex/Chat/Store/Messages.hs | 22 ++++++++++++++++++++++ src/Simplex/Chat/Store/NoteFolders.hs | 5 +++++ src/Simplex/Chat/Store/Profiles.hs | 6 ++++++ tests/ChatTests/Local.hs | 4 ++++ 6 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 68a7953dc9..246c6a0fe7 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -971,7 +971,10 @@ processChatCommand' vr = \case startProximateTimedItemThread user (ChatRef CTGroup chatId, itemId) deleteAt withStore' $ \db -> updateGroupChatItemsRead db userId chatId fromToIds ok user - CTLocal -> error "TODO: APIChatRead.CTLocal" + CTLocal -> do + user <- withStore $ \db -> getUserByNoteFolderId db chatId + withStore' $ \db -> updateLocalChatItemsRead db user chatId fromToIds + ok user CTContactRequest -> pure $ chatCmdError Nothing "not supported" CTContactConnection -> pure $ chatCmdError Nothing "not supported" APIChatUnread (ChatRef cType chatId) unreadChat -> withUser $ \user -> case cType of @@ -985,7 +988,11 @@ processChatCommand' vr = \case Group {groupInfo} <- getGroup db vr user chatId liftIO $ updateGroupUnreadChat db user groupInfo unreadChat ok user - CTLocal -> error "APIChatUnread: CTLocal" + CTLocal -> do + withStore $ \db -> do + nf <- getNoteFolder db user chatId + liftIO $ updateNoteFolderUnreadChat db user nf unreadChat + ok user _ -> pure $ chatCmdError (Just user) "not supported" APIDeleteChat (ChatRef cType chatId) notify -> withUser $ \user@User {userId} -> case cType of CTDirect -> do diff --git a/src/Simplex/Chat/Store/Direct.hs b/src/Simplex/Chat/Store/Direct.hs index 7504f19c95..b6e542ef81 100644 --- a/src/Simplex/Chat/Store/Direct.hs +++ b/src/Simplex/Chat/Store/Direct.hs @@ -399,6 +399,7 @@ setUserChatsRead db User {userId} = do updatedAt <- getCurrentTime DB.execute db "UPDATE contacts SET unread_chat = ?, updated_at = ? WHERE user_id = ? AND unread_chat = ?" (False, updatedAt, userId, True) DB.execute db "UPDATE groups SET unread_chat = ?, updated_at = ? WHERE user_id = ? AND unread_chat = ?" (False, updatedAt, userId, True) + DB.execute db "UPDATE note_folders SET unread_chat = ?, updated_at = ? WHERE user_id = ? AND unread_chat = ?" (False, updatedAt, userId, True) DB.execute db "UPDATE chat_items SET item_status = ?, updated_at = ? WHERE user_id = ? AND item_status = ?" (CISRcvRead, updatedAt, userId, CISRcvNew) updateContactStatus :: DB.Connection -> User -> Contact -> ContactStatus -> IO Contact diff --git a/src/Simplex/Chat/Store/Messages.hs b/src/Simplex/Chat/Store/Messages.hs index 71b014628c..cdbe98a0e6 100644 --- a/src/Simplex/Chat/Store/Messages.hs +++ b/src/Simplex/Chat/Store/Messages.hs @@ -61,6 +61,7 @@ module Simplex.Chat.Store.Messages updateGroupChatItemsRead, getGroupUnreadTimedItems, setGroupChatItemDeleteAt, + updateLocalChatItemsRead, getChatRefViaItemId, getChatItemVersions, getDirectCIReactions, @@ -1299,6 +1300,27 @@ setGroupChatItemDeleteAt db User {userId} groupId chatItemId deleteAt = "UPDATE chat_items SET timed_delete_at = ? WHERE user_id = ? AND group_id = ? AND chat_item_id = ?" (deleteAt, userId, groupId, chatItemId) +updateLocalChatItemsRead :: DB.Connection -> User -> NoteFolderId -> Maybe (ChatItemId, ChatItemId) -> IO () +updateLocalChatItemsRead db User {userId} noteFolderId itemsRange_ = do + currentTs <- getCurrentTime + case itemsRange_ of + Just (fromItemId, toItemId) -> + DB.execute + db + [sql| + UPDATE chat_items SET item_status = ?, updated_at = ? + WHERE user_id = ? AND note_folder_id = ? AND chat_item_id >= ? AND chat_item_id <= ? AND item_status = ? + |] + (CISRcvRead, currentTs, userId, noteFolderId, fromItemId, toItemId, CISRcvNew) + _ -> + DB.execute + db + [sql| + UPDATE chat_items SET item_status = ?, updated_at = ? + WHERE user_id = ? AND note_folder_id = ? AND item_status = ? + |] + (CISRcvRead, currentTs, userId, noteFolderId, CISRcvNew) + type MaybeCIFIleRow = (Maybe Int64, Maybe String, Maybe Integer, Maybe FilePath, Maybe C.SbKey, Maybe C.CbNonce, Maybe ACIFileStatus, Maybe FileProtocol) type ChatItemModeRow = (Maybe Int, Maybe UTCTime, Maybe Bool) diff --git a/src/Simplex/Chat/Store/NoteFolders.hs b/src/Simplex/Chat/Store/NoteFolders.hs index 86151751df..751d4d1677 100644 --- a/src/Simplex/Chat/Store/NoteFolders.hs +++ b/src/Simplex/Chat/Store/NoteFolders.hs @@ -69,6 +69,11 @@ getNoteFolder db User {userId} noteFolderId = toNoteFolder (displayName, localDisplayName, createdAt, updatedAt, chatTs, favorite, unread) = NoteFolder {noteFolderId, userId, displayName, localDisplayName, createdAt, updatedAt, chatTs, favorite, unread} +updateNoteFolderUnreadChat :: DB.Connection -> User -> NoteFolder -> Bool -> IO () +updateNoteFolderUnreadChat db User {userId} NoteFolder {noteFolderId} unreadChat = do + updatedAt <- getCurrentTime + DB.execute db [sql| UPDATE note_folders SET unread_chat = ?, updated_at = ? WHERE user_id = ? AND note_folder_id = ? |] (unreadChat, updatedAt, userId, noteFolderId) + deleteNoteFolderFiles :: DB.Connection -> UserId -> NoteFolder -> IO () deleteNoteFolderFiles db userId NoteFolder {noteFolderId} = do DB.execute diff --git a/src/Simplex/Chat/Store/Profiles.hs b/src/Simplex/Chat/Store/Profiles.hs index ce1d17859a..5e5dbd907e 100644 --- a/src/Simplex/Chat/Store/Profiles.hs +++ b/src/Simplex/Chat/Store/Profiles.hs @@ -27,6 +27,7 @@ module Simplex.Chat.Store.Profiles getUserByARcvFileId, getUserByContactId, getUserByGroupId, + getUserByNoteFolderId, getUserByFileId, getUserFileInfo, deleteUserRecord, @@ -200,6 +201,11 @@ getUserByGroupId db groupId = ExceptT . firstRow toUser (SEUserNotFoundByGroupId groupId) $ DB.query db (userQuery <> " JOIN groups g ON g.user_id = u.user_id WHERE g.group_id = ?") (Only groupId) +getUserByNoteFolderId :: DB.Connection -> NoteFolderId -> ExceptT StoreError IO User +getUserByNoteFolderId db contactId = + ExceptT . firstRow toUser (SEUserNotFoundByContactId contactId) $ + DB.query db (userQuery <> " JOIN note_folders nf ON nf.user_id = u.user_id WHERE nf.note_folder_id = ?") (Only contactId) + getUserByFileId :: DB.Connection -> FileTransferId -> ExceptT StoreError IO User getUserByFileId db fileId = ExceptT . firstRow toUser (SEUserNotFoundByFileId fileId) $ diff --git a/tests/ChatTests/Local.hs b/tests/ChatTests/Local.hs index 9897393685..5e5a2bb7ab 100644 --- a/tests/ChatTests/Local.hs +++ b/tests/ChatTests/Local.hs @@ -33,6 +33,10 @@ testNotes tmp = withNewTestChat tmp "alice" aliceProfile $ \alice -> do alice ##> "/_reaction $1 1 on {\"type\":\"emoji\",\"emoji\":\"🚀\"}" alice <## "added 🚀" + alice #$> ("/_read chat $1 from=1 to=100", id, "ok") + alice ##> "/_unread chat $1 on" + alice <## "ok" + alice ##> "/_delete item $1 1 internal" alice <## "message deleted" alice ##> "/tail"