FixCalendarSyncCommand.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(private IUserManager $userManager,
  18. private CalDavBackend $calDavBackend) {
  19. parent::__construct('dav:fix-missing-caldav-changes');
  20. }
  21. protected function configure(): void {
  22. $this->setDescription('Insert missing calendarchanges rows for existing events');
  23. $this->addArgument(
  24. 'user',
  25. InputArgument::OPTIONAL,
  26. 'User to fix calendar sync tokens for, if omitted all users will be fixed',
  27. null,
  28. );
  29. }
  30. public function execute(InputInterface $input, OutputInterface $output): int {
  31. $userArg = $input->getArgument('user');
  32. if ($userArg !== null) {
  33. $user = $this->userManager->get($userArg);
  34. if ($user === null) {
  35. $output->writeln("<error>User $userArg does not exist</error>");
  36. return 1;
  37. }
  38. $this->fixUserCalendars($user);
  39. } else {
  40. $progress = new ProgressBar($output);
  41. $this->userManager->callForSeenUsers(function (IUser $user) use ($progress) {
  42. $this->fixUserCalendars($user, $progress);
  43. });
  44. $progress->finish();
  45. }
  46. return 0;
  47. }
  48. private function fixUserCalendars(IUser $user, ?ProgressBar $progress = null): void {
  49. $calendars = $this->calDavBackend->getCalendarsForUser('principals/users/' . $user->getUID());
  50. foreach ($calendars as $calendar) {
  51. $this->calDavBackend->restoreChanges($calendar['id']);
  52. }
  53. if ($progress !== null) {
  54. $progress->advance();
  55. }
  56. }
  57. }