Skip to content

Commit

Permalink
Merge pull request #41374 from nextcloud/feat/search/search-event-att…
Browse files Browse the repository at this point in the history
…endees

feat(search): Seach for persons as event attendees and organizers
  • Loading branch information
ChristophWurst authored Nov 10, 2023
2 parents 78d5ec4 + 9b8cb5c commit 7556021
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 18 deletions.
92 changes: 76 additions & 16 deletions apps/dav/lib/Search/EventsSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,24 @@

use OCA\DAV\CalDAV\CalDavBackend;
use OCP\IUser;
use OCP\Search\IFilteringProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
use Sabre\VObject\Component;
use Sabre\VObject\DateTimeParser;
use Sabre\VObject\Property;
use function array_combine;
use function array_fill;
use function array_key_exists;
use function array_map;

/**
* Class EventsSearchProvider
*
* @package OCA\DAV\Search
*/
class EventsSearchProvider extends ACalendarSearchProvider {
class EventsSearchProvider extends ACalendarSearchProvider implements IFilteringProvider {
/**
* @var string[]
*/
Expand Down Expand Up @@ -106,22 +111,60 @@ public function search(
$calendarsById = $this->getSortedCalendars($principalUri);
$subscriptionsById = $this->getSortedSubscriptions($principalUri);

$searchResults = $this->backend->searchPrincipalUri(
$principalUri,
$query->getFilter('term')?->get() ?? '',
[self::$componentType],
self::$searchProperties,
self::$searchParameters,
[
'limit' => $query->getLimit(),
'offset' => $query->getCursor(),
'timerange' => [
'start' => $query->getFilter('since')?->get(),
'end' => $query->getFilter('until')?->get(),
/** @var string|null $term */
$term = $query->getFilter('term')?->get();
if ($term === null) {
$searchResults = [];
} else {
$searchResults = $this->backend->searchPrincipalUri(
$principalUri,
$term,
[self::$componentType],
self::$searchProperties,
self::$searchParameters,
[
'limit' => $query->getLimit(),
'offset' => $query->getCursor(),
'timerange' => [
'start' => $query->getFilter('since')?->get(),
'end' => $query->getFilter('until')?->get(),
],
]
);
}
/** @var IUser|null $person */
$person = $query->getFilter('person')?->get();
$personDisplayName = $person?->getDisplayName();
if ($personDisplayName !== null) {
$attendeeSearchResults = $this->backend->searchPrincipalUri(
$principalUri,
$personDisplayName,
[self::$componentType],
['ATTENDEE'],
self::$searchParameters,
[
'limit' => $query->getLimit(),
'offset' => $query->getCursor(),
'timerange' => [
'start' => $query->getFilter('since')?->get(),
'end' => $query->getFilter('until')?->get(),
],
],
]
);
$formattedResults = \array_map(function (array $eventRow) use ($calendarsById, $subscriptionsById):SearchResultEntry {
);

$searchResultIndex = array_combine(
array_map(fn($event) => $event['calendarid'] . '-' . $event['uri'], $searchResults),
array_fill(0, count($searchResults), null),
);
foreach ($attendeeSearchResults as $attendeeResult) {
if (array_key_exists($attendeeResult['calendarid'] . '-' . $attendeeResult['uri'], $searchResultIndex)) {
// Duplicate
continue;
}
$searchResults[] = $attendeeResult;
}
}
$formattedResults = \array_map(function (array $eventRow) use ($calendarsById, $subscriptionsById): SearchResultEntry {
$component = $this->getPrimaryComponent($eventRow['calendardata'], self::$componentType);
$title = (string)($component->SUMMARY ?? $this->l10n->t('Untitled event'));
$subline = $this->generateSubline($component);
Expand Down Expand Up @@ -228,4 +271,21 @@ protected function isDayEqual(
): bool {
return $dtStart->format('Y-m-d') === $dtEnd->format('Y-m-d');
}

public function getSupportedFilters(): array {
return [
'term',
'person',
'since',
'until',
];
}

public function getAlternateIds(): array {
return [];
}

public function getCustomFilters(): array {
return [];
}
}
12 changes: 10 additions & 2 deletions apps/dav/tests/unit/Search/EventsSearchProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IFilter;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
Expand Down Expand Up @@ -293,7 +294,14 @@ public function testSearch(): void {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('john.doe');
$query = $this->createMock(ISearchQuery::class);
$query->method('getTerm')->willReturn('search term');
$seachTermFilter = $this->createMock(IFilter::class);
$query->method('getFilter')->willReturnCallback(function($name) use ($seachTermFilter) {
return match ($name) {
'term' => $seachTermFilter,
default => null,
};
});
$seachTermFilter->method('get')->willReturn('search term');
$query->method('getLimit')->willReturn(5);
$query->method('getCursor')->willReturn(20);
$this->appManager->expects($this->once())
Expand Down Expand Up @@ -328,7 +336,7 @@ public function testSearch(): void {
]);
$this->backend->expects($this->once())
->method('searchPrincipalUri')
->with('principals/users/john.doe', '', ['VEVENT'],
->with('principals/users/john.doe', 'search term', ['VEVENT'],
['SUMMARY', 'LOCATION', 'DESCRIPTION', 'ATTENDEE', 'ORGANIZER', 'CATEGORIES'],
['ATTENDEE' => ['CN'], 'ORGANIZER' => ['CN']],
['limit' => 5, 'offset' => 20, 'timerange' => ['start' => null, 'end' => null]])
Expand Down

0 comments on commit 7556021

Please sign in to comment.