Skip to content

Commit 5b7534c

Browse files
ChristophWurstkesselb
authored andcommitted
fix(dav): Add occ command to fix missing caldav sync tokens
Signed-off-by: Christoph Wurst <[email protected]>
1 parent ebf9e0d commit 5b7534c

File tree

7 files changed

+250
-38
lines changed

7 files changed

+250
-38
lines changed

apps/dav/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<command>OCA\DAV\Command\CreateAddressBook</command>
5151
<command>OCA\DAV\Command\CreateCalendar</command>
5252
<command>OCA\DAV\Command\DeleteCalendar</command>
53+
<command>OCA\DAV\Command\FixCalendarSyncCommand</command>
5354
<command>OCA\DAV\Command\MoveCalendar</command>
5455
<command>OCA\DAV\Command\ListCalendars</command>
5556
<command>OCA\DAV\Command\RetentionCleanupCommand</command>

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
'OCA\\DAV\\Command\\CreateAddressBook' => $baseDir . '/../lib/Command/CreateAddressBook.php',
138138
'OCA\\DAV\\Command\\CreateCalendar' => $baseDir . '/../lib/Command/CreateCalendar.php',
139139
'OCA\\DAV\\Command\\DeleteCalendar' => $baseDir . '/../lib/Command/DeleteCalendar.php',
140+
'OCA\\DAV\\Command\\FixCalendarSyncCommand' => $baseDir . '/../lib/Command/FixCalendarSyncCommand.php',
140141
'OCA\\DAV\\Command\\ListCalendars' => $baseDir . '/../lib/Command/ListCalendars.php',
141142
'OCA\\DAV\\Command\\MoveCalendar' => $baseDir . '/../lib/Command/MoveCalendar.php',
142143
'OCA\\DAV\\Command\\RemoveInvalidShares' => $baseDir . '/../lib/Command/RemoveInvalidShares.php',

apps/dav/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class ComposerStaticInitDAV
152152
'OCA\\DAV\\Command\\CreateAddressBook' => __DIR__ . '/..' . '/../lib/Command/CreateAddressBook.php',
153153
'OCA\\DAV\\Command\\CreateCalendar' => __DIR__ . '/..' . '/../lib/Command/CreateCalendar.php',
154154
'OCA\\DAV\\Command\\DeleteCalendar' => __DIR__ . '/..' . '/../lib/Command/DeleteCalendar.php',
155+
'OCA\\DAV\\Command\\FixCalendarSyncCommand' => __DIR__ . '/..' . '/../lib/Command/FixCalendarSyncCommand.php',
155156
'OCA\\DAV\\Command\\ListCalendars' => __DIR__ . '/..' . '/../lib/Command/ListCalendars.php',
156157
'OCA\\DAV\\Command\\MoveCalendar' => __DIR__ . '/..' . '/../lib/Command/MoveCalendar.php',
157158
'OCA\\DAV\\Command\\RemoveInvalidShares' => __DIR__ . '/..' . '/../lib/Command/RemoveInvalidShares.php',

apps/dav/lib/CalDAV/CalDavBackend.php

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
use Sabre\VObject\Reader;
100100
use Sabre\VObject\Recur\EventIterator;
101101
use function array_column;
102+
use function array_map;
102103
use function array_merge;
103104
use function array_values;
104105
use function explode;
@@ -868,7 +869,7 @@ public function updateCalendar($calendarId, PropPatch $propPatch) {
868869
$query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId)));
869870
$query->executeStatement();
870871

871-
$this->addChange($calendarId, "", 2);
872+
$this->addChanges($calendarId, [""], 2);
872873

873874
$calendarData = $this->getCalendarById($calendarId);
874875
$shares = $this->getShares($calendarId);
@@ -1287,7 +1288,7 @@ public function createCalendarObject($calendarId, $objectUri, $calendarData, $ca
12871288
->executeStatement();
12881289

12891290
$this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1290-
$this->addChange($calendarId, $objectUri, 1, $calendarType);
1291+
$this->addChanges($calendarId, [$objectUri], 1, $calendarType);
12911292

12921293
$objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType);
12931294
assert($objectRow !== null);
@@ -1348,7 +1349,7 @@ public function updateCalendarObject($calendarId, $objectUri, $calendarData, $ca
13481349
->executeStatement();
13491350

13501351
$this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType);
1351-
$this->addChange($calendarId, $objectUri, 2, $calendarType);
1352+
$this->addChanges($calendarId, [$objectUri], 2, $calendarType);
13521353

13531354
$objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType);
13541355
if (is_array($objectRow)) {
@@ -1398,8 +1399,8 @@ public function moveCalendarObject(int $sourceCalendarId, int $targetCalendarId,
13981399
$this->purgeProperties($sourceCalendarId, $objectId);
13991400
$this->updateProperties($targetCalendarId, $object['uri'], $object['calendardata'], $calendarType);
14001401

1401-
$this->addChange($sourceCalendarId, $object['uri'], 3, $calendarType);
1402-
$this->addChange($targetCalendarId, $object['uri'], 1, $calendarType);
1402+
$this->addChanges($sourceCalendarId, [$object['uri']], 3, $calendarType);
1403+
$this->addChanges($targetCalendarId, [$object['uri']], 1, $calendarType);
14031404

14041405
$object = $this->getCalendarObjectById($newPrincipalUri, $objectId);
14051406
// Calendar Object wasn't found - possibly because it was deleted in the meantime by a different client
@@ -1526,7 +1527,7 @@ public function deleteCalendarObject($calendarId, $objectUri, $calendarType = se
15261527
}
15271528
}
15281529

1529-
$this->addChange($calendarId, $objectUri, 3, $calendarType);
1530+
$this->addChanges($calendarId, [$objectUri], 3, $calendarType);
15301531
}, $this->db);
15311532
}
15321533

@@ -1568,7 +1569,7 @@ public function restoreCalendarObject(array $objectData): void {
15681569
// Welp, this should possibly not have happened, but let's ignore
15691570
return;
15701571
}
1571-
$this->addChange($row['calendarid'], $row['uri'], 1, (int) $row['calendartype']);
1572+
$this->addChanges($row['calendarid'], [$row['uri']], 1, (int) $row['calendartype']);
15721573

15731574
$calendarRow = $this->getCalendarById((int) $row['calendarid']);
15741575
if ($calendarRow === null) {
@@ -2749,16 +2750,16 @@ public function createSchedulingObject($principalUri, $objectUri, $objectData) {
27492750
* Adds a change record to the calendarchanges table.
27502751
*
27512752
* @param mixed $calendarId
2752-
* @param string $objectUri
2753+
* @param string[] $objectUris
27532754
* @param int $operation 1 = add, 2 = modify, 3 = delete.
27542755
* @param int $calendarType
27552756
* @return void
27562757
*/
2757-
protected function addChange(int $calendarId, string $objectUri, int $operation, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void {
2758+
protected function addChanges(int $calendarId, array $objectUris, int $operation, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void {
27582759
$this->cachedObjects = [];
27592760
$table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions';
27602761

2761-
$this->atomic(function () use ($calendarId, $objectUri, $operation, $calendarType, $table) {
2762+
$this->atomic(function () use ($calendarId, $objectUris, $operation, $calendarType, $table) {
27622763
$query = $this->db->getQueryBuilder();
27632764
$query->select('synctoken')
27642765
->from($table)
@@ -2770,13 +2771,16 @@ protected function addChange(int $calendarId, string $objectUri, int $operation,
27702771
$query = $this->db->getQueryBuilder();
27712772
$query->insert('calendarchanges')
27722773
->values([
2773-
'uri' => $query->createNamedParameter($objectUri),
2774+
'uri' => $query->createParameter('uri'),
27742775
'synctoken' => $query->createNamedParameter($syncToken),
27752776
'calendarid' => $query->createNamedParameter($calendarId),
27762777
'operation' => $query->createNamedParameter($operation),
27772778
'calendartype' => $query->createNamedParameter($calendarType),
2778-
])
2779-
->executeStatement();
2779+
]);
2780+
foreach ($objectUris as $uri) {
2781+
$query->setParameter('uri', $uri);
2782+
$query->executeStatement();
2783+
}
27802784

27812785
$query = $this->db->getQueryBuilder();
27822786
$query->update($table)
@@ -2786,6 +2790,47 @@ protected function addChange(int $calendarId, string $objectUri, int $operation,
27862790
}, $this->db);
27872791
}
27882792

2793+
public function restoreChanges(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void {
2794+
$this->cachedObjects = [];
2795+
2796+
$this->atomic(function () use ($calendarId, $calendarType) {
2797+
$qbAdded = $this->db->getQueryBuilder();
2798+
$qbAdded->select('uri')
2799+
->from('calendarobjects')
2800+
->where(
2801+
$qbAdded->expr()->andX(
2802+
$qbAdded->expr()->eq('calendarid', $qbAdded->createNamedParameter($calendarId)),
2803+
$qbAdded->expr()->eq('calendartype', $qbAdded->createNamedParameter($calendarType)),
2804+
$qbAdded->expr()->isNull('deleted_at'),
2805+
)
2806+
);
2807+
$resultAdded = $qbAdded->executeQuery();
2808+
$addedUris = $resultAdded->fetchAll(\PDO::FETCH_COLUMN);
2809+
$resultAdded->closeCursor();
2810+
// Track everything as changed
2811+
// Tracking the creation is not necessary because \OCA\DAV\CalDAV\CalDavBackend::getChangesForCalendar
2812+
// only returns the last change per object.
2813+
$this->addChanges($calendarId, $addedUris, 2, $calendarType);
2814+
2815+
$qbDeleted = $this->db->getQueryBuilder();
2816+
$qbDeleted->select('uri')
2817+
->from('calendarobjects')
2818+
->where(
2819+
$qbDeleted->expr()->andX(
2820+
$qbDeleted->expr()->eq('calendarid', $qbDeleted->createNamedParameter($calendarId)),
2821+
$qbDeleted->expr()->eq('calendartype', $qbDeleted->createNamedParameter($calendarType)),
2822+
$qbDeleted->expr()->isNotNull('deleted_at'),
2823+
)
2824+
);
2825+
$resultDeleted = $qbDeleted->executeQuery();
2826+
$deletedUris = array_map(function (string $uri) {
2827+
return str_replace("-deleted.ics", ".ics", $uri);
2828+
}, $resultDeleted->fetchAll(\PDO::FETCH_COLUMN));
2829+
$resultDeleted->closeCursor();
2830+
$this->addChanges($calendarId, $deletedUris, 3, $calendarType);
2831+
}, $this->db);
2832+
}
2833+
27892834
/**
27902835
* Parses some information from calendar objects, used for optimized
27912836
* calendar-queries.
@@ -3127,9 +3172,7 @@ public function purgeAllCachedEventsForSubscription($subscriptionId) {
31273172
->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION)))
31283173
->executeStatement();
31293174

3130-
foreach ($uris as $uri) {
3131-
$this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION);
3132-
}
3175+
$this->addChanges($subscriptionId, $uris, 3, self::CALENDAR_TYPE_SUBSCRIPTION);
31333176
}, $this->db);
31343177
}
31353178

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* @copyright 2024 Christoph Wurst <[email protected]>
7+
*
8+
* @author 2024 Christoph Wurst <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
namespace OCA\DAV\Command;
27+
28+
use OCA\DAV\CalDAV\CalDavBackend;
29+
use OCP\IUser;
30+
use OCP\IUserManager;
31+
use Symfony\Component\Console\Command\Command;
32+
use Symfony\Component\Console\Helper\ProgressBar;
33+
use Symfony\Component\Console\Input\InputArgument;
34+
use Symfony\Component\Console\Input\InputInterface;
35+
use Symfony\Component\Console\Output\OutputInterface;
36+
37+
class FixCalendarSyncCommand extends Command {
38+
39+
public function __construct(private IUserManager $userManager,
40+
private CalDavBackend $calDavBackend) {
41+
parent::__construct('dav:fix-missing-caldav-changes');
42+
}
43+
44+
protected function configure(): void {
45+
$this->setDescription('Insert missing calendarchanges rows for existing events');
46+
$this->addArgument(
47+
'user',
48+
InputArgument::OPTIONAL,
49+
'User to fix calendar sync tokens for, if omitted all users will be fixed',
50+
null,
51+
);
52+
}
53+
54+
public function execute(InputInterface $input, OutputInterface $output): int {
55+
$userArg = $input->getArgument('user');
56+
if ($userArg !== null) {
57+
$user = $this->userManager->get($userArg);
58+
if ($user === null) {
59+
$output->writeln("<error>User $userArg does not exist</error>");
60+
return 1;
61+
}
62+
63+
$this->fixUserCalendars($user);
64+
} else {
65+
$progress = new ProgressBar($output);
66+
$this->userManager->callForSeenUsers(function (IUser $user) use ($progress) {
67+
$this->fixUserCalendars($user, $progress);
68+
});
69+
$progress->finish();
70+
}
71+
return 0;
72+
}
73+
74+
private function fixUserCalendars(IUser $user, ?ProgressBar $progress = null): void {
75+
$calendars = $this->calDavBackend->getCalendarsForUser("principals/users/" . $user->getUID());
76+
77+
foreach ($calendars as $calendar) {
78+
$this->calDavBackend->restoreChanges($calendar['id']);
79+
}
80+
81+
if ($progress !== null) {
82+
$progress->advance();
83+
}
84+
}
85+
86+
}

apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use OCP\App\IAppManager;
3636
use OCP\EventDispatcher\IEventDispatcher;
3737
use OCP\IConfig;
38+
use OCP\IDBConnection;
3839
use OCP\IGroupManager;
3940
use OCP\IUserManager;
4041
use OCP\IUserSession;
@@ -56,26 +57,16 @@
5657
*/
5758
abstract class AbstractCalDavBackend extends TestCase {
5859

59-
/** @var CalDavBackend */
60-
protected $backend;
61-
62-
/** @var Principal | MockObject */
63-
protected $principal;
64-
/** @var IUserManager|MockObject */
65-
protected $userManager;
66-
/** @var IGroupManager|MockObject */
67-
protected $groupManager;
68-
/** @var IEventDispatcher|MockObject */
69-
protected $dispatcher;
70-
71-
72-
/** @var IConfig | MockObject */
73-
private $config;
74-
/** @var ISecureRandom */
75-
private $random;
76-
/** @var LoggerInterface*/
77-
private $logger;
7860

61+
protected CalDavBackend $backend;
62+
protected Principal|MockObject $principal;
63+
protected IUserManager|MockObject $userManager;
64+
protected IGroupManager|MockObject $groupManager;
65+
protected IEventDispatcher|MockObject $dispatcher;
66+
private LoggerInterface|MockObject $logger;
67+
private IConfig|MockObject $config;
68+
private ISecureRandom $random;
69+
protected IDBConnection $db;
7970
public const UNIT_TEST_USER = 'principals/users/caldav-unit-test';
8071
public const UNIT_TEST_USER1 = 'principals/users/caldav-unit-test1';
8172
public const UNIT_TEST_GROUP = 'principals/groups/caldav-unit-test-group';
@@ -111,19 +102,19 @@ protected function setUp(): void {
111102
->withAnyParameters()
112103
->willReturn([self::UNIT_TEST_GROUP, self::UNIT_TEST_GROUP2]);
113104

114-
$db = \OC::$server->getDatabaseConnection();
105+
$this->db = \OC::$server->getDatabaseConnection();
115106
$this->random = \OC::$server->getSecureRandom();
116107
$this->logger = $this->createMock(LoggerInterface::class);
117108
$this->config = $this->createMock(IConfig::class);
118109
$this->backend = new CalDavBackend(
119-
$db,
110+
$this->db,
120111
$this->principal,
121112
$this->userManager,
122113
$this->groupManager,
123114
$this->random,
124115
$this->logger,
125116
$this->dispatcher,
126-
$this->config
117+
$this->config,
127118
);
128119

129120
$this->cleanUpBackend();

0 commit comments

Comments
 (0)