Search.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /** @var IContainer */
  37. private $c;
  38. protected $pluginList = [];
  39. public function __construct(IContainer $c) {
  40. $this->c = $c;
  41. }
  42. /**
  43. * @param string $search
  44. * @param array $shareTypes
  45. * @param bool $lookup
  46. * @param int|null $limit
  47. * @param int|null $offset
  48. * @return array
  49. * @throws \OCP\AppFramework\QueryException
  50. */
  51. public function search($search, array $shareTypes, $lookup, $limit, $offset) {
  52. $hasMoreResults = false;
  53. // Trim leading and trailing whitespace characters, e.g. when query is copy-pasted
  54. $search = trim($search);
  55. /** @var ISearchResult $searchResult */
  56. $searchResult = $this->c->resolve(SearchResult::class);
  57. foreach ($shareTypes as $type) {
  58. if (!isset($this->pluginList[$type])) {
  59. continue;
  60. }
  61. foreach ($this->pluginList[$type] as $plugin) {
  62. /** @var ISearchPlugin $searchPlugin */
  63. $searchPlugin = $this->c->resolve($plugin);
  64. $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
  65. }
  66. }
  67. // Get from lookup server, not a separate share type
  68. if ($lookup) {
  69. $searchPlugin = $this->c->resolve(LookupPlugin::class);
  70. $hasMoreResults = $searchPlugin->search($search, $limit, $offset, $searchResult) || $hasMoreResults;
  71. }
  72. // sanitizing, could go into the plugins as well
  73. // if we have an exact match, either for the federated cloud id or for the
  74. // email address, we only return the exact match. It is highly unlikely
  75. // that the exact same email address and federated cloud id exists
  76. $emailType = new SearchResultType('emails');
  77. $remoteType = new SearchResultType('remotes');
  78. if ($searchResult->hasExactIdMatch($emailType) && !$searchResult->hasExactIdMatch($remoteType)) {
  79. $searchResult->unsetResult($remoteType);
  80. } elseif (!$searchResult->hasExactIdMatch($emailType) && $searchResult->hasExactIdMatch($remoteType)) {
  81. $searchResult->unsetResult($emailType);
  82. }
  83. $this->dropMailSharesWhereRemoteShareIsPossible($searchResult);
  84. // if we have an exact local user match with an email-a-like query,
  85. // there is no need to show the remote and email matches.
  86. $userType = new SearchResultType('users');
  87. if (str_contains($search, '@') && $searchResult->hasExactIdMatch($userType)) {
  88. $searchResult->unsetResult($remoteType);
  89. $searchResult->unsetResult($emailType);
  90. }
  91. return [$searchResult->asArray(), $hasMoreResults];
  92. }
  93. public function registerPlugin(array $pluginInfo) {
  94. $shareType = constant(Share::class . '::' . $pluginInfo['shareType']);
  95. if ($shareType === null) {
  96. throw new \InvalidArgumentException('Provided ShareType is invalid');
  97. }
  98. $this->pluginList[$shareType][] = $pluginInfo['class'];
  99. }
  100. protected function dropMailSharesWhereRemoteShareIsPossible(ISearchResult $searchResult): void {
  101. $allResults = $searchResult->asArray();
  102. $emailType = new SearchResultType('emails');
  103. $remoteType = new SearchResultType('remotes');
  104. if (!isset($allResults[$remoteType->getLabel()])
  105. || !isset($allResults[$emailType->getLabel()])) {
  106. return;
  107. }
  108. $mailIdMap = [];
  109. foreach ($allResults[$emailType->getLabel()] as $mailRow) {
  110. // sure, array_reduce looks nicer, but foreach needs less resources and is faster
  111. if (!isset($mailRow['uuid'])) {
  112. continue;
  113. }
  114. $mailIdMap[$mailRow['uuid']] = $mailRow['value']['shareWith'];
  115. }
  116. foreach ($allResults[$remoteType->getLabel()] as $resultRow) {
  117. if (!isset($resultRow['uuid'])) {
  118. continue;
  119. }
  120. if (isset($mailIdMap[$resultRow['uuid']])) {
  121. $searchResult->removeCollaboratorResult($emailType, $mailIdMap[$resultRow['uuid']]);
  122. }
  123. }
  124. }
  125. }