CreateSubscription.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Command;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCA\Theming\ThemingDefaults;
  10. use OCP\IUserManager;
  11. use Sabre\DAV\Xml\Property\Href;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class CreateSubscription extends Command {
  17. public function __construct(
  18. protected IUserManager $userManager,
  19. private CalDavBackend $caldav,
  20. private ThemingDefaults $themingDefaults,
  21. ) {
  22. parent::__construct();
  23. }
  24. protected function configure(): void {
  25. $this
  26. ->setName('dav:create-subscription')
  27. ->setDescription('Create a dav subscription')
  28. ->addArgument('user',
  29. InputArgument::REQUIRED,
  30. 'User for whom the subscription will be created')
  31. ->addArgument('name',
  32. InputArgument::REQUIRED,
  33. 'Name of the subscription to create')
  34. ->addArgument('url',
  35. InputArgument::REQUIRED,
  36. 'Source url of the subscription to create')
  37. ->addArgument('color',
  38. InputArgument::OPTIONAL,
  39. 'Hex color code for the calendar color');
  40. }
  41. protected function execute(InputInterface $input, OutputInterface $output): int {
  42. $user = $input->getArgument('user');
  43. if (!$this->userManager->userExists($user)) {
  44. $output->writeln("<error>User <$user> in unknown.</error>");
  45. return self::FAILURE;
  46. }
  47. $name = $input->getArgument('name');
  48. $url = $input->getArgument('url');
  49. $color = $input->getArgument('color') ?? $this->themingDefaults->getColorPrimary();
  50. $subscriptions = $this->caldav->getSubscriptionsForUser("principals/users/$user");
  51. $exists = array_filter($subscriptions, function ($row) use ($url) {
  52. return $row['source'] === $url;
  53. });
  54. if (!empty($exists)) {
  55. $output->writeln("<error>Subscription for url <$url> already exists for this user.</error>");
  56. return self::FAILURE;
  57. }
  58. $urlProperty = new Href($url);
  59. $properties = ['{http://owncloud.org/ns}calendar-enabled' => 1,
  60. '{DAV:}displayname' => $name,
  61. '{http://apple.com/ns/ical/}calendar-color' => $color,
  62. '{http://calendarserver.org/ns/}source' => $urlProperty,
  63. ];
  64. $this->caldav->createSubscription("principals/users/$user", $name, $properties);
  65. return self::SUCCESS;
  66. }
  67. }