UpcomingEventsController.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. private ?string $userId;
  22. private UpcomingEventsService $service;
  23. public function __construct(
  24. IRequest $request,
  25. ?string $userId,
  26. UpcomingEventsService $service) {
  27. parent::__construct(Application::APP_ID, $request);
  28. $this->userId = $userId;
  29. $this->service = $service;
  30. }
  31. /**
  32. * Get information about upcoming events
  33. *
  34. * @param string|null $location location/URL to filter by
  35. * @return DataResponse<Http::STATUS_OK, array{events: DAVUpcomingEvent[]}, array{}>|DataResponse<Http::STATUS_UNAUTHORIZED, null, array{}>
  36. *
  37. * 200: Upcoming events
  38. * 401: When not authenticated
  39. */
  40. #[NoAdminRequired]
  41. public function getEvents(?string $location = null): DataResponse {
  42. if ($this->userId === null) {
  43. return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
  44. }
  45. return new DataResponse([
  46. 'events' => array_map(fn (UpcomingEvent $e) => $e->jsonSerialize(), $this->service->getEvents(
  47. $this->userId,
  48. $location,
  49. )),
  50. ]);
  51. }
  52. }