diff --git a/src/Simplex/Chat.hs b/src/Simplex/Chat.hs index bbc8000c28..bc8716891a 100644 --- a/src/Simplex/Chat.hs +++ b/src/Simplex/Chat.hs @@ -2122,7 +2122,7 @@ chatCommandP = <|> "/_send " *> (APISendMessage <$> chatRefP <*> (" json " *> jsonP <|> " text " *> (ComposedMessage Nothing Nothing <$> mcTextP))) <|> "/_update item " *> (APIUpdateChatItem <$> chatRefP <* A.space <*> A.decimal <* A.space <*> msgContentP) <|> "/_delete item " *> (APIDeleteChatItem <$> chatRefP <* A.space <*> A.decimal <* A.space <*> ciDeleteMode) - <|> "/_read chat " *> (APIChatRead <$> chatRefP <* A.space <*> ((,) <$> ("from=" *> A.decimal) <* A.space <*> ("to=" *> A.decimal))) + <|> "/_read chat " *> (APIChatRead <$> chatRefP <*> optional (A.space *> ((,) <$> ("from=" *> A.decimal) <* A.space <*> ("to=" *> A.decimal)))) <|> "/_delete " *> (APIDeleteChat <$> chatRefP) <|> "/_accept " *> (APIAcceptContact <$> A.decimal) <|> "/_reject " *> (APIRejectContact <$> A.decimal) diff --git a/src/Simplex/Chat/Controller.hs b/src/Simplex/Chat/Controller.hs index 9eca5c606a..1de9da5371 100644 --- a/src/Simplex/Chat/Controller.hs +++ b/src/Simplex/Chat/Controller.hs @@ -107,7 +107,7 @@ data ChatCommand | APISendMessage ChatRef ComposedMessage | APIUpdateChatItem ChatRef ChatItemId MsgContent | APIDeleteChatItem ChatRef ChatItemId CIDeleteMode - | APIChatRead ChatRef (ChatItemId, ChatItemId) + | APIChatRead ChatRef (Maybe (ChatItemId, ChatItemId)) | APIDeleteChat ChatRef | APIAcceptContact Int64 | APIRejectContact Int64 diff --git a/src/Simplex/Chat/Store.hs b/src/Simplex/Chat/Store.hs index c08406646a..f8a0cebc02 100644 --- a/src/Simplex/Chat/Store.hs +++ b/src/Simplex/Chat/Store.hs @@ -3562,29 +3562,49 @@ toChatItemRef = \case (itemId, Nothing, Just groupId) -> Right (itemId, ChatRef CTGroup groupId) (itemId, _, _) -> Left $ SEBadChatItem itemId -updateDirectChatItemsRead :: (StoreMonad m) => SQLiteStore -> Int64 -> (ChatItemId, ChatItemId) -> m () -updateDirectChatItemsRead st contactId (fromItemId, toItemId) = do +updateDirectChatItemsRead :: (StoreMonad m) => SQLiteStore -> Int64 -> Maybe (ChatItemId, ChatItemId) -> m () +updateDirectChatItemsRead st contactId itemsRange_ = do currentTs <- liftIO getCurrentTime liftIO . withTransaction st $ \db -> - DB.execute - db - [sql| - UPDATE chat_items SET item_status = ?, updated_at = ? - WHERE contact_id = ? AND chat_item_id >= ? AND chat_item_id <= ? AND item_status = ? - |] - (CISRcvRead, currentTs, contactId, fromItemId, toItemId, CISRcvNew) + case itemsRange_ of + Just (fromItemId, toItemId) -> + DB.execute + db + [sql| + UPDATE chat_items SET item_status = ?, updated_at = ? + WHERE contact_id = ? AND chat_item_id >= ? AND chat_item_id <= ? AND item_status = ? + |] + (CISRcvRead, currentTs, contactId, fromItemId, toItemId, CISRcvNew) + _ -> + DB.execute + db + [sql| + UPDATE chat_items SET item_status = ?, updated_at = ? + WHERE contact_id = ? AND item_status = ? + |] + (CISRcvRead, currentTs, contactId, CISRcvNew) -updateGroupChatItemsRead :: (StoreMonad m) => SQLiteStore -> Int64 -> (ChatItemId, ChatItemId) -> m () -updateGroupChatItemsRead st groupId (fromItemId, toItemId) = do +updateGroupChatItemsRead :: (StoreMonad m) => SQLiteStore -> Int64 -> Maybe (ChatItemId, ChatItemId) -> m () +updateGroupChatItemsRead st groupId itemsRange_ = do currentTs <- liftIO getCurrentTime liftIO . withTransaction st $ \db -> - DB.execute - db - [sql| - UPDATE chat_items SET item_status = ?, updated_at = ? - WHERE group_id = ? AND chat_item_id >= ? AND chat_item_id <= ? AND item_status = ? - |] - (CISRcvRead, currentTs, groupId, fromItemId, toItemId, CISRcvNew) + case itemsRange_ of + Just (fromItemId, toItemId) -> + DB.execute + db + [sql| + UPDATE chat_items SET item_status = ?, updated_at = ? + WHERE group_id = ? AND chat_item_id >= ? AND chat_item_id <= ? AND item_status = ? + |] + (CISRcvRead, currentTs, groupId, fromItemId, toItemId, CISRcvNew) + _ -> + DB.execute + db + [sql| + UPDATE chat_items SET item_status = ?, updated_at = ? + WHERE group_id = ? AND item_status = ? + |] + (CISRcvRead, currentTs, groupId, CISRcvNew) type ChatStatsRow = (Int, ChatItemId) diff --git a/tests/ChatTests.hs b/tests/ChatTests.hs index 9a6f0b7be8..f3ad49831c 100644 --- a/tests/ChatTests.hs +++ b/tests/ChatTests.hs @@ -152,6 +152,8 @@ testAddContact = -- read messages alice #$> ("/_read chat @2 from=1 to=100", id, "ok") bob #$> ("/_read chat @2 from=1 to=100", id, "ok") + alice #$> ("/_read chat @2", id, "ok") + bob #$> ("/_read chat @2", id, "ok") testDirectMessageQuotedReply :: IO () testDirectMessageQuotedReply = @@ -383,6 +385,9 @@ testGroup = alice #$> ("/_read chat #1 from=1 to=100", id, "ok") bob #$> ("/_read chat #1 from=1 to=100", id, "ok") cath #$> ("/_read chat #1 from=1 to=100", id, "ok") + alice #$> ("/_read chat #1", id, "ok") + bob #$> ("/_read chat #1", id, "ok") + cath #$> ("/_read chat #1", id, "ok") testGroup2 :: IO () testGroup2 =