AutoCompleteController.php 3.8 KB

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