From a58d1e6b06d553927ff6b80822074e640ee66a9c Mon Sep 17 00:00:00 2001 From: Anna Larch Date: Mon, 27 Sep 2021 14:58:16 +0200 Subject: Add Public Calendar Provider Signed-off-by: Anna Larch Signed-off-by: Christoph Wurst --- lib/private/Calendar/CalendarQuery.php | 124 +++++++++++++++++++++++++++++++++ lib/private/Calendar/Manager.php | 50 ++++++++++++- 2 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 lib/private/Calendar/CalendarQuery.php (limited to 'lib/private/Calendar') diff --git a/lib/private/Calendar/CalendarQuery.php b/lib/private/Calendar/CalendarQuery.php new file mode 100644 index 00000000000..6b4e2b3be16 --- /dev/null +++ b/lib/private/Calendar/CalendarQuery.php @@ -0,0 +1,124 @@ + + * + * @author Anna Larch + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OC\Calendar; + +use OCP\Calendar\ICalendarQuery; + +class CalendarQuery implements ICalendarQuery { + + /** @var string */ + private $principalUri; + + /** @var array */ + public $searchProperties; + + /** @var string|null */ + private $searchPattern; + + /** @var array */ + private $options; + + /** @var int|null */ + private $offset; + + /** @var int|null */ + private $limit; + + /** @var array */ + private $calendarUris; + + public function __construct(string $principalUri) { + $this->principalUri = $principalUri; + $this->searchProperties = []; + $this->options = [ + 'types' => [], + ]; + } + + public function getPrincipalUri(): string { + return $this->principalUri; + } + + public function setPrincipalUri(string $principalUri): void { + $this->principalUri = $principalUri; + } + + public function setSearchPattern(string $pattern): void { + $this->searchPattern = $pattern; + } + + public function getSearchPattern(): ?string { + return $this->searchPattern; + } + + public function addSearchProperty(string $value): void { + $this->searchProperties[] = $value; + } + + public function getSearchProperties(): array { + return $this->searchProperties; + } + + public function addSearchCalendar(string $calendarUri): void { + $this->calendarUris[] = $calendarUri; + } + + public function getCalendarUris(): array { + return $this->calendarUris; + } + + public function getLimit(): ?int { + return $this->limit; + } + + public function setLimit(int $limit): void { + $this->limit = $limit; + } + + public function getOffset(): ?int { + return $this->offset; + } + + public function setOffset(int $offset): void { + $this->offset = $offset; + } + + public function addType(string $value): void { + $this->options['types'][] = $value; + } + + public function setTimerangeStart(\DateTimeImmutable $startTime): void { + $this->options['timerange']['start'] = $startTime; + } + + public function setTimerangeEnd(\DateTimeImmutable $endTime): void { + $this->options['timerange']['end'] = $endTime; + } + + public function getOptions(): array { + return $this->options; + } +} diff --git a/lib/private/Calendar/Manager.php b/lib/private/Calendar/Manager.php index ab22c3ba7b8..30ee60e4943 100644 --- a/lib/private/Calendar/Manager.php +++ b/lib/private/Calendar/Manager.php @@ -1,4 +1,7 @@ * @@ -23,9 +26,13 @@ */ namespace OC\Calendar; +use OC\AppFramework\Bootstrap\Coordinator; use OCP\Calendar\ICalendar; +use OCP\Calendar\ICalendarProvider; +use OCP\Calendar\ICalendarQuery; +use OCP\Calendar\IManager; -class Manager implements \OCP\Calendar\IManager { +class Manager implements IManager { /** * @var ICalendar[] holds all registered calendars @@ -37,6 +44,13 @@ class Manager implements \OCP\Calendar\IManager { */ private $calendarLoaders = []; + /** @var Coordinator */ + private $coordinator; + + public function __construct(Coordinator $coordinator) { + $this->coordinator = $coordinator; + } + /** * This function is used to search and find objects within the user's calendars. * In case $pattern is empty all events/journals/todos will be returned. @@ -137,4 +151,38 @@ class Manager implements \OCP\Calendar\IManager { } $this->calendarLoaders = []; } + + public function searchForPrincipal(ICalendarQuery $query): array { + $context = $this->coordinator->getRegistrationContext(); + if ($context === null) { + return []; + } + + /** @var CalendarQuery $query */ + $calendars = array_merge(...array_map(static function (ICalendarProvider $p) use ($query) { + return $p->getCalendars($query->getPrincipalUri(), $query->getCalendarUris()); + }, $context->getCalendarProviders())); + + $results = []; + /** @var ICalendar $calendar */ + foreach ($calendars as $calendar) { + $r = $calendar->search( + $query->getSearchPattern() ?? '', + $query->getSearchProperties(), + $query->getOptions(), + $query->getLimit(), + $query->getOffset() + ); + + foreach ($r as $o) { + $o['calendar-key'] = $calendar->getKey(); + $results[] = $o; + } + } + return $results; + } + + public function newQuery(string $principalUri): ICalendarQuery { + return new CalendarQuery($principalUri); + } } -- cgit v1.2.3