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.

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