AutoCompleteController.php 2.9 KB

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