1
0

AutoCompleteController.php 3.5 KB

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