AutoCompleteController.php 4.7 KB

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