Skip to content

Commit df48a6a

Browse files
committed
fix(dav): use absolute cutoff for sync token pruning
Signed-off-by: mooons <10822203+mooons@users.noreply.github.com>
1 parent d56ad83 commit df48a6a

4 files changed

Lines changed: 18 additions & 12 deletions

File tree

‎apps/dav/lib/CalDAV/CalDavBackend.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3619,11 +3619,13 @@ public function pruneOutdatedSyncTokens(int $keep, int $retention): int {
36193619
return 0;
36203620
}
36213621

3622+
$cutoff = max(0, time() - $retention);
3623+
36223624
$query = $this->db->getQueryBuilder();
36233625
$query->delete('calendarchanges')
36243626
->where(
36253627
$query->expr()->lte('id', $query->createNamedParameter($maxId - $keep, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
3626-
$query->expr()->lte('created_at', $query->createNamedParameter($retention)),
3628+
$query->expr()->lte('created_at', $query->createNamedParameter($cutoff, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
36273629
);
36283630
return $query->executeStatement();
36293631
}

‎apps/dav/lib/CardDAV/CardDavBackend.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,11 +1496,13 @@ public function pruneOutdatedSyncTokens(int $keep, int $retention): int {
14961496
return 0;
14971497
}
14981498

1499+
$cutoff = max(0, time() - $retention);
1500+
14991501
$query = $this->db->getQueryBuilder();
15001502
$query->delete('addressbookchanges')
15011503
->where(
15021504
$query->expr()->lte('id', $query->createNamedParameter($maxId - $keep, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
1503-
$query->expr()->lte('created_at', $query->createNamedParameter($retention)),
1505+
$query->expr()->lte('created_at', $query->createNamedParameter($cutoff, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
15041506
);
15051507
return $query->executeStatement();
15061508
}

‎apps/dav/tests/unit/CalDAV/CalDavBackendTest.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,11 +1398,12 @@ public function testPruneOutdatedSyncTokens(): void {
13981398
EOD;
13991399
$this->backend->updateCalendarObject($calendarId, $uri, $calData);
14001400

1401-
// Keep everything
1402-
$deleted = $this->backend->pruneOutdatedSyncTokens(0, 0);
1401+
// Keep everything by using a retention duration larger than the row age
1402+
$deleted = $this->backend->pruneOutdatedSyncTokens(0, time());
14031403
self::assertSame(0, $deleted);
14041404

1405-
$deleted = $this->backend->pruneOutdatedSyncTokens(0, time());
1405+
// A retention duration of 0 means everything older than "now" is eligible
1406+
$deleted = $this->backend->pruneOutdatedSyncTokens(0, 0);
14061407
// At least one from the object creation and one from the object update
14071408
$this->assertGreaterThanOrEqual(2, $deleted);
14081409
$changes = $this->backend->getChangesForCalendar($calendarId, $syncToken, 1);
@@ -1468,7 +1469,7 @@ public function testPruneOutdatedSyncTokens(): void {
14681469
$this->assertEmpty($changes['deleted']);
14691470

14701471
// Delete all but last change
1471-
$deleted = $this->backend->pruneOutdatedSyncTokens(1, time());
1472+
$deleted = $this->backend->pruneOutdatedSyncTokens(1, 0);
14721473
$this->assertEquals(1, $deleted); // We had two changes before, now one
14731474

14741475
// Only update should remain
@@ -1478,7 +1479,7 @@ public function testPruneOutdatedSyncTokens(): void {
14781479
$this->assertEmpty($changes['deleted']);
14791480

14801481
// Check that no crash occurs when prune is called without current changes
1481-
$deleted = $this->backend->pruneOutdatedSyncTokens(1, time());
1482+
$deleted = $this->backend->pruneOutdatedSyncTokens(1, 0);
14821483
self::assertSame(0, $deleted);
14831484
}
14841485

‎apps/dav/tests/unit/CardDAV/CardDavBackendTest.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -875,11 +875,12 @@ public function testPruneOutdatedSyncTokens(): void {
875875
$this->backend->createCard($addressBookId, $uri, $this->vcardTest0);
876876
$this->backend->updateCard($addressBookId, $uri, $this->vcardTest1);
877877

878-
// Do not delete anything if week data as old as ts=0
879-
$deleted = $this->backend->pruneOutdatedSyncTokens(0, 0);
878+
// Keep everything by using a retention duration larger than the row age
879+
$deleted = $this->backend->pruneOutdatedSyncTokens(0, time());
880880
self::assertSame(0, $deleted);
881881

882-
$deleted = $this->backend->pruneOutdatedSyncTokens(0, time());
882+
// A retention duration of 0 means everything older than "now" is eligible
883+
$deleted = $this->backend->pruneOutdatedSyncTokens(0, 0);
883884
// At least one from the object creation and one from the object update
884885
$this->assertGreaterThanOrEqual(2, $deleted);
885886
$changes = $this->backend->getChangesForAddressBook($addressBookId, $syncToken, 1);
@@ -911,7 +912,7 @@ public function testPruneOutdatedSyncTokens(): void {
911912
$this->assertEmpty($changes['deleted']);
912913

913914
// Delete all but last change
914-
$deleted = $this->backend->pruneOutdatedSyncTokens(1, time());
915+
$deleted = $this->backend->pruneOutdatedSyncTokens(1, 0);
915916
$this->assertEquals(1, $deleted); // We had two changes before, now one
916917

917918
// Only update should remain
@@ -921,6 +922,6 @@ public function testPruneOutdatedSyncTokens(): void {
921922
$this->assertEmpty($changes['deleted']);
922923

923924
// Check that no crash occurs when prune is called without current changes
924-
$deleted = $this->backend->pruneOutdatedSyncTokens(1, time());
925+
$deleted = $this->backend->pruneOutdatedSyncTokens(1, 0);
925926
}
926927
}

0 commit comments

Comments
 (0)