|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\Calendar\Controller; |
| 10 | + |
| 11 | +use OCA\Calendar\Service\CalendarInitialStateService; |
| 12 | +use OCP\AppFramework\Controller; |
| 13 | +use OCP\AppFramework\Http\RedirectResponse; |
| 14 | +use OCP\AppFramework\Http\Response; |
| 15 | +use OCP\AppFramework\Http\TemplateResponse; |
| 16 | +use OCP\Calendar\IManager; |
| 17 | +use OCP\IRequest; |
| 18 | +use OCP\IURLGenerator; |
| 19 | + |
| 20 | +/** |
| 21 | + * Controller for permanent event deep links. |
| 22 | + * |
| 23 | + * Routes like /apps/calendar/event/{uid} and /apps/calendar/event/{uid}/{recurrenceId} |
| 24 | + * resolve the UID to the appropriate calendar object and redirect to the standard |
| 25 | + * edit route |
| 26 | + */ |
| 27 | +class EventController extends Controller { |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + string $appName, |
| 31 | + IRequest $request, |
| 32 | + private CalendarInitialStateService $calendarInitialStateService, |
| 33 | + private IManager $calendarManager, |
| 34 | + private IURLGenerator $urlGenerator, |
| 35 | + private ?string $userId, |
| 36 | + ) { |
| 37 | + parent::__construct($appName, $request); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Resolve a permanent event deep link. |
| 42 | + * |
| 43 | + * Searches all calendars for an event with the given UID and redirects |
| 44 | + * to the standard edit route. Falls back to serving the SPA if the |
| 45 | + * event cannot be resolved server-side. |
| 46 | + * |
| 47 | + * @NoAdminRequired |
| 48 | + * @NoCSRFRequired |
| 49 | + * |
| 50 | + * @param string $uid The iCalendar UID of the event |
| 51 | + * @param string|null $recurrenceId Unix timestamp of the recurrence instance, or null for 'next' |
| 52 | + * @return Response |
| 53 | + */ |
| 54 | + public function index(string $uid, ?string $recurrenceId = null): Response { |
| 55 | + if ($this->userId === null) { |
| 56 | + $this->calendarInitialStateService->run(); |
| 57 | + return new TemplateResponse($this->appName, 'main'); |
| 58 | + } |
| 59 | + |
| 60 | + $principalUri = "principals/users/{$this->userId}"; |
| 61 | + $calendars = $this->calendarManager->getCalendarsForPrincipal($principalUri); |
| 62 | + |
| 63 | + foreach ($calendars as $calendar) { |
| 64 | + if (method_exists($calendar, 'isDeleted') && $calendar->isDeleted()) { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + $results = $calendar->search('', [], ['uid' => $uid], 1); |
| 69 | + if (!empty($results)) { |
| 70 | + $result = $results[0]; |
| 71 | + $objectUri = $result['uri']; |
| 72 | + $calendarUri = $calendar->getUri(); |
| 73 | + |
| 74 | + $davPath = '/remote.php/dav/calendars/' . $this->userId . '/' . $calendarUri . '/' . $objectUri; |
| 75 | + $objectId = base64_encode($davPath); |
| 76 | + |
| 77 | + $editRecurrenceId = $recurrenceId ?? 'next'; |
| 78 | + |
| 79 | + return new RedirectResponse( |
| 80 | + $this->urlGenerator->linkToRoute('calendar.view.indexdirect.edit.recurrenceId', [ |
| 81 | + 'objectId' => $objectId, |
| 82 | + 'recurrenceId' => $editRecurrenceId, |
| 83 | + ]) |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Event not found (no access or deleted) — redirect to a non-existent object so |
| 89 | + // the SPA's error handling displays "Event does not exist" instead of a blank page. |
| 90 | + return new RedirectResponse( |
| 91 | + $this->urlGenerator->linkToRoute('calendar.view.indexdirect.edit.recurrenceId', [ |
| 92 | + 'objectId' => base64_encode("/event-not-found/$uid"), |
| 93 | + 'recurrenceId' => $recurrenceId ?? 'next', |
| 94 | + ]) |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments