AutoCompleteController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Kate Döen <kate.doeen@nextcloud.com>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OC\Core\Controller;
  30. use OCA\Core\ResponseDefinitions;
  31. use OCP\AppFramework\Http;
  32. use OCP\AppFramework\Http\Attribute\ApiRoute;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\OCSController;
  35. use OCP\Collaboration\AutoComplete\AutoCompleteEvent;
  36. use OCP\Collaboration\AutoComplete\AutoCompleteFilterEvent;
  37. use OCP\Collaboration\AutoComplete\IManager;
  38. use OCP\Collaboration\Collaborators\ISearch;
  39. use OCP\EventDispatcher\IEventDispatcher;
  40. use OCP\IRequest;
  41. use OCP\Share\IShare;
  42. /**
  43. * @psalm-import-type CoreAutocompleteResult from ResponseDefinitions
  44. */
  45. class AutoCompleteController extends OCSController {
  46. public function __construct(
  47. string $appName,
  48. IRequest $request,
  49. private ISearch $collaboratorSearch,
  50. private IManager $autoCompleteManager,
  51. private IEventDispatcher $dispatcher,
  52. ) {
  53. parent::__construct($appName, $request);
  54. }
  55. /**
  56. * @NoAdminRequired
  57. *
  58. * Autocomplete a query
  59. *
  60. * @param string $search Text to search for
  61. * @param string|null $itemType Type of the items to search for
  62. * @param string|null $itemId ID of the items to search for
  63. * @param string|null $sorter can be piped, top prio first, e.g.: "commenters|share-recipients"
  64. * @param int[] $shareTypes Types of shares to search for
  65. * @param int $limit Maximum number of results to return
  66. *
  67. * @return DataResponse<Http::STATUS_OK, CoreAutocompleteResult[], array{}>
  68. *
  69. * 200: Autocomplete results returned
  70. */
  71. #[ApiRoute(verb: 'GET', url: '/autocomplete/get', root: '/core')]
  72. public function get(string $search, ?string $itemType, ?string $itemId, ?string $sorter = null, array $shareTypes = [IShare::TYPE_USER], int $limit = 10): DataResponse {
  73. // if enumeration/user listings are disabled, we'll receive an empty
  74. // result from search() – thus nothing else to do here.
  75. [$results,] = $this->collaboratorSearch->search($search, $shareTypes, false, $limit, 0);
  76. $event = new AutoCompleteEvent([
  77. 'search' => $search,
  78. 'results' => $results,
  79. 'itemType' => $itemType,
  80. 'itemId' => $itemId,
  81. 'sorter' => $sorter,
  82. 'shareTypes' => $shareTypes,
  83. 'limit' => $limit,
  84. ]);
  85. $this->dispatcher->dispatch(IManager::class . '::filterResults', $event);
  86. $results = $event->getResults();
  87. $event = new AutoCompleteFilterEvent(
  88. $results,
  89. $search,
  90. $itemType,
  91. $itemId,
  92. $sorter,
  93. $shareTypes,
  94. $limit,
  95. );
  96. $this->dispatcher->dispatchTyped($event);
  97. $results = $event->getResults();
  98. $exactMatches = $results['exact'];
  99. unset($results['exact']);
  100. $results = array_merge_recursive($exactMatches, $results);
  101. if ($sorter !== null) {
  102. $sorters = array_reverse(explode('|', $sorter));
  103. $this->autoCompleteManager->runSorters($sorters, $results, [
  104. 'itemType' => $itemType,
  105. 'itemId' => $itemId,
  106. ]);
  107. }
  108. // transform to expected format
  109. $results = $this->prepareResultArray($results);
  110. return new DataResponse($results);
  111. }
  112. /**
  113. * @return CoreAutocompleteResult[]
  114. */
  115. protected function prepareResultArray(array $results): array {
  116. $output = [];
  117. /** @var string $type */
  118. foreach ($results as $type => $subResult) {
  119. foreach ($subResult as $result) {
  120. /** @var ?string $icon */
  121. $icon = array_key_exists('icon', $result) ? $result['icon'] : null;
  122. /** @var string $label */
  123. $label = $result['label'];
  124. /** @var ?string $subline */
  125. $subline = array_key_exists('subline', $result) ? $result['subline'] : null;
  126. /** @var ?array{status: string, message: ?string, icon: ?string, clearAt: ?int} $status */
  127. $status = array_key_exists('status', $result) && is_array($result['status']) && !empty($result['status']) ? $result['status'] : null;
  128. /** @var ?string $shareWithDisplayNameUnique */
  129. $shareWithDisplayNameUnique = array_key_exists('shareWithDisplayNameUnique', $result) ? $result['shareWithDisplayNameUnique'] : null;
  130. $output[] = [
  131. 'id' => (string) $result['value']['shareWith'],
  132. 'label' => $label,
  133. 'icon' => $icon ?? '',
  134. 'source' => $type,
  135. 'status' => $status ?? '',
  136. 'subline' => $subline ?? '',
  137. 'shareWithDisplayNameUnique' => $shareWithDisplayNameUnique ?? '',
  138. ];
  139. }
  140. }
  141. return $output;
  142. }
  143. }