From 2f39cfd86f9486e391ffd2e189fa4ffc9e1fd25a Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Tue, 17 Jan 2023 13:08:51 +0400 Subject: [PATCH] core: support marking chat items read for any user (#1784) --- src/Simplex/Chat.hs | 8 +++++--- src/Simplex/Chat/Store.hs | 7 +++++++ tests/ChatTests.hs | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index 73cbacc30b..8a1cec3958 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -544,8 +544,9 @@ processChatCommand = \case (CIDMBroadcast, _, _) -> throwChatError CEInvalidChatItemDelete CTContactRequest -> pure $ chatCmdError (Just user) "not supported" CTContactConnection -> pure $ chatCmdError (Just user) "not supported" - APIChatRead (ChatRef cType chatId) fromToIds -> withUser $ \user@User {userId} -> case cType of + APIChatRead (ChatRef cType chatId) fromToIds -> withUser $ \_ -> case cType of CTDirect -> do + user <- withStore $ \db -> getUserByContactId db chatId timedItems <- withStore' $ \db -> getDirectUnreadTimedItems db user chatId fromToIds ts <- liftIO getCurrentTime forM_ timedItems $ \(itemId, ttl) -> do @@ -555,6 +556,7 @@ processChatCommand = \case withStore' $ \db -> updateDirectChatItemsRead db user chatId fromToIds pure $ CRCmdOk (Just user) CTGroup -> do + user@User {userId} <- withStore $ \db -> getUserByGroupId db chatId timedItems <- withStore' $ \db -> getGroupUnreadTimedItems db user chatId fromToIds ts <- liftIO getCurrentTime forM_ timedItems $ \(itemId, ttl) -> do @@ -563,8 +565,8 @@ processChatCommand = \case startProximateTimedItemThread user (ChatRef CTGroup chatId, itemId) deleteAt withStore' $ \db -> updateGroupChatItemsRead db userId chatId fromToIds pure $ CRCmdOk (Just user) - CTContactRequest -> pure $ chatCmdError (Just user) "not supported" - CTContactConnection -> pure $ chatCmdError (Just user) "not supported" + CTContactRequest -> pure $ chatCmdError Nothing "not supported" + CTContactConnection -> pure $ chatCmdError Nothing "not supported" APIChatUnread (ChatRef cType chatId) unreadChat -> withUser $ \user -> case cType of CTDirect -> do withStore $ \db -> do diff --git a/src/Simplex/Chat/Store.hs b/src/Simplex/Chat/Store.hs index 78de28f5cf..4af9febbf5 100644 --- a/src/Simplex/Chat/Store.hs +++ b/src/Simplex/Chat/Store.hs @@ -33,6 +33,7 @@ module Simplex.Chat.Store getUserIdByName, getUserByAConnId, getUserByContactId, + getUserByGroupId, createDirectConnection, createConnReqConnection, getProfileById, @@ -512,6 +513,11 @@ getUserByContactId db contactId = ExceptT . firstRow toUser (SEUserNotFoundByContactId contactId) $ DB.query db (userQuery <> " JOIN contacts ct ON ct.user_id = u.user_id WHERE ct.contact_id = ?") (Only contactId) +getUserByGroupId :: DB.Connection -> GroupId -> ExceptT StoreError IO User +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) + getUserByFileId :: DB.Connection -> FileTransferId -> ExceptT StoreError IO User getUserByFileId db fileId = ExceptT . firstRow toUser (SEUserNotFoundByFileId fileId) $ @@ -4879,6 +4885,7 @@ data StoreError | SEUserNotFound {userId :: UserId} | SEUserNotFoundByName {contactName :: ContactName} | SEUserNotFoundByContactId {contactId :: ContactId} + | SEUserNotFoundByGroupId {groupId :: GroupId} | SEUserNotFoundByFileId {fileId :: FileTransferId} | SEContactNotFound {contactId :: ContactId} | SEContactNotFoundByName {contactName :: ContactName} diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index 2716bd90fb..aba627a458 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -4425,7 +4425,7 @@ testCreateSecondUser = showActiveUser alice "alice (Alice)" alice ##> "/users" - alice <## "alice (Alice) (active) (unread: 1)" + alice <## "alice (Alice) (active)" alice <## "alisa" alice <##> bob