UpcomingEventsController.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Controller;
  8. use OCA\DAV\AppInfo\Application;
  9. use OCA\DAV\CalDAV\UpcomingEvent;
  10. use OCA\DAV\CalDAV\UpcomingEventsService;
  11. use OCA\DAV\ResponseDefinitions;
  12. use OCP\AppFramework\Http;
  13. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\AppFramework\OCSController;
  16. use OCP\IRequest;
  17. /**
  18. * @psalm-import-type DAVUpcomingEvent from ResponseDefinitions
  19. */
  20. class UpcomingEventsController extends OCSController {
  21. public function __construct(
  22. IRequest $request,
  23. private ?string $userId,
  24. private UpcomingEventsService $service,
  25. ) {
  26. parent::__construct(Application::APP_ID, $request);
  27. }
  28. /**
  29. * Get information about upcoming events
  30. *
  31. * @param string|null $location location/URL to filter by
  32. * @return DataResponse<Http::STATUS_OK, array{events: list<DAVUpcomingEvent>}, array{}>|DataResponse<Http::STATUS_UNAUTHORIZED, null, array{}>
  33. *
  34. * 200: Upcoming events
  35. * 401: When not authenticated
  36. */
  37. #[NoAdminRequired]
  38. public function getEvents(?string $location = null): DataResponse {
  39. if ($this->userId === null) {
  40. return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
  41. }
  42. return new DataResponse([
  43. 'events' => array_values(array_map(fn (UpcomingEvent $e) => $e->jsonSerialize(), $this->service->getEvents(
  44. $this->userId,
  45. $location,
  46. ))),
  47. ]);
  48. }
  49. }