From a70d155ce4684e922b1609e7df30d128b7205dc5 Mon Sep 17 00:00:00 2001
From: Benjamin Brahmer <info@b-brahmer.de>
Date: Mon, 3 Apr 2023 15:27:21 +0200
Subject: [PATCH] Set lastmodified in read all

if this is not updated the API won't work correctly and we get sync errors.
Adjust test to use item api

Signed-off-by: Benjamin Brahmer <info@b-brahmer.de>
---
 CHANGELOG.md                     |  1 +
 lib/Db/ItemMapperV2.php          |  5 ++++-
 tests/Unit/Db/ItemMapperTest.php | 10 +++++-----
 tests/api/items.bats             |  2 +-
 4 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1f8fda6cb..dbe54206c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@ The format is mostly based on [Keep a Changelog](https://keepachangelog.com/en/1
 ### Changed
 
 ### Fixed
+- Fix last_modified not updated when all items are marked as read #2183
 
 # Releases
 ## [21.2.0-beta1] - 2023-03-23
diff --git a/lib/Db/ItemMapperV2.php b/lib/Db/ItemMapperV2.php
index 704a8214d..d8b36723d 100644
--- a/lib/Db/ItemMapperV2.php
+++ b/lib/Db/ItemMapperV2.php
@@ -285,12 +285,15 @@ class ItemMapperV2 extends NewsMapperV2
             return intval($value['id']);
         }, $this->db->executeQuery($idBuilder->getSQL(), $idBuilder->getParameters())->fetchAll());
 
+        $time = new Time();
         $builder = $this->db->getQueryBuilder();
         $builder->update(self::TABLE_NAME)
             ->set('unread', $builder->createParameter('unread'))
+            ->set('last_modified', $builder->createParameter('last_modified'))
             ->andWhere('id IN (:idList)')
             ->setParameter('idList', $idList, IQueryBuilder::PARAM_INT_ARRAY)
-            ->setParameter('unread', false, IQueryBuilder::PARAM_BOOL);
+            ->setParameter('unread', false, IQueryBuilder::PARAM_BOOL)
+            ->setParameter('last_modified', $time->getMicroTime(), IQueryBuilder::PARAM_STR);
 
         return $this->db->executeStatement(
             $builder->getSQL(),
diff --git a/tests/Unit/Db/ItemMapperTest.php b/tests/Unit/Db/ItemMapperTest.php
index 3a4a026d6..80421bc0a 100644
--- a/tests/Unit/Db/ItemMapperTest.php
+++ b/tests/Unit/Db/ItemMapperTest.php
@@ -528,7 +528,7 @@ class ItemMapperTest extends MapperTestUtility
             ->with('SQL QUERY')
             ->willReturn($result);
 
-        $this->builder->expects($this->once())
+        $this->builder->expects($this->exactly(2))
             ->method('createParameter')
             ->will($this->returnArgument(0));
 
@@ -537,9 +537,9 @@ class ItemMapperTest extends MapperTestUtility
             ->with('news_items')
             ->will($this->returnSelf());
 
-        $this->builder->expects($this->once())
+        $this->builder->expects($this->exactly(2))
             ->method('set')
-            ->with('unread', 'unread')
+            ->withConsecutive(['unread', 'unread'], ['last_modified', 'last_modified'])
             ->will($this->returnSelf());
 
         $this->builder->expects($this->exactly(1))
@@ -547,9 +547,9 @@ class ItemMapperTest extends MapperTestUtility
             ->withConsecutive(['id IN (:idList)'])
             ->will($this->returnSelf());
 
-        $this->builder->expects($this->exactly(2))
+        $this->builder->expects($this->exactly(3))
             ->method('setParameter')
-            ->withConsecutive(['idList', [1, 2]], ['unread', false])
+            ->withConsecutive(['idList', [1, 2]], ['unread', false], ['last_modified'])
             ->will($this->returnSelf());
 
         $this->builder->expects($this->exactly(1))
diff --git a/tests/api/items.bats b/tests/api/items.bats
index d6896785f..4bb38bf75 100644
--- a/tests/api/items.bats
+++ b/tests/api/items.bats
@@ -67,7 +67,7 @@ teardown() {
   SYNC_TIME=$(date +%s)
 
   # mark all items of feed as read, returns nothing (other client marks items as read)
-  STATUS_CODE=$(http --ignore-stdin -hdo /tmp/body -a ${user}:${APP_PASSWORD} PUT ${BASE_URLv1}/feeds/$FEEDID/read newestItemId="$max" 2>&1| grep HTTP/)
+  STATUS_CODE=$(http --ignore-stdin -hdo /tmp/body -a ${user}:${APP_PASSWORD} PUT ${BASE_URLv1}/items/read newestItemId="$max" 2>&1| grep HTTP/)
 
   # client 2 checks for updates since last sync
   UPDATED_ITEMS=($(http --ignore-stdin -b -a ${user}:${APP_PASSWORD} GET ${BASE_URLv1}/items/updated id=$FEEDID lastModified=$SYNC_TIME | grep -Po '"id":\K([0-9]+)' | tr '\n' ' '))