1
0

UpcomingEventsService.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\CalDAV;
  8. use OCP\App\IAppManager;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\Calendar\IManager;
  11. use OCP\IURLGenerator;
  12. use OCP\IUserManager;
  13. use function array_map;
  14. class UpcomingEventsService {
  15. public function __construct(private IManager $calendarManager,
  16. private ITimeFactory $timeFactory,
  17. private IUserManager $userManager,
  18. private IAppManager $appManager,
  19. private IURLGenerator $urlGenerator) {
  20. }
  21. /**
  22. * @return UpcomingEvent[]
  23. */
  24. public function getEvents(string $userId, ?string $location = null): array {
  25. $searchQuery = $this->calendarManager->newQuery('principals/users/' . $userId);
  26. if ($location !== null) {
  27. $searchQuery->addSearchProperty('LOCATION');
  28. $searchQuery->setSearchPattern($location);
  29. }
  30. $searchQuery->addType('VEVENT');
  31. $searchQuery->setLimit(3);
  32. $now = $this->timeFactory->now();
  33. $searchQuery->setTimerangeStart($now->modify('-1 minute'));
  34. $searchQuery->setTimerangeEnd($now->modify('+1 month'));
  35. $events = $this->calendarManager->searchForPrincipal($searchQuery);
  36. $calendarAppEnabled = $this->appManager->isEnabledForUser(
  37. 'calendar',
  38. $this->userManager->get($userId),
  39. );
  40. return array_map(fn (array $event) => new UpcomingEvent(
  41. $event['uri'],
  42. ($event['objects'][0]['RECURRENCE-ID'][0] ?? null)?->getTimeStamp(),
  43. $event['calendar-uri'],
  44. $event['objects'][0]['DTSTART'][0]?->getTimestamp(),
  45. $event['objects'][0]['SUMMARY'][0] ?? null,
  46. $event['objects'][0]['LOCATION'][0] ?? null,
  47. match ($calendarAppEnabled) {
  48. // TODO: create a named, deep route in calendar
  49. // TODO: it's a code smell to just assume this route exists, find an abstraction
  50. true => $this->urlGenerator->linkToRouteAbsolute('calendar.view.index'),
  51. false => null,
  52. },
  53. ), $events);
  54. }
  55. }