AutoCompleteController.php 3.6 KB

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