Search.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Collaboration\Collaborators;
  7. use OCP\Collaboration\Collaborators\ISearch;
  8. use OCP\Collaboration\Collaborators\ISearchPlugin;
  9. use OCP\Collaboration\Collaborators\ISearchResult;
  10. use OCP\Collaboration\Collaborators\SearchResultType;
  11. use OCP\IContainer;
  12. use OCP\Share;
  13. class Search implements ISearch {
  14. protected array $pluginList = [];
  15. public function __construct(
  16. private IContainer $container,
  17. ) {
  18. }
  19. /**
  20. * @param string $search
  21. * @param bool $lookup
  22. * @param int|null $limit
  23. * @param int|null $offset
  24. * @throws \OCP\AppFramework\QueryException
  25. */
  26. public function search($search, array $shareTypes, $lookup, $limit, $offset): array {
  27. $hasMoreResults = false;
  28. // Trim leading and trailing whitespace characters, e.g. when query is copy-pasted
  29. $search = trim($search);
  30. /** @var ISearchResult $searchResult */
  31. $searchResult = $this->container->resolve(SearchResult::class);
  32. foreach ($shareTypes as $type) {
  33. if (!isset($this->pluginList[$type])) {
  34. continue;
  35. }
  36. foreach ($this->pluginList[$type] as $plugin) {
  37. /** @var ISearchPlugin $searchPlugin */
  38. $searchPlugin = $this->container->resolve($plugin);
  39. $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
  40. }
  41. }
  42. // Get from lookup server, not a separate share type
  43. if ($lookup) {
  44. $searchPlugin = $this->container->resolve(LookupPlugin::class);
  45. $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
  46. }
  47. // sanitizing, could go into the plugins as well
  48. // if we have an exact match, either for the federated cloud id or for the
  49. // email address, we only return the exact match. It is highly unlikely
  50. // that the exact same email address and federated cloud id exists
  51. $emailType = new SearchResultType('emails');
  52. $remoteType = new SearchResultType('remotes');
  53. if ($searchResult->hasExactIdMatch($emailType) && !$searchResult->hasExactIdMatch($remoteType)) {
  54. $searchResult->unsetResult($remoteType);
  55. } elseif (!$searchResult->hasExactIdMatch($emailType) && $searchResult->hasExactIdMatch($remoteType)) {
  56. $searchResult->unsetResult($emailType);
  57. }
  58. $this->dropMailSharesWhereRemoteShareIsPossible($searchResult);
  59. // if we have an exact local user match with an email-a-like query,
  60. // there is no need to show the remote and email matches.
  61. $userType = new SearchResultType('users');
  62. if (str_contains($search, '@') && $searchResult->hasExactIdMatch($userType)) {
  63. $searchResult->unsetResult($remoteType);
  64. $searchResult->unsetResult($emailType);
  65. }
  66. return [$searchResult->asArray(), $hasMoreResults];
  67. }
  68. public function registerPlugin(array $pluginInfo): void {
  69. $shareType = constant(Share::class . '::' . $pluginInfo['shareType']);
  70. if ($shareType === null) {
  71. throw new \InvalidArgumentException('Provided ShareType is invalid');
  72. }
  73. $this->pluginList[$shareType][] = $pluginInfo['class'];
  74. }
  75. protected function dropMailSharesWhereRemoteShareIsPossible(ISearchResult $searchResult): void {
  76. $allResults = $searchResult->asArray();
  77. $emailType = new SearchResultType('emails');
  78. $remoteType = new SearchResultType('remotes');
  79. if (!isset($allResults[$remoteType->getLabel()])
  80. || !isset($allResults[$emailType->getLabel()])) {
  81. return;
  82. }
  83. $mailIdMap = [];
  84. foreach ($allResults[$emailType->getLabel()] as $mailRow) {
  85. // sure, array_reduce looks nicer, but foreach needs less resources and is faster
  86. if (!isset($mailRow['uuid'])) {
  87. continue;
  88. }
  89. $mailIdMap[$mailRow['uuid']] = $mailRow['value']['shareWith'];
  90. }
  91. foreach ($allResults[$remoteType->getLabel()] as $resultRow) {
  92. if (!isset($resultRow['uuid'])) {
  93. continue;
  94. }
  95. if (isset($mailIdMap[$resultRow['uuid']])) {
  96. $searchResult->removeCollaboratorResult($emailType, $mailIdMap[$resultRow['uuid']]);
  97. }
  98. }
  99. }
  100. }