1
0

FixCalendarSyncCommand.php 2.6 KB

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