AutoCompleteController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\DataResponse;
  33. use OCP\AppFramework\OCSController;
  34. use OCP\Collaboration\AutoComplete\AutoCompleteEvent;
  35. use OCP\Collaboration\AutoComplete\AutoCompleteFilterEvent;
  36. use OCP\Collaboration\AutoComplete\IManager;
  37. use OCP\Collaboration\Collaborators\ISearch;
  38. use OCP\EventDispatcher\IEventDispatcher;
  39. use OCP\IRequest;
  40. use OCP\Share\IShare;
  41. /**
  42. * @psalm-import-type CoreAutocompleteResult from ResponseDefinitions
  43. */
  44. class AutoCompleteController extends OCSController {
  45. public function __construct(
  46. string $appName,
  47. IRequest $request,
  48. private ISearch $collaboratorSearch,
  49. private IManager $autoCompleteManager,
  50. private IEventDispatcher $dispatcher,
  51. ) {
  52. parent::__construct($appName, $request);
  53. }
  54. /**
  55. * @NoAdminRequired
  56. *
  57. * Autocomplete a query
  58. *
  59. * @param string $search Text to search for
  60. * @param string|null $itemType Type of the items to search for
  61. * @param string|null $itemId ID of the items to search for
  62. * @param string|null $sorter can be piped, top prio first, e.g.: "commenters|share-recipients"
  63. * @param int[] $shareTypes Types of shares to search for
  64. * @param int $limit Maximum number of results to return
  65. *
  66. * @return DataResponse<Http::STATUS_OK, CoreAutocompleteResult[], array{}>
  67. *
  68. * 200: Autocomplete results returned
  69. */
  70. public function get(string $search, ?string $itemType, ?string $itemId, ?string $sorter = null, array $shareTypes = [IShare::TYPE_USER], int $limit = 10): DataResponse {
  71. // if enumeration/user listings are disabled, we'll receive an empty
  72. // result from search() – thus nothing else to do here.
  73. [$results,] = $this->collaboratorSearch->search($search, $shareTypes, false, $limit, 0);
  74. $event = new AutoCompleteEvent([
  75. 'search' => $search,
  76. 'results' => $results,
  77. 'itemType' => $itemType,
  78. 'itemId' => $itemId,
  79. 'sorter' => $sorter,
  80. 'shareTypes' => $shareTypes,
  81. 'limit' => $limit,
  82. ]);
  83. $this->dispatcher->dispatch(IManager::class . '::filterResults', $event);
  84. $results = $event->getResults();
  85. $event = new AutoCompleteFilterEvent(
  86. $results,
  87. $search,
  88. $itemType,
  89. $itemId,
  90. $sorter,
  91. $shareTypes,
  92. $limit,
  93. );
  94. $this->dispatcher->dispatchTyped($event);
  95. $results = $event->getResults();
  96. $exactMatches = $results['exact'];
  97. unset($results['exact']);
  98. $results = array_merge_recursive($exactMatches, $results);
  99. if ($sorter !== null) {
  100. $sorters = array_reverse(explode('|', $sorter));
  101. $this->autoCompleteManager->runSorters($sorters, $results, [
  102. 'itemType' => $itemType,
  103. 'itemId' => $itemId,
  104. ]);
  105. }
  106. // transform to expected format
  107. $results = $this->prepareResultArray($results);
  108. return new DataResponse($results);
  109. }
  110. /**
  111. * @return CoreAutocompleteResult[]
  112. */
  113. protected function prepareResultArray(array $results): array {
  114. $output = [];
  115. /** @var string $type */
  116. foreach ($results as $type => $subResult) {
  117. foreach ($subResult as $result) {
  118. /** @var ?string $icon */
  119. $icon = array_key_exists('icon', $result) ? $result['icon'] : null;
  120. /** @var string $label */
  121. $label = $result['label'];
  122. /** @var ?string $subline */
  123. $subline = array_key_exists('subline', $result) ? $result['subline'] : null;
  124. /** @var ?array{status: string, message: ?string, icon: ?string, clearAt: ?int} $status */
  125. $status = array_key_exists('status', $result) && is_array($result['status']) && !empty($result['status']) ? $result['status'] : null;
  126. /** @var ?string $shareWithDisplayNameUnique */
  127. $shareWithDisplayNameUnique = array_key_exists('shareWithDisplayNameUnique', $result) ? $result['shareWithDisplayNameUnique'] : null;
  128. $output[] = [
  129. 'id' => (string) $result['value']['shareWith'],
  130. 'label' => $label,
  131. 'icon' => $icon ?? '',
  132. 'source' => $type,
  133. 'status' => $status ?? '',
  134. 'subline' => $subline ?? '',
  135. 'shareWithDisplayNameUnique' => $shareWithDisplayNameUnique ?? '',
  136. ];
  137. }
  138. }
  139. return $output;
  140. }
  141. }