@@ -29,52 +29,52 @@ |
||
| 29 | 29 | * @since 13.0.0 |
| 30 | 30 | */ |
| 31 | 31 | interface ISearchResult { |
| 32 | - /** |
|
| 33 | - * @param SearchResultType $type |
|
| 34 | - * @param array $matches |
|
| 35 | - * @param array|null $exactMatches |
|
| 36 | - * @since 13.0.0 |
|
| 37 | - */ |
|
| 38 | - public function addResultSet(SearchResultType $type, array $matches, array $exactMatches = null); |
|
| 32 | + /** |
|
| 33 | + * @param SearchResultType $type |
|
| 34 | + * @param array $matches |
|
| 35 | + * @param array|null $exactMatches |
|
| 36 | + * @since 13.0.0 |
|
| 37 | + */ |
|
| 38 | + public function addResultSet(SearchResultType $type, array $matches, array $exactMatches = null); |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param SearchResultType $type |
|
| 42 | - * @param string $collaboratorId |
|
| 43 | - * @return bool |
|
| 44 | - * @since 13.0.0 |
|
| 45 | - */ |
|
| 46 | - public function hasResult(SearchResultType $type, $collaboratorId); |
|
| 40 | + /** |
|
| 41 | + * @param SearchResultType $type |
|
| 42 | + * @param string $collaboratorId |
|
| 43 | + * @return bool |
|
| 44 | + * @since 13.0.0 |
|
| 45 | + */ |
|
| 46 | + public function hasResult(SearchResultType $type, $collaboratorId); |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Removes all result where $collaborationId exactly matches shareWith of |
|
| 50 | - * any of wide and exact result matches of the given type. |
|
| 51 | - * |
|
| 52 | - * @since 22.0.0 |
|
| 53 | - */ |
|
| 54 | - public function removeCollaboratorResult(SearchResultType $type, string $collaboratorId): bool; |
|
| 48 | + /** |
|
| 49 | + * Removes all result where $collaborationId exactly matches shareWith of |
|
| 50 | + * any of wide and exact result matches of the given type. |
|
| 51 | + * |
|
| 52 | + * @since 22.0.0 |
|
| 53 | + */ |
|
| 54 | + public function removeCollaboratorResult(SearchResultType $type, string $collaboratorId): bool; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @param SearchResultType $type |
|
| 58 | - * @since 13.0.0 |
|
| 59 | - */ |
|
| 60 | - public function unsetResult(SearchResultType $type); |
|
| 56 | + /** |
|
| 57 | + * @param SearchResultType $type |
|
| 58 | + * @since 13.0.0 |
|
| 59 | + */ |
|
| 60 | + public function unsetResult(SearchResultType $type); |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @param SearchResultType $type |
|
| 64 | - * @since 13.0.0 |
|
| 65 | - */ |
|
| 66 | - public function markExactIdMatch(SearchResultType $type); |
|
| 62 | + /** |
|
| 63 | + * @param SearchResultType $type |
|
| 64 | + * @since 13.0.0 |
|
| 65 | + */ |
|
| 66 | + public function markExactIdMatch(SearchResultType $type); |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * @param SearchResultType $type |
|
| 70 | - * @return bool |
|
| 71 | - * @since 13.0.0 |
|
| 72 | - */ |
|
| 73 | - public function hasExactIdMatch(SearchResultType $type); |
|
| 68 | + /** |
|
| 69 | + * @param SearchResultType $type |
|
| 70 | + * @return bool |
|
| 71 | + * @since 13.0.0 |
|
| 72 | + */ |
|
| 73 | + public function hasExactIdMatch(SearchResultType $type); |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @return array |
|
| 77 | - * @since 13.0.0 |
|
| 78 | - */ |
|
| 79 | - public function asArray(); |
|
| 75 | + /** |
|
| 76 | + * @return array |
|
| 77 | + * @since 13.0.0 |
|
| 78 | + */ |
|
| 79 | + public function asArray(); |
|
| 80 | 80 | } |
@@ -35,111 +35,111 @@ |
||
| 35 | 35 | use OCP\Share; |
| 36 | 36 | |
| 37 | 37 | class Search implements ISearch { |
| 38 | - /** @var IContainer */ |
|
| 39 | - private $c; |
|
| 40 | - |
|
| 41 | - protected $pluginList = []; |
|
| 42 | - |
|
| 43 | - public function __construct(IContainer $c) { |
|
| 44 | - $this->c = $c; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * @param string $search |
|
| 49 | - * @param array $shareTypes |
|
| 50 | - * @param bool $lookup |
|
| 51 | - * @param int|null $limit |
|
| 52 | - * @param int|null $offset |
|
| 53 | - * @return array |
|
| 54 | - * @throws \OCP\AppFramework\QueryException |
|
| 55 | - */ |
|
| 56 | - public function search($search, array $shareTypes, $lookup, $limit, $offset) { |
|
| 57 | - $hasMoreResults = false; |
|
| 58 | - |
|
| 59 | - // Trim leading and trailing whitespace characters, e.g. when query is copy-pasted |
|
| 60 | - $search = trim($search); |
|
| 61 | - |
|
| 62 | - /** @var ISearchResult $searchResult */ |
|
| 63 | - $searchResult = $this->c->resolve(SearchResult::class); |
|
| 64 | - |
|
| 65 | - foreach ($shareTypes as $type) { |
|
| 66 | - if (!isset($this->pluginList[$type])) { |
|
| 67 | - continue; |
|
| 68 | - } |
|
| 69 | - foreach ($this->pluginList[$type] as $plugin) { |
|
| 70 | - /** @var ISearchPlugin $searchPlugin */ |
|
| 71 | - $searchPlugin = $this->c->resolve($plugin); |
|
| 72 | - $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults; |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // Get from lookup server, not a separate share type |
|
| 77 | - if ($lookup) { |
|
| 78 | - $searchPlugin = $this->c->resolve(LookupPlugin::class); |
|
| 79 | - $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - // sanitizing, could go into the plugins as well |
|
| 83 | - |
|
| 84 | - // if we have an exact match, either for the federated cloud id or for the |
|
| 85 | - // email address, we only return the exact match. It is highly unlikely |
|
| 86 | - // that the exact same email address and federated cloud id exists |
|
| 87 | - $emailType = new SearchResultType('emails'); |
|
| 88 | - $remoteType = new SearchResultType('remotes'); |
|
| 89 | - if ($searchResult->hasExactIdMatch($emailType) && !$searchResult->hasExactIdMatch($remoteType)) { |
|
| 90 | - $searchResult->unsetResult($remoteType); |
|
| 91 | - } elseif (!$searchResult->hasExactIdMatch($emailType) && $searchResult->hasExactIdMatch($remoteType)) { |
|
| 92 | - $searchResult->unsetResult($emailType); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $this->dropMailSharesWhereRemoteShareIsPossible($searchResult); |
|
| 96 | - |
|
| 97 | - // if we have an exact local user match with an email-a-like query, |
|
| 98 | - // there is no need to show the remote and email matches. |
|
| 99 | - $userType = new SearchResultType('users'); |
|
| 100 | - if (strpos($search, '@') !== false && $searchResult->hasExactIdMatch($userType)) { |
|
| 101 | - $searchResult->unsetResult($remoteType); |
|
| 102 | - $searchResult->unsetResult($emailType); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - return [$searchResult->asArray(), $hasMoreResults]; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function registerPlugin(array $pluginInfo) { |
|
| 109 | - $shareType = constant(Share::class . '::' . $pluginInfo['shareType']); |
|
| 110 | - if ($shareType === null) { |
|
| 111 | - throw new \InvalidArgumentException('Provided ShareType is invalid'); |
|
| 112 | - } |
|
| 113 | - $this->pluginList[$shareType][] = $pluginInfo['class']; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - protected function dropMailSharesWhereRemoteShareIsPossible(ISearchResult $searchResult): void { |
|
| 117 | - $allResults = $searchResult->asArray(); |
|
| 118 | - |
|
| 119 | - $emailType = new SearchResultType('emails'); |
|
| 120 | - $remoteType = new SearchResultType('remotes'); |
|
| 121 | - |
|
| 122 | - if (!isset($allResults[$remoteType->getLabel()]) |
|
| 123 | - || !isset($allResults[$emailType->getLabel()])) { |
|
| 124 | - return; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $mailIdMap = []; |
|
| 128 | - foreach ($allResults[$emailType->getLabel()] as $mailRow) { |
|
| 129 | - // sure, array_reduce looks nicer, but foreach needs less resources and is faster |
|
| 130 | - if (!isset($mailRow['uuid'])) { |
|
| 131 | - continue; |
|
| 132 | - } |
|
| 133 | - $mailIdMap[$mailRow['uuid']] = $mailRow['value']['shareWith']; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - foreach ($allResults[$remoteType->getLabel()] as $resultRow) { |
|
| 137 | - if (!isset($resultRow['uuid'])) { |
|
| 138 | - continue; |
|
| 139 | - } |
|
| 140 | - if (isset($mailIdMap[$resultRow['uuid']])) { |
|
| 141 | - $searchResult->removeCollaboratorResult($emailType, $mailIdMap[$resultRow['uuid']]); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - } |
|
| 38 | + /** @var IContainer */ |
|
| 39 | + private $c; |
|
| 40 | + |
|
| 41 | + protected $pluginList = []; |
|
| 42 | + |
|
| 43 | + public function __construct(IContainer $c) { |
|
| 44 | + $this->c = $c; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * @param string $search |
|
| 49 | + * @param array $shareTypes |
|
| 50 | + * @param bool $lookup |
|
| 51 | + * @param int|null $limit |
|
| 52 | + * @param int|null $offset |
|
| 53 | + * @return array |
|
| 54 | + * @throws \OCP\AppFramework\QueryException |
|
| 55 | + */ |
|
| 56 | + public function search($search, array $shareTypes, $lookup, $limit, $offset) { |
|
| 57 | + $hasMoreResults = false; |
|
| 58 | + |
|
| 59 | + // Trim leading and trailing whitespace characters, e.g. when query is copy-pasted |
|
| 60 | + $search = trim($search); |
|
| 61 | + |
|
| 62 | + /** @var ISearchResult $searchResult */ |
|
| 63 | + $searchResult = $this->c->resolve(SearchResult::class); |
|
| 64 | + |
|
| 65 | + foreach ($shareTypes as $type) { |
|
| 66 | + if (!isset($this->pluginList[$type])) { |
|
| 67 | + continue; |
|
| 68 | + } |
|
| 69 | + foreach ($this->pluginList[$type] as $plugin) { |
|
| 70 | + /** @var ISearchPlugin $searchPlugin */ |
|
| 71 | + $searchPlugin = $this->c->resolve($plugin); |
|
| 72 | + $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults; |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // Get from lookup server, not a separate share type |
|
| 77 | + if ($lookup) { |
|
| 78 | + $searchPlugin = $this->c->resolve(LookupPlugin::class); |
|
| 79 | + $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + // sanitizing, could go into the plugins as well |
|
| 83 | + |
|
| 84 | + // if we have an exact match, either for the federated cloud id or for the |
|
| 85 | + // email address, we only return the exact match. It is highly unlikely |
|
| 86 | + // that the exact same email address and federated cloud id exists |
|
| 87 | + $emailType = new SearchResultType('emails'); |
|
| 88 | + $remoteType = new SearchResultType('remotes'); |
|
| 89 | + if ($searchResult->hasExactIdMatch($emailType) && !$searchResult->hasExactIdMatch($remoteType)) { |
|
| 90 | + $searchResult->unsetResult($remoteType); |
|
| 91 | + } elseif (!$searchResult->hasExactIdMatch($emailType) && $searchResult->hasExactIdMatch($remoteType)) { |
|
| 92 | + $searchResult->unsetResult($emailType); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $this->dropMailSharesWhereRemoteShareIsPossible($searchResult); |
|
| 96 | + |
|
| 97 | + // if we have an exact local user match with an email-a-like query, |
|
| 98 | + // there is no need to show the remote and email matches. |
|
| 99 | + $userType = new SearchResultType('users'); |
|
| 100 | + if (strpos($search, '@') !== false && $searchResult->hasExactIdMatch($userType)) { |
|
| 101 | + $searchResult->unsetResult($remoteType); |
|
| 102 | + $searchResult->unsetResult($emailType); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + return [$searchResult->asArray(), $hasMoreResults]; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function registerPlugin(array $pluginInfo) { |
|
| 109 | + $shareType = constant(Share::class . '::' . $pluginInfo['shareType']); |
|
| 110 | + if ($shareType === null) { |
|
| 111 | + throw new \InvalidArgumentException('Provided ShareType is invalid'); |
|
| 112 | + } |
|
| 113 | + $this->pluginList[$shareType][] = $pluginInfo['class']; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + protected function dropMailSharesWhereRemoteShareIsPossible(ISearchResult $searchResult): void { |
|
| 117 | + $allResults = $searchResult->asArray(); |
|
| 118 | + |
|
| 119 | + $emailType = new SearchResultType('emails'); |
|
| 120 | + $remoteType = new SearchResultType('remotes'); |
|
| 121 | + |
|
| 122 | + if (!isset($allResults[$remoteType->getLabel()]) |
|
| 123 | + || !isset($allResults[$emailType->getLabel()])) { |
|
| 124 | + return; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $mailIdMap = []; |
|
| 128 | + foreach ($allResults[$emailType->getLabel()] as $mailRow) { |
|
| 129 | + // sure, array_reduce looks nicer, but foreach needs less resources and is faster |
|
| 130 | + if (!isset($mailRow['uuid'])) { |
|
| 131 | + continue; |
|
| 132 | + } |
|
| 133 | + $mailIdMap[$mailRow['uuid']] = $mailRow['value']['shareWith']; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + foreach ($allResults[$remoteType->getLabel()] as $resultRow) { |
|
| 137 | + if (!isset($resultRow['uuid'])) { |
|
| 138 | + continue; |
|
| 139 | + } |
|
| 140 | + if (isset($mailIdMap[$resultRow['uuid']])) { |
|
| 141 | + $searchResult->removeCollaboratorResult($emailType, $mailIdMap[$resultRow['uuid']]); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | 145 | } |
@@ -29,80 +29,80 @@ |
||
| 29 | 29 | use OCP\Collaboration\Collaborators\SearchResultType; |
| 30 | 30 | |
| 31 | 31 | class SearchResult implements ISearchResult { |
| 32 | - protected $result = [ |
|
| 33 | - 'exact' => [], |
|
| 34 | - ]; |
|
| 35 | - |
|
| 36 | - protected $exactIdMatches = []; |
|
| 37 | - |
|
| 38 | - public function addResultSet(SearchResultType $type, array $matches, array $exactMatches = null) { |
|
| 39 | - $type = $type->getLabel(); |
|
| 40 | - if (!isset($this->result[$type])) { |
|
| 41 | - $this->result[$type] = []; |
|
| 42 | - $this->result['exact'][$type] = []; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - $this->result[$type] = array_merge($this->result[$type], $matches); |
|
| 46 | - if (is_array($exactMatches)) { |
|
| 47 | - $this->result['exact'][$type] = array_merge($this->result['exact'][$type], $exactMatches); |
|
| 48 | - } |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - public function markExactIdMatch(SearchResultType $type) { |
|
| 52 | - $this->exactIdMatches[$type->getLabel()] = 1; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - public function hasExactIdMatch(SearchResultType $type) { |
|
| 56 | - return isset($this->exactIdMatches[$type->getLabel()]); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - public function hasResult(SearchResultType $type, $collaboratorId) { |
|
| 60 | - $type = $type->getLabel(); |
|
| 61 | - if (!isset($this->result[$type])) { |
|
| 62 | - return false; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - $resultArrays = [$this->result['exact'][$type], $this->result[$type]]; |
|
| 66 | - foreach ($resultArrays as $resultArray) { |
|
| 67 | - foreach ($resultArray as $result) { |
|
| 68 | - if ($result['value']['shareWith'] === $collaboratorId) { |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - return false; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function asArray() { |
|
| 78 | - return $this->result; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function unsetResult(SearchResultType $type) { |
|
| 82 | - $type = $type->getLabel(); |
|
| 83 | - $this->result[$type] = []; |
|
| 84 | - if (isset($this->result['exact'][$type])) { |
|
| 85 | - $this->result['exact'][$type] = []; |
|
| 86 | - } |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function removeCollaboratorResult(SearchResultType $type, string $collaboratorId): bool { |
|
| 90 | - $type = $type->getLabel(); |
|
| 91 | - if (!isset($this->result[$type])) { |
|
| 92 | - return false; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $actionDone = false; |
|
| 96 | - $resultArrays = [&$this->result['exact'][$type], &$this->result[$type]]; |
|
| 97 | - foreach ($resultArrays as &$resultArray) { |
|
| 98 | - foreach ($resultArray as $k => $result) { |
|
| 99 | - if ($result['value']['shareWith'] === $collaboratorId) { |
|
| 100 | - unset($resultArray[$k]); |
|
| 101 | - $actionDone = true; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return $actionDone; |
|
| 107 | - } |
|
| 32 | + protected $result = [ |
|
| 33 | + 'exact' => [], |
|
| 34 | + ]; |
|
| 35 | + |
|
| 36 | + protected $exactIdMatches = []; |
|
| 37 | + |
|
| 38 | + public function addResultSet(SearchResultType $type, array $matches, array $exactMatches = null) { |
|
| 39 | + $type = $type->getLabel(); |
|
| 40 | + if (!isset($this->result[$type])) { |
|
| 41 | + $this->result[$type] = []; |
|
| 42 | + $this->result['exact'][$type] = []; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + $this->result[$type] = array_merge($this->result[$type], $matches); |
|
| 46 | + if (is_array($exactMatches)) { |
|
| 47 | + $this->result['exact'][$type] = array_merge($this->result['exact'][$type], $exactMatches); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + public function markExactIdMatch(SearchResultType $type) { |
|
| 52 | + $this->exactIdMatches[$type->getLabel()] = 1; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + public function hasExactIdMatch(SearchResultType $type) { |
|
| 56 | + return isset($this->exactIdMatches[$type->getLabel()]); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + public function hasResult(SearchResultType $type, $collaboratorId) { |
|
| 60 | + $type = $type->getLabel(); |
|
| 61 | + if (!isset($this->result[$type])) { |
|
| 62 | + return false; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + $resultArrays = [$this->result['exact'][$type], $this->result[$type]]; |
|
| 66 | + foreach ($resultArrays as $resultArray) { |
|
| 67 | + foreach ($resultArray as $result) { |
|
| 68 | + if ($result['value']['shareWith'] === $collaboratorId) { |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + return false; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function asArray() { |
|
| 78 | + return $this->result; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function unsetResult(SearchResultType $type) { |
|
| 82 | + $type = $type->getLabel(); |
|
| 83 | + $this->result[$type] = []; |
|
| 84 | + if (isset($this->result['exact'][$type])) { |
|
| 85 | + $this->result['exact'][$type] = []; |
|
| 86 | + } |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function removeCollaboratorResult(SearchResultType $type, string $collaboratorId): bool { |
|
| 90 | + $type = $type->getLabel(); |
|
| 91 | + if (!isset($this->result[$type])) { |
|
| 92 | + return false; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $actionDone = false; |
|
| 96 | + $resultArrays = [&$this->result['exact'][$type], &$this->result[$type]]; |
|
| 97 | + foreach ($resultArrays as &$resultArray) { |
|
| 98 | + foreach ($resultArray as $k => $result) { |
|
| 99 | + if ($result['value']['shareWith'] === $collaboratorId) { |
|
| 100 | + unset($resultArray[$k]); |
|
| 101 | + $actionDone = true; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return $actionDone; |
|
| 107 | + } |
|
| 108 | 108 | } |