AutoCompleteController.php 3.8 KB

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