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