diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2024-03-11 16:27:23 +0100 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2024-03-12 12:44:00 +0100 |
commit | f40f962c2e744b2b7ae74b62a4065aee7fc5eff3 (patch) | |
tree | 5ea6523a8a0e1d8a8fc4bd641a2516e159d2b9f1 /apps/dav/lib | |
parent | c42397358f05aa60ae91ed11e7754fddba182cce (diff) | |
download | nextcloud-server-f40f962c2e744b2b7ae74b62a4065aee7fc5eff3.tar.gz nextcloud-server-f40f962c2e744b2b7ae74b62a4065aee7fc5eff3.zip |
fix(dav): Add occ command to fix missing caldav sync tokens
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/CalDAV/CalDavBackend.php | 75 | ||||
-rw-r--r-- | apps/dav/lib/Command/FixCalendarSyncCommand.php | 86 |
2 files changed, 145 insertions, 16 deletions
diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index e3b8f9ce731..b1ff7419751 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -98,6 +98,7 @@ use Sabre\VObject\Property; use Sabre\VObject\Reader; use Sabre\VObject\Recur\EventIterator; use function array_column; +use function array_map; use function array_merge; use function array_values; use function explode; @@ -874,7 +875,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription $query->where($query->expr()->eq('id', $query->createNamedParameter($calendarId))); $query->executeStatement(); - $this->addChange($calendarId, "", 2); + $this->addChanges($calendarId, [""], 2); $calendarData = $this->getCalendarById($calendarId); $shares = $this->getShares($calendarId); @@ -1295,7 +1296,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription ->executeStatement(); $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); - $this->addChange($calendarId, $objectUri, 1, $calendarType); + $this->addChanges($calendarId, [$objectUri], 1, $calendarType); $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); assert($objectRow !== null); @@ -1356,7 +1357,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription ->executeStatement(); $this->updateProperties($calendarId, $objectUri, $calendarData, $calendarType); - $this->addChange($calendarId, $objectUri, 2, $calendarType); + $this->addChanges($calendarId, [$objectUri], 2, $calendarType); $objectRow = $this->getCalendarObject($calendarId, $objectUri, $calendarType); if (is_array($objectRow)) { @@ -1406,8 +1407,8 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription $this->purgeProperties($sourceCalendarId, $objectId); $this->updateProperties($targetCalendarId, $object['uri'], $object['calendardata'], $calendarType); - $this->addChange($sourceCalendarId, $object['uri'], 3, $calendarType); - $this->addChange($targetCalendarId, $object['uri'], 1, $calendarType); + $this->addChanges($sourceCalendarId, [$object['uri']], 3, $calendarType); + $this->addChanges($targetCalendarId, [$object['uri']], 1, $calendarType); $object = $this->getCalendarObjectById($newPrincipalUri, $objectId); // Calendar Object wasn't found - possibly because it was deleted in the meantime by a different client @@ -1534,7 +1535,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription } } - $this->addChange($calendarId, $objectUri, 3, $calendarType); + $this->addChanges($calendarId, [$objectUri], 3, $calendarType); }, $this->db); } @@ -1576,7 +1577,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription // Welp, this should possibly not have happened, but let's ignore return; } - $this->addChange($row['calendarid'], $row['uri'], 1, (int) $row['calendartype']); + $this->addChanges($row['calendarid'], [$row['uri']], 1, (int) $row['calendartype']); $calendarRow = $this->getCalendarById((int) $row['calendarid']); if ($calendarRow === null) { @@ -2758,16 +2759,16 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription * Adds a change record to the calendarchanges table. * * @param mixed $calendarId - * @param string $objectUri + * @param string[] $objectUris * @param int $operation 1 = add, 2 = modify, 3 = delete. * @param int $calendarType * @return void */ - protected function addChange(int $calendarId, string $objectUri, int $operation, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void { + protected function addChanges(int $calendarId, array $objectUris, int $operation, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void { $this->cachedObjects = []; $table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions'; - $this->atomic(function () use ($calendarId, $objectUri, $operation, $calendarType, $table) { + $this->atomic(function () use ($calendarId, $objectUris, $operation, $calendarType, $table) { $query = $this->db->getQueryBuilder(); $query->select('synctoken') ->from($table) @@ -2779,13 +2780,16 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription $query = $this->db->getQueryBuilder(); $query->insert('calendarchanges') ->values([ - 'uri' => $query->createNamedParameter($objectUri), + 'uri' => $query->createParameter('uri'), 'synctoken' => $query->createNamedParameter($syncToken), 'calendarid' => $query->createNamedParameter($calendarId), 'operation' => $query->createNamedParameter($operation), 'calendartype' => $query->createNamedParameter($calendarType), - ]) - ->executeStatement(); + ]); + foreach ($objectUris as $uri) { + $query->setParameter('uri', $uri); + $query->executeStatement(); + } $query = $this->db->getQueryBuilder(); $query->update($table) @@ -2795,6 +2799,47 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription }, $this->db); } + public function restoreChanges(int $calendarId, int $calendarType = self::CALENDAR_TYPE_CALENDAR): void { + $this->cachedObjects = []; + + $this->atomic(function () use ($calendarId, $calendarType) { + $qbAdded = $this->db->getQueryBuilder(); + $qbAdded->select('uri') + ->from('calendarobjects') + ->where( + $qbAdded->expr()->andX( + $qbAdded->expr()->eq('calendarid', $qbAdded->createNamedParameter($calendarId)), + $qbAdded->expr()->eq('calendartype', $qbAdded->createNamedParameter($calendarType)), + $qbAdded->expr()->isNull('deleted_at'), + ) + ); + $resultAdded = $qbAdded->executeQuery(); + $addedUris = $resultAdded->fetchAll(\PDO::FETCH_COLUMN); + $resultAdded->closeCursor(); + // Track everything as changed + // Tracking the creation is not necessary because \OCA\DAV\CalDAV\CalDavBackend::getChangesForCalendar + // only returns the last change per object. + $this->addChanges($calendarId, $addedUris, 2, $calendarType); + + $qbDeleted = $this->db->getQueryBuilder(); + $qbDeleted->select('uri') + ->from('calendarobjects') + ->where( + $qbDeleted->expr()->andX( + $qbDeleted->expr()->eq('calendarid', $qbDeleted->createNamedParameter($calendarId)), + $qbDeleted->expr()->eq('calendartype', $qbDeleted->createNamedParameter($calendarType)), + $qbDeleted->expr()->isNotNull('deleted_at'), + ) + ); + $resultDeleted = $qbDeleted->executeQuery(); + $deletedUris = array_map(function (string $uri) { + return str_replace("-deleted.ics", ".ics", $uri); + }, $resultDeleted->fetchAll(\PDO::FETCH_COLUMN)); + $resultDeleted->closeCursor(); + $this->addChanges($calendarId, $deletedUris, 3, $calendarType); + }, $this->db); + } + /** * Parses some information from calendar objects, used for optimized * calendar-queries. @@ -3137,9 +3182,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription ->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter(self::CALENDAR_TYPE_SUBSCRIPTION))) ->executeStatement(); - foreach ($uris as $uri) { - $this->addChange($subscriptionId, $uri, 3, self::CALENDAR_TYPE_SUBSCRIPTION); - } + $this->addChanges($subscriptionId, $uris, 3, self::CALENDAR_TYPE_SUBSCRIPTION); }, $this->db); } diff --git a/apps/dav/lib/Command/FixCalendarSyncCommand.php b/apps/dav/lib/Command/FixCalendarSyncCommand.php new file mode 100644 index 00000000000..6db32ff6d5e --- /dev/null +++ b/apps/dav/lib/Command/FixCalendarSyncCommand.php @@ -0,0 +1,86 @@ +<?php + +declare(strict_types=1); + +/* + * @copyright 2024 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2024 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\DAV\Command; + +use OCA\DAV\CalDAV\CalDavBackend; +use OCP\IUser; +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Helper\ProgressBar; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class FixCalendarSyncCommand extends Command { + + public function __construct(private IUserManager $userManager, + private CalDavBackend $calDavBackend) { + parent::__construct('dav:fix-missing-caldav-changes'); + } + + protected function configure(): void { + $this->setDescription('Insert missing calendarchanges rows for existing events'); + $this->addArgument( + 'user', + InputArgument::OPTIONAL, + 'User to fix calendar sync tokens for, if omitted all users will be fixed', + null, + ); + } + + public function execute(InputInterface $input, OutputInterface $output): int { + $userArg = $input->getArgument('user'); + if ($userArg !== null) { + $user = $this->userManager->get($userArg); + if ($user === null) { + $output->writeln("<error>User $userArg does not exist</error>"); + return 1; + } + + $this->fixUserCalendars($user); + } else { + $progress = new ProgressBar($output); + $this->userManager->callForSeenUsers(function (IUser $user) use ($progress) { + $this->fixUserCalendars($user, $progress); + }); + $progress->finish(); + } + return 0; + } + + private function fixUserCalendars(IUser $user, ?ProgressBar $progress = null): void { + $calendars = $this->calDavBackend->getCalendarsForUser("principals/users/" . $user->getUID()); + + foreach ($calendars as $calendar) { + $this->calDavBackend->restoreChanges($calendar['id']); + } + + if ($progress !== null) { + $progress->advance(); + } + } + +} |