Search.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author onehappycat <one.happy.cat@gmx.com>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OC\Collaboration\Collaborators;
  29. use OCP\Collaboration\Collaborators\ISearch;
  30. use OCP\Collaboration\Collaborators\ISearchPlugin;
  31. use OCP\Collaboration\Collaborators\ISearchResult;
  32. use OCP\Collaboration\Collaborators\SearchResultType;
  33. use OCP\IContainer;
  34. use OCP\Share;
  35. class Search implements ISearch {
  36. protected array $pluginList = [];
  37. public function __construct(
  38. private IContainer $container,
  39. ) {
  40. }
  41. /**
  42. * @param string $search
  43. * @param bool $lookup
  44. * @param int|null $limit
  45. * @param int|null $offset
  46. * @throws \OCP\AppFramework\QueryException
  47. */
  48. public function search($search, array $shareTypes, $lookup, $limit, $offset): array {
  49. $hasMoreResults = false;
  50. // Trim leading and trailing whitespace characters, e.g. when query is copy-pasted
  51. $search = trim($search);
  52. /** @var ISearchResult $searchResult */
  53. $searchResult = $this->container->resolve(SearchResult::class);
  54. foreach ($shareTypes as $type) {
  55. if (!isset($this->pluginList[$type])) {
  56. continue;
  57. }
  58. foreach ($this->pluginList[$type] as $plugin) {
  59. /** @var ISearchPlugin $searchPlugin */
  60. $searchPlugin = $this->container->resolve($plugin);
  61. $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
  62. }
  63. }
  64. // Get from lookup server, not a separate share type
  65. if ($lookup) {
  66. $searchPlugin = $this->container->resolve(LookupPlugin::class);
  67. $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
  68. }
  69. // sanitizing, could go into the plugins as well
  70. // if we have an exact match, either for the federated cloud id or for the
  71. // email address, we only return the exact match. It is highly unlikely
  72. // that the exact same email address and federated cloud id exists
  73. $emailType = new SearchResultType('emails');
  74. $remoteType = new SearchResultType('remotes');
  75. if ($searchResult->hasExactIdMatch($emailType) && !$searchResult->hasExactIdMatch($remoteType)) {
  76. $searchResult->unsetResult($remoteType);
  77. } elseif (!$searchResult->hasExactIdMatch($emailType) && $searchResult->hasExactIdMatch($remoteType)) {
  78. $searchResult->unsetResult($emailType);
  79. }
  80. $this->dropMailSharesWhereRemoteShareIsPossible($searchResult);
  81. // if we have an exact local user match with an email-a-like query,
  82. // there is no need to show the remote and email matches.
  83. $userType = new SearchResultType('users');
  84. if (str_contains($search, '@') && $searchResult->hasExactIdMatch($userType)) {
  85. $searchResult->unsetResult($remoteType);
  86. $searchResult->unsetResult($emailType);
  87. }
  88. return [$searchResult->asArray(), $hasMoreResults];
  89. }
  90. public function registerPlugin(array $pluginInfo): void {
  91. $shareType = constant(Share::class . '::' . $pluginInfo['shareType']);
  92. if ($shareType === null) {
  93. throw new \InvalidArgumentException('Provided ShareType is invalid');
  94. }
  95. $this->pluginList[$shareType][] = $pluginInfo['class'];
  96. }
  97. protected function dropMailSharesWhereRemoteShareIsPossible(ISearchResult $searchResult): void {
  98. $allResults = $searchResult->asArray();
  99. $emailType = new SearchResultType('emails');
  100. $remoteType = new SearchResultType('remotes');
  101. if (!isset($allResults[$remoteType->getLabel()])
  102. || !isset($allResults[$emailType->getLabel()])) {
  103. return;
  104. }
  105. $mailIdMap = [];
  106. foreach ($allResults[$emailType->getLabel()] as $mailRow) {
  107. // sure, array_reduce looks nicer, but foreach needs less resources and is faster
  108. if (!isset($mailRow['uuid'])) {
  109. continue;
  110. }
  111. $mailIdMap[$mailRow['uuid']] = $mailRow['value']['shareWith'];
  112. }
  113. foreach ($allResults[$remoteType->getLabel()] as $resultRow) {
  114. if (!isset($resultRow['uuid'])) {
  115. continue;
  116. }
  117. if (isset($mailIdMap[$resultRow['uuid']])) {
  118. $searchResult->removeCollaboratorResult($emailType, $mailIdMap[$resultRow['uuid']]);
  119. }
  120. }
  121. }
  122. }