|
29 | 29 |
|
30 | 30 | use OCA\DAV\CalDAV\CalDavBackend; |
31 | 31 | use OCP\IUser; |
| 32 | +use OCP\Search\IFilteringProvider; |
32 | 33 | use OCP\Search\ISearchQuery; |
33 | 34 | use OCP\Search\SearchResult; |
34 | 35 | use OCP\Search\SearchResultEntry; |
35 | 36 | use Sabre\VObject\Component; |
36 | 37 | use Sabre\VObject\DateTimeParser; |
37 | 38 | use Sabre\VObject\Property; |
| 39 | +use function array_combine; |
| 40 | +use function array_fill; |
| 41 | +use function array_key_exists; |
| 42 | +use function array_map; |
38 | 43 |
|
39 | 44 | /** |
40 | 45 | * Class EventsSearchProvider |
41 | 46 | * |
42 | 47 | * @package OCA\DAV\Search |
43 | 48 | */ |
44 | | -class EventsSearchProvider extends ACalendarSearchProvider { |
| 49 | +class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider { |
45 | 50 | /** |
46 | 51 | * @var string[] |
47 | 52 | */ |
@@ -106,22 +111,60 @@ public function search( |
106 | 111 | $calendarsById = $this->getSortedCalendars($principalUri); |
107 | 112 | $subscriptionsById = $this->getSortedSubscriptions($principalUri); |
108 | 113 |
|
109 | | - $searchResults = $this->backend->searchPrincipalUri( |
110 | | - $principalUri, |
111 | | - $query->getFilter('term')?->get() ?? '', |
112 | | - [self::$componentType], |
113 | | - self::$searchProperties, |
114 | | - self::$searchParameters, |
115 | | - [ |
116 | | - 'limit' => $query->getLimit(), |
117 | | - 'offset' => $query->getCursor(), |
118 | | - 'timerange' => [ |
119 | | - 'start' => $query->getFilter('since')?->get(), |
120 | | - 'end' => $query->getFilter('until')?->get(), |
| 114 | + /** @var string|null $term */ |
| 115 | + $term = $query->getFilter('term')?->get(); |
| 116 | + if ($term === null) { |
| 117 | + $searchResults = []; |
| 118 | + } else { |
| 119 | + $searchResults = $this->backend->searchPrincipalUri( |
| 120 | + $principalUri, |
| 121 | + $term, |
| 122 | + [self::$componentType], |
| 123 | + self::$searchProperties, |
| 124 | + self::$searchParameters, |
| 125 | + [ |
| 126 | + 'limit' => $query->getLimit(), |
| 127 | + 'offset' => $query->getCursor(), |
| 128 | + 'timerange' => [ |
| 129 | + 'start' => $query->getFilter('since')?->get(), |
| 130 | + 'end' => $query->getFilter('until')?->get(), |
| 131 | + ], |
| 132 | + ] |
| 133 | + ); |
| 134 | + } |
| 135 | + /** @var IUser|null $person */ |
| 136 | + $person = $query->getFilter('person')?->get(); |
| 137 | + $personDisplayName = $person?->getDisplayName(); |
| 138 | + if ($personDisplayName !== null) { |
| 139 | + $attendeeSearchResults = $this->backend->searchPrincipalUri( |
| 140 | + $principalUri, |
| 141 | + $personDisplayName, |
| 142 | + [self::$componentType], |
| 143 | + ['ATTENDEE'], |
| 144 | + self::$searchParameters, |
| 145 | + [ |
| 146 | + 'limit' => $query->getLimit(), |
| 147 | + 'offset' => $query->getCursor(), |
| 148 | + 'timerange' => [ |
| 149 | + 'start' => $query->getFilter('since')?->get(), |
| 150 | + 'end' => $query->getFilter('until')?->get(), |
| 151 | + ], |
121 | 152 | ], |
122 | | - ] |
123 | | - ); |
124 | | - $formattedResults = \array_map(function (array $eventRow) use ($calendarsById, $subscriptionsById):SearchResultEntry { |
| 153 | + ); |
| 154 | + |
| 155 | + $searchResultIndex = array_combine( |
| 156 | + array_map(fn($event) => $event['calendarid'] . '-' . $event['uri'], $searchResults), |
| 157 | + array_fill(0, count($searchResults), null), |
| 158 | + ); |
| 159 | + foreach ($attendeeSearchResults as $attendeeResult) { |
| 160 | + if (array_key_exists($attendeeResult['calendarid'] . '-' . $attendeeResult['uri'], $searchResultIndex)) { |
| 161 | + // Duplicate |
| 162 | + continue; |
| 163 | + } |
| 164 | + $searchResults[] = $attendeeResult; |
| 165 | + } |
| 166 | + } |
| 167 | + $formattedResults = \array_map(function (array $eventRow) use ($calendarsById, $subscriptionsById): SearchResultEntry { |
125 | 168 | $component = $this->getPrimaryComponent($eventRow['calendardata'], self::$componentType); |
126 | 169 | $title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled event')); |
127 | 170 | $subline = $this->generateSubline($component); |
@@ -228,4 +271,21 @@ protected function isDayEqual( |
228 | 271 | ): bool { |
229 | 272 | return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d'); |
230 | 273 | } |
| 274 | + |
| 275 | + public function getSupportedFilters(): array { |
| 276 | + return [ |
| 277 | + 'term', |
| 278 | + 'person', |
| 279 | + 'since', |
| 280 | + 'until', |
| 281 | + ]; |
| 282 | + } |
| 283 | + |
| 284 | + public function getAlternateIds(): array { |
| 285 | + return []; |
| 286 | + } |
| 287 | + |
| 288 | + public function getCustomFilters(): array { |
| 289 | + return []; |
| 290 | + } |
231 | 291 | } |
0 commit comments