You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.7 KiB

  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\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($appName, IRequest $request, ISearch $collaboratorSearch, IManager $autoCompleteManager) {
  36. parent::__construct($appName, $request);
  37. $this->collaboratorSearch = $collaboratorSearch;
  38. $this->autoCompleteManager = $autoCompleteManager;
  39. }
  40. /**
  41. * @NoAdminRequired
  42. *
  43. * @param string $itemType
  44. * @param string $itemId
  45. * @param string|null $sorter can be piped, top prio first, e.g.: "commenters|share-recipients"
  46. * @param array $shareTypes
  47. * @return DataResponse
  48. */
  49. public function get($itemType, $itemId, $sorter = null, $shareTypes = [Share::SHARE_TYPE_USER]) {
  50. // if enumeration/user listings are disabled, we'll receive an empty
  51. // result from search() – thus nothing else to do here.
  52. list($results,) = $this->collaboratorSearch->search('', $shareTypes, false, 20, 0);
  53. // there won't be exact matches without a search string
  54. unset($results['exact']);
  55. $sorters = array_reverse(explode('|', $sorter));
  56. $this->autoCompleteManager->runSorters($sorters, $results, [
  57. 'itemType' => $itemType,
  58. 'itemId' => $itemId,
  59. ]);
  60. // transform to expected format
  61. $results = $this->prepareResultArray($results);
  62. return new DataResponse($results);
  63. }
  64. protected function prepareResultArray(array $results) {
  65. $output = [];
  66. foreach ($results as $type => $subResult) {
  67. foreach ($subResult as $result) {
  68. $output[] = [
  69. 'id' => $result['value']['shareWith'],
  70. 'label' => $result['label'],
  71. 'source' => $type,
  72. ];
  73. }
  74. }
  75. return $output;
  76. }
  77. }