You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AutoCompleteController.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Core\Controller;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\AppFramework\OCSController as Controller;
  30. use OCP\Collaboration\AutoComplete\AutoCompleteEvent;
  31. use OCP\Collaboration\AutoComplete\IManager;
  32. use OCP\Collaboration\Collaborators\ISearch;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use OCP\IRequest;
  35. use OCP\Share\IShare;
  36. class AutoCompleteController extends Controller {
  37. /** @var ISearch */
  38. private $collaboratorSearch;
  39. /** @var IManager */
  40. private $autoCompleteManager;
  41. /** @var IEventDispatcher */
  42. private $dispatcher;
  43. public function __construct(
  44. string $appName,
  45. IRequest $request,
  46. ISearch $collaboratorSearch,
  47. IManager $autoCompleteManager,
  48. IEventDispatcher $dispatcher
  49. ) {
  50. parent::__construct($appName, $request);
  51. $this->collaboratorSearch = $collaboratorSearch;
  52. $this->autoCompleteManager = $autoCompleteManager;
  53. $this->dispatcher = $dispatcher;
  54. }
  55. /**
  56. * @NoAdminRequired
  57. *
  58. * @param string $search
  59. * @param string $itemType
  60. * @param string $itemId
  61. * @param string|null $sorter can be piped, top prio first, e.g.: "commenters|share-recipients"
  62. * @param array $shareTypes
  63. * @param int $limit
  64. * @return DataResponse
  65. */
  66. public function get($search, $itemType, $itemId, $sorter = null, $shareTypes = [IShare::TYPE_USER], $limit = 10): DataResponse {
  67. // if enumeration/user listings are disabled, we'll receive an empty
  68. // result from search() – thus nothing else to do here.
  69. [$results,] = $this->collaboratorSearch->search($search, $shareTypes, false, $limit, 0);
  70. $event = new AutoCompleteEvent([
  71. 'search' => $search,
  72. 'results' => $results,
  73. 'itemType' => $itemType,
  74. 'itemId' => $itemId,
  75. 'sorter' => $sorter,
  76. 'shareTypes' => $shareTypes,
  77. 'limit' => $limit,
  78. ]);
  79. $this->dispatcher->dispatch(IManager::class . '::filterResults', $event);
  80. $results = $event->getResults();
  81. $exactMatches = $results['exact'];
  82. unset($results['exact']);
  83. $results = array_merge_recursive($exactMatches, $results);
  84. if ($sorter !== null) {
  85. $sorters = array_reverse(explode('|', $sorter));
  86. $this->autoCompleteManager->runSorters($sorters, $results, [
  87. 'itemType' => $itemType,
  88. 'itemId' => $itemId,
  89. ]);
  90. }
  91. // transform to expected format
  92. $results = $this->prepareResultArray($results);
  93. return new DataResponse($results);
  94. }
  95. protected function prepareResultArray(array $results): array {
  96. $output = [];
  97. foreach ($results as $type => $subResult) {
  98. foreach ($subResult as $result) {
  99. $output[] = [
  100. 'id' => (string) $result['value']['shareWith'],
  101. 'label' => $result['label'],
  102. 'source' => $type,
  103. ];
  104. }
  105. }
  106. return $output;
  107. }
  108. }