|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Calendar\Service; |
| 11 | + |
| 12 | +use OCA\Calendar\AppInfo\Application; |
| 13 | +use OCA\Calendar\Exception\ServiceException; |
| 14 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 15 | +use OCP\Calendar\ICreateFromString; |
| 16 | +use OCP\Calendar\IManager as ICalendarManager; |
| 17 | +use OCP\Files\IAppData; |
| 18 | +use OCP\Files\NotFoundException; |
| 19 | +use OCP\Files\NotPermittedException; |
| 20 | +use OCP\IAppConfig; |
| 21 | +use OCP\Security\ISecureRandom; |
| 22 | +use Sabre\VObject\Component\VCalendar; |
| 23 | +use Sabre\VObject\Component\VEvent; |
| 24 | + |
| 25 | +class ExampleEventService { |
| 26 | + private const FOLDER_NAME = 'example_event'; |
| 27 | + private const FILE_NAME = 'example_event.ics'; |
| 28 | + private const ENABLE_CONFIG_KEY = 'create_example_event'; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private readonly ICalendarManager $calendarManager, |
| 32 | + private readonly ISecureRandom $random, |
| 33 | + private readonly ITimeFactory $time, |
| 34 | + private readonly IAppData $appData, |
| 35 | + private readonly IAppConfig $appConfig, |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + public function createExampleEvent(string $userId): void { |
| 40 | + $calendars = $this->calendarManager->getCalendarsForPrincipal("principals/users/$userId"); |
| 41 | + if ($calendars === []) { |
| 42 | + throw new ServiceException("User $userId has no calendars"); |
| 43 | + } |
| 44 | + |
| 45 | + /** @var ICreateFromString $firstCalendar */ |
| 46 | + $firstCalendar = $calendars[0]; |
| 47 | + |
| 48 | + $customIcs = $this->getCustomExampleEvent(); |
| 49 | + if ($customIcs === null) { |
| 50 | + $this->createDefaultEvent($firstCalendar); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // TODO: parsing should be handled inside OCP |
| 55 | + try { |
| 56 | + $vCalendar = \Sabre\VObject\Reader::read($customIcs); |
| 57 | + if (!($vCalendar instanceof VCalendar)) { |
| 58 | + throw new ServiceException('Custom event does not contain a VCALENDAR component'); |
| 59 | + } |
| 60 | + |
| 61 | + /** @var VEvent|null $vEvent */ |
| 62 | + $vEvent = $vCalendar->getBaseComponent('VEVENT'); |
| 63 | + if ($vEvent === null) { |
| 64 | + throw new ServiceException('Custom event does not contain a VEVENT component'); |
| 65 | + } |
| 66 | + } catch (\Exception $e) { |
| 67 | + throw new ServiceException('Failed to parse custom event: ' . $e->getMessage(), 0, $e); |
| 68 | + } |
| 69 | + |
| 70 | + $uid = $this->random->generate(32, ISecureRandom::CHAR_ALPHANUMERIC); |
| 71 | + $vEvent->UID = $uid; |
| 72 | + $vEvent->DTSTART = $this->getStartDate(); |
| 73 | + $vEvent->DTEND = $this->getEndDate(); |
| 74 | + $vEvent->remove('ORGANIZER'); |
| 75 | + $vEvent->remove('ATTENDEE'); |
| 76 | + $firstCalendar->createFromString("$uid.ics", $vCalendar->serialize()); |
| 77 | + } |
| 78 | + |
| 79 | + private function getStartDate(): \DateTimeInterface { |
| 80 | + return $this->time->now() |
| 81 | + ->add(new \DateInterval('P7D')) |
| 82 | + ->setTime(10, 00); |
| 83 | + } |
| 84 | + |
| 85 | + private function getEndDate(): \DateTimeInterface { |
| 86 | + return $this->time->now() |
| 87 | + ->add(new \DateInterval('P7D')) |
| 88 | + ->setTime(11, 00); |
| 89 | + } |
| 90 | + |
| 91 | + private function createDefaultEvent(ICreateFromString $calendar): void { |
| 92 | + $defaultDescription = <<<EOF |
| 93 | +Welcome to Nextcloud Calendar! |
| 94 | +
|
| 95 | +This is a sample event - explore the flexibility of planning with Nextcloud Calendar by making any edits you want! |
| 96 | +
|
| 97 | +With Nextcloud Calendar, you can: |
| 98 | +- Create, edit, and manage events effortlessly. |
| 99 | +- Create multiple calendars and share them with teammates, friends, or family. |
| 100 | +- Check availability and display your busy times to others. |
| 101 | +- Seamlessly integrate with apps and devices via CalDAV. |
| 102 | +- Customize your experience: schedule recurring events, adjust notifications and other settings. |
| 103 | +EOF; |
| 104 | + |
| 105 | + $eventBuilder = $this->calendarManager->createEventBuilder(); |
| 106 | + $eventBuilder->setSummary('Example event - open me!'); |
| 107 | + $eventBuilder->setDescription($defaultDescription); |
| 108 | + $eventBuilder->setStartDate($this->getStartDate()); |
| 109 | + $eventBuilder->setEndDate($this->getEndDate()); |
| 110 | + $eventBuilder->createInCalendar($calendar); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return string|null The ics of the custom example event or null if no custom event was uploaded. |
| 115 | + * @throws ServiceException If reading the custom ics file fails. |
| 116 | + */ |
| 117 | + private function getCustomExampleEvent(): ?string { |
| 118 | + try { |
| 119 | + $folder = $this->appData->getFolder(self::FOLDER_NAME); |
| 120 | + $icsFile = $folder->getFile(self::FILE_NAME); |
| 121 | + } catch (NotFoundException $e) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + |
| 125 | + try { |
| 126 | + return $icsFile->getContent(); |
| 127 | + } catch (NotFoundException|NotPermittedException $e) { |
| 128 | + throw new ServiceException( |
| 129 | + 'Failed to read custom example event', |
| 130 | + 0, |
| 131 | + $e, |
| 132 | + ); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public function saveCustomExampleEvent(string $ics): void { |
| 137 | + try { |
| 138 | + $folder = $this->appData->getFolder(self::FOLDER_NAME); |
| 139 | + } catch (NotFoundException $e) { |
| 140 | + $folder = $this->appData->newFolder(self::FOLDER_NAME); |
| 141 | + } |
| 142 | + |
| 143 | + try { |
| 144 | + $existingFile = $folder->getFile(self::FILE_NAME); |
| 145 | + $existingFile->putContent($ics); |
| 146 | + } catch (NotFoundException $e) { |
| 147 | + $folder->newFile(self::FILE_NAME, $ics); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + public function deleteCustomExampleEvent(): void { |
| 152 | + try { |
| 153 | + $folder = $this->appData->getFolder(self::FOLDER_NAME); |
| 154 | + $file = $folder->getFile(self::FILE_NAME); |
| 155 | + } catch (NotFoundException $e) { |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + $file->delete(); |
| 160 | + } |
| 161 | + |
| 162 | + public function hasCustomExampleEvent(): bool { |
| 163 | + try { |
| 164 | + return $this->getCustomExampleEvent() !== null; |
| 165 | + } catch (ServiceException $e) { |
| 166 | + return false; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + public function setCreateExampleEvent(bool $enable) { |
| 171 | + $this->appConfig->setValueBool(Application::APP_ID, self::ENABLE_CONFIG_KEY, $enable); |
| 172 | + } |
| 173 | + |
| 174 | + public function shouldCreateExampleEvent(): bool { |
| 175 | + return $this->appConfig->getValueBool(Application::APP_ID, self::ENABLE_CONFIG_KEY, true); |
| 176 | + } |
| 177 | +} |
0 commit comments