AutoCompleteController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCSController;
  32. use OCP\Collaboration\AutoComplete\AutoCompleteEvent;
  33. use OCP\Collaboration\AutoComplete\IManager;
  34. use OCP\Collaboration\Collaborators\ISearch;
  35. use OCP\EventDispatcher\IEventDispatcher;
  36. use OCP\IRequest;
  37. use OCP\Share\IShare;
  38. class AutoCompleteController extends OCSController {
  39. private ISearch $collaboratorSearch;
  40. private IManager $autoCompleteManager;
  41. private IEventDispatcher $dispatcher;
  42. public function __construct(string $appName,
  43. IRequest $request,
  44. ISearch $collaboratorSearch,
  45. IManager $autoCompleteManager,
  46. IEventDispatcher $dispatcher) {
  47. parent::__construct($appName, $request);
  48. $this->collaboratorSearch = $collaboratorSearch;
  49. $this->autoCompleteManager = $autoCompleteManager;
  50. $this->dispatcher = $dispatcher;
  51. }
  52. /**
  53. * @NoAdminRequired
  54. * @param string|null $sorter can be piped, top prio first, e.g.: "commenters|share-recipients"
  55. */
  56. public function get(string $search, ?string $itemType, ?string $itemId, ?string $sorter = null, array $shareTypes = [IShare::TYPE_USER], int $limit = 10): DataResponse {
  57. // if enumeration/user listings are disabled, we'll receive an empty
  58. // result from search() – thus nothing else to do here.
  59. [$results,] = $this->collaboratorSearch->search($search, $shareTypes, false, $limit, 0);
  60. $event = new AutoCompleteEvent([
  61. 'search' => $search,
  62. 'results' => $results,
  63. 'itemType' => $itemType,
  64. 'itemId' => $itemId,
  65. 'sorter' => $sorter,
  66. 'shareTypes' => $shareTypes,
  67. 'limit' => $limit,
  68. ]);
  69. $this->dispatcher->dispatch(IManager::class . '::filterResults', $event);
  70. $results = $event->getResults();
  71. $exactMatches = $results['exact'];
  72. unset($results['exact']);
  73. $results = array_merge_recursive($exactMatches, $results);
  74. if ($sorter !== null) {
  75. $sorters = array_reverse(explode('|', $sorter));
  76. $this->autoCompleteManager->runSorters($sorters, $results, [
  77. 'itemType' => $itemType,
  78. 'itemId' => $itemId,
  79. ]);
  80. }
  81. // transform to expected format
  82. $results = $this->prepareResultArray($results);
  83. return new DataResponse($results);
  84. }
  85. protected function prepareResultArray(array $results): array {
  86. $output = [];
  87. foreach ($results as $type => $subResult) {
  88. foreach ($subResult as $result) {
  89. $output[] = [
  90. 'id' => (string) $result['value']['shareWith'],
  91. 'label' => $result['label'],
  92. 'icon' => $result['icon'] ?? '',
  93. 'source' => $type,
  94. 'status' => $result['status'] ?? '',
  95. 'subline' => $result['subline'] ?? '',
  96. 'shareWithDisplayNameUnique' => $result['shareWithDisplayNameUnique'] ?? '',
  97. ];
  98. }
  99. }
  100. return $output;
  101. }
  102. }