FixCalendarSyncCommand.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Command;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCP\IUser;
  10. use OCP\IUserManager;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\ProgressBar;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class FixCalendarSyncCommand extends Command {
  17. public function __construct(
  18. private IUserManager $userManager,
  19. private CalDavBackend $calDavBackend,
  20. ) {
  21. parent::__construct('dav:fix-missing-caldav-changes');
  22. }
  23. protected function configure(): void {
  24. $this->setDescription('Insert missing calendarchanges rows for existing events');
  25. $this->addArgument(
  26. 'user',
  27. InputArgument::OPTIONAL,
  28. 'User to fix calendar sync tokens for, if omitted all users will be fixed',
  29. null,
  30. );
  31. }
  32. public function execute(InputInterface $input, OutputInterface $output): int {
  33. $userArg = $input->getArgument('user');
  34. if ($userArg !== null) {
  35. $user = $this->userManager->get($userArg);
  36. if ($user === null) {
  37. $output->writeln("<error>User $userArg does not exist</error>");
  38. return 1;
  39. }
  40. $this->fixUserCalendars($user);
  41. } else {
  42. $progress = new ProgressBar($output);
  43. $this->userManager->callForSeenUsers(function (IUser $user) use ($progress): void {
  44. $this->fixUserCalendars($user, $progress);
  45. });
  46. $progress->finish();
  47. }
  48. return 0;
  49. }
  50. private function fixUserCalendars(IUser $user, ?ProgressBar $progress = null): void {
  51. $calendars = $this->calDavBackend->getCalendarsForUser('principals/users/' . $user->getUID());
  52. foreach ($calendars as $calendar) {
  53. $this->calDavBackend->restoreChanges($calendar['id']);
  54. }
  55. if ($progress !== null) {
  56. $progress->advance();
  57. }
  58. }
  59. }