2017-08-31 16:47:02 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2017-08-31 16:47:02 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-08-31 16:47:02 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Collaboration\Collaborators;
|
|
|
|
|
|
|
|
|
|
use OCP\Collaboration\Collaborators\ISearch;
|
|
|
|
|
use OCP\Collaboration\Collaborators\ISearchPlugin;
|
|
|
|
|
use OCP\Collaboration\Collaborators\ISearchResult;
|
2017-09-06 10:09:29 -04:00
|
|
|
use OCP\Collaboration\Collaborators\SearchResultType;
|
2017-08-31 16:47:02 -04:00
|
|
|
use OCP\IContainer;
|
2025-07-15 05:38:42 -04:00
|
|
|
use OCP\Share\IShare;
|
2017-08-31 16:47:02 -04:00
|
|
|
|
|
|
|
|
class Search implements ISearch {
|
2023-07-03 03:36:55 -04:00
|
|
|
protected array $pluginList = [];
|
2017-08-31 16:47:02 -04:00
|
|
|
|
2023-07-03 03:36:55 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private IContainer $container,
|
|
|
|
|
) {
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
2018-01-17 07:49:18 -05:00
|
|
|
/**
|
2018-01-17 07:58:42 -05:00
|
|
|
* @param string $search
|
|
|
|
|
* @param bool $lookup
|
|
|
|
|
* @param int|null $limit
|
|
|
|
|
* @param int|null $offset
|
2018-01-17 07:49:18 -05:00
|
|
|
* @throws \OCP\AppFramework\QueryException
|
|
|
|
|
*/
|
2023-07-03 03:36:55 -04:00
|
|
|
public function search($search, array $shareTypes, $lookup, $limit, $offset): array {
|
2017-08-31 16:47:02 -04:00
|
|
|
$hasMoreResults = false;
|
|
|
|
|
|
2020-07-23 16:20:18 -04:00
|
|
|
// Trim leading and trailing whitespace characters, e.g. when query is copy-pasted
|
|
|
|
|
$search = trim($search);
|
|
|
|
|
|
2017-08-31 16:47:02 -04:00
|
|
|
/** @var ISearchResult $searchResult */
|
2023-07-03 03:36:55 -04:00
|
|
|
$searchResult = $this->container->resolve(SearchResult::class);
|
2017-08-31 16:47:02 -04:00
|
|
|
|
|
|
|
|
foreach ($shareTypes as $type) {
|
2017-09-06 15:57:00 -04:00
|
|
|
if (!isset($this->pluginList[$type])) {
|
2017-08-31 16:47:02 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2017-09-06 16:51:18 -04:00
|
|
|
foreach ($this->pluginList[$type] as $plugin) {
|
|
|
|
|
/** @var ISearchPlugin $searchPlugin */
|
2023-07-03 03:36:55 -04:00
|
|
|
$searchPlugin = $this->container->resolve($plugin);
|
2020-10-14 07:00:20 -04:00
|
|
|
$hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
|
2017-09-06 16:51:18 -04:00
|
|
|
}
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get from lookup server, not a separate share type
|
|
|
|
|
if ($lookup) {
|
2023-07-03 03:36:55 -04:00
|
|
|
$searchPlugin = $this->container->resolve(LookupPlugin::class);
|
2020-10-14 07:00:20 -04:00
|
|
|
$hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sanitizing, could go into the plugins as well
|
|
|
|
|
|
2021-04-13 16:49:42 -04:00
|
|
|
// if we have an exact match, either for the federated cloud id or for the
|
|
|
|
|
// email address, we only return the exact match. It is highly unlikely
|
2017-08-31 16:47:02 -04:00
|
|
|
// that the exact same email address and federated cloud id exists
|
2017-09-06 10:09:29 -04:00
|
|
|
$emailType = new SearchResultType('emails');
|
|
|
|
|
$remoteType = new SearchResultType('remotes');
|
|
|
|
|
if ($searchResult->hasExactIdMatch($emailType) && !$searchResult->hasExactIdMatch($remoteType)) {
|
|
|
|
|
$searchResult->unsetResult($remoteType);
|
|
|
|
|
} elseif (!$searchResult->hasExactIdMatch($emailType) && $searchResult->hasExactIdMatch($remoteType)) {
|
|
|
|
|
$searchResult->unsetResult($emailType);
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:49:42 -04:00
|
|
|
$this->dropMailSharesWhereRemoteShareIsPossible($searchResult);
|
|
|
|
|
|
2020-09-23 04:21:55 -04:00
|
|
|
// if we have an exact local user match with an email-a-like query,
|
|
|
|
|
// there is no need to show the remote and email matches.
|
2019-06-21 09:38:19 -04:00
|
|
|
$userType = new SearchResultType('users');
|
2023-05-15 07:47:19 -04:00
|
|
|
if (str_contains($search, '@') && $searchResult->hasExactIdMatch($userType)) {
|
2019-06-21 09:38:19 -04:00
|
|
|
$searchResult->unsetResult($remoteType);
|
|
|
|
|
$searchResult->unsetResult($emailType);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 06:57:03 -05:00
|
|
|
return [$searchResult->asArray(), $hasMoreResults];
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|
2017-09-06 15:57:00 -04:00
|
|
|
|
2023-07-03 03:36:55 -04:00
|
|
|
public function registerPlugin(array $pluginInfo): void {
|
2025-07-15 05:38:42 -04:00
|
|
|
$shareType = constant(IShare::class . '::' . substr($pluginInfo['shareType'], strlen('SHARE_')));
|
2017-09-06 15:57:00 -04:00
|
|
|
if ($shareType === null) {
|
|
|
|
|
throw new \InvalidArgumentException('Provided ShareType is invalid');
|
|
|
|
|
}
|
2017-09-06 16:51:18 -04:00
|
|
|
$this->pluginList[$shareType][] = $pluginInfo['class'];
|
2017-09-06 15:57:00 -04:00
|
|
|
}
|
2021-04-13 16:49:42 -04:00
|
|
|
|
|
|
|
|
protected function dropMailSharesWhereRemoteShareIsPossible(ISearchResult $searchResult): void {
|
|
|
|
|
$allResults = $searchResult->asArray();
|
|
|
|
|
|
|
|
|
|
$emailType = new SearchResultType('emails');
|
|
|
|
|
$remoteType = new SearchResultType('remotes');
|
|
|
|
|
|
|
|
|
|
if (!isset($allResults[$remoteType->getLabel()])
|
|
|
|
|
|| !isset($allResults[$emailType->getLabel()])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mailIdMap = [];
|
|
|
|
|
foreach ($allResults[$emailType->getLabel()] as $mailRow) {
|
|
|
|
|
// sure, array_reduce looks nicer, but foreach needs less resources and is faster
|
|
|
|
|
if (!isset($mailRow['uuid'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$mailIdMap[$mailRow['uuid']] = $mailRow['value']['shareWith'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($allResults[$remoteType->getLabel()] as $resultRow) {
|
|
|
|
|
if (!isset($resultRow['uuid'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (isset($mailIdMap[$resultRow['uuid']])) {
|
|
|
|
|
$searchResult->removeCollaboratorResult($emailType, $mailIdMap[$resultRow['uuid']]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|