-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathSearch.php
More file actions
127 lines (107 loc) · 4.24 KB
/
Search.php
File metadata and controls
127 lines (107 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Collaboration\Collaborators;
use OC\Core\AppInfo\ConfigLexicon;
use OCP\AppFramework\QueryException;
use OCP\Collaboration\Collaborators\ISearch;
use OCP\Collaboration\Collaborators\ISearchPlugin;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\IAppConfig;
use OCP\IContainer;
use OCP\Share\IShare;
class Search implements ISearch {
protected array $pluginList = [];
public function __construct(
private IContainer $container,
private readonly IAppConfig $appConfig,
) {
}
/**
* @param string $search
* @param bool $lookup
* @param int|null $limit
* @param int|null $offset
* @throws QueryException
*/
public function search($search, array $shareTypes, $lookup, $limit, $offset): array {
$hasMoreResults = false;
// Trim leading and trailing whitespace characters, e.g. when query is copy-pasted
$search = trim($search);
/** @var ISearchResult $searchResult */
$searchResult = $this->container->resolve(SearchResult::class);
foreach ($shareTypes as $type) {
if (!isset($this->pluginList[$type])) {
continue;
}
foreach ($this->pluginList[$type] as $plugin) {
/** @var ISearchPlugin $searchPlugin */
$searchPlugin = $this->container->resolve($plugin);
if ($searchPlugin instanceof UserPlugin && $lookup && $this->appConfig->getValueBool('core', ConfigLexicon::LOOKUP_LOCAL_ACCOUNT_SEARCH)) {
continue;
}
$hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
}
}
// Get from lookup server, not a separate share type
if ($lookup) {
$searchPlugin = $this->container->resolve(LookupPlugin::class);
$hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
}
// sanitizing, could go into the plugins as well
// 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
// that the exact same email address and federated cloud id exists
$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);
}
$this->dropMailSharesWhereRemoteShareIsPossible($searchResult);
// 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.
$userType = new SearchResultType('users');
if (str_contains($search, '@') && $searchResult->hasExactIdMatch($userType)) {
$searchResult->unsetResult($remoteType);
$searchResult->unsetResult($emailType);
}
return [$searchResult->asArray(), $hasMoreResults];
}
public function registerPlugin(array $pluginInfo): void {
$shareType = constant(IShare::class . '::' . substr($pluginInfo['shareType'], strlen('SHARE_')));
if ($shareType === null) {
throw new \InvalidArgumentException('Provided ShareType is invalid');
}
$this->pluginList[$shareType][] = $pluginInfo['class'];
}
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']]);
}
}
}
}