diff --git a/core/Controller/AutoCompleteController.php b/core/Controller/AutoCompleteController.php index 3f3858ce1c..80f5d3c863 100644 --- a/core/Controller/AutoCompleteController.php +++ b/core/Controller/AutoCompleteController.php @@ -1,4 +1,5 @@ * @@ -25,27 +26,33 @@ namespace OC\Core\Controller; use OCP\AppFramework\OCSController as Controller; use OCP\AppFramework\Http\DataResponse; +use OCP\Collaboration\AutoComplete\AutoCompleteEvent; use OCP\Collaboration\AutoComplete\IManager; use OCP\Collaboration\Collaborators\ISearch; use OCP\IRequest; use OCP\Share; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class AutoCompleteController extends Controller { /** @var ISearch */ private $collaboratorSearch; /** @var IManager */ private $autoCompleteManager; + /** @var EventDispatcherInterface */ + private $dispatcher; public function __construct( - $appName, + string $appName, IRequest $request, ISearch $collaboratorSearch, - IManager $autoCompleteManager + IManager $autoCompleteManager, + EventDispatcherInterface $dispatcher ) { parent::__construct($appName, $request); $this->collaboratorSearch = $collaboratorSearch; $this->autoCompleteManager = $autoCompleteManager; + $this->dispatcher = $dispatcher; } /** @@ -59,10 +66,22 @@ class AutoCompleteController extends Controller { * @param int $limit * @return DataResponse */ - public function get($search, $itemType, $itemId, $sorter = null, $shareTypes = [Share::SHARE_TYPE_USER], $limit = 10) { + public function get($search, $itemType, $itemId, $sorter = null, $shareTypes = [Share::SHARE_TYPE_USER], $limit = 10): DataResponse { // if enumeration/user listings are disabled, we'll receive an empty // result from search() – thus nothing else to do here. - list($results,) = $this->collaboratorSearch->search($search, $shareTypes, false, $limit, 0); + [$results,] = $this->collaboratorSearch->search($search, $shareTypes, false, $limit, 0); + + $event = new AutoCompleteEvent([ + 'search' => $search, + 'results' => $results, + 'itemType' => $itemType, + 'itemId' => $itemId, + 'sorter' => $sorter, + 'shareTypes' => $shareTypes, + 'limit' => $limit, + ]); + $this->dispatcher->dispatch(IManager::class . '::filterResults', $event); + $results = $event->getResults(); $exactMatches = $results['exact']; unset($results['exact']); @@ -83,7 +102,7 @@ class AutoCompleteController extends Controller { } - protected function prepareResultArray(array $results) { + protected function prepareResultArray(array $results): array { $output = []; foreach ($results as $type => $subResult) { foreach ($subResult as $result) { diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 9ac6d83744..be7702c1d4 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -104,6 +104,7 @@ return array( 'OCP\\Calendar\\Room\\IRoom' => $baseDir . '/lib/public/Calendar/Room/IRoom.php', 'OCP\\Capabilities\\ICapability' => $baseDir . '/lib/public/Capabilities/ICapability.php', 'OCP\\Capabilities\\IPublicCapability' => $baseDir . '/lib/public/Capabilities/IPublicCapability.php', + 'OCP\\Collaboration\\AutoComplete\\AutoCompleteEvent' => $baseDir . '/lib/public/Collaboration/AutoComplete/AutoCompleteEvent.php', 'OCP\\Collaboration\\AutoComplete\\IManager' => $baseDir . '/lib/public/Collaboration/AutoComplete/IManager.php', 'OCP\\Collaboration\\AutoComplete\\ISorter' => $baseDir . '/lib/public/Collaboration/AutoComplete/ISorter.php', 'OCP\\Collaboration\\Collaborators\\ISearch' => $baseDir . '/lib/public/Collaboration/Collaborators/ISearch.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index ca5e47bb9f..04395a3b80 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -134,6 +134,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OCP\\Calendar\\Room\\IRoom' => __DIR__ . '/../../..' . '/lib/public/Calendar/Room/IRoom.php', 'OCP\\Capabilities\\ICapability' => __DIR__ . '/../../..' . '/lib/public/Capabilities/ICapability.php', 'OCP\\Capabilities\\IPublicCapability' => __DIR__ . '/../../..' . '/lib/public/Capabilities/IPublicCapability.php', + 'OCP\\Collaboration\\AutoComplete\\AutoCompleteEvent' => __DIR__ . '/../../..' . '/lib/public/Collaboration/AutoComplete/AutoCompleteEvent.php', 'OCP\\Collaboration\\AutoComplete\\IManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/AutoComplete/IManager.php', 'OCP\\Collaboration\\AutoComplete\\ISorter' => __DIR__ . '/../../..' . '/lib/public/Collaboration/AutoComplete/ISorter.php', 'OCP\\Collaboration\\Collaborators\\ISearch' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Collaborators/ISearch.php', diff --git a/lib/public/Collaboration/AutoComplete/AutoCompleteEvent.php b/lib/public/Collaboration/AutoComplete/AutoCompleteEvent.php new file mode 100644 index 0000000000..59dbafcb56 --- /dev/null +++ b/lib/public/Collaboration/AutoComplete/AutoCompleteEvent.php @@ -0,0 +1,99 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\Collaboration\AutoComplete; + + +use Symfony\Component\EventDispatcher\GenericEvent; + +/** + * @since 16.0.0 + */ +class AutoCompleteEvent extends GenericEvent { + + /** + * @param array $arguments + * @since 16.0.0 + */ + public function __construct(array $arguments) { + parent::__construct(null, $arguments); + } + + /** + * @since 16.0.0 + */ + public function getResults(): array { + return $this->getArgument('results'); + } + + /** + * @param array $results + * @since 16.0.0 + */ + public function setResults(array $results): void { + $this->setArgument('results', $results); + } + + /** + * @since 16.0.0 + */ + public function getSearchTerm(): string { + return $this->getArgument('search'); + } + + /** + * @return int[] + * @since 16.0.0 + */ + public function getShareTypes(): array { + return $this->getArgument('shareTypes'); + } + + /** + * @since 16.0.0 + */ + public function getItemType(): string { + return $this->getArgument('itemType'); + } + + /** + * @since 16.0.0 + */ + public function getItemId(): string { + return $this->getArgument('itemId'); + } + + /** + * @since 16.0.0 + */ + public function getSorter(): string { + return $this->getArgument('sorter'); + } + + /** + * @since 16.0.0 + */ + public function getLimit(): int { + return $this->getArgument('limit'); + } + +} diff --git a/tests/Core/Controller/AutoCompleteControllerTest.php b/tests/Core/Controller/AutoCompleteControllerTest.php index b1e9e6ba30..8ff0542cb0 100644 --- a/tests/Core/Controller/AutoCompleteControllerTest.php +++ b/tests/Core/Controller/AutoCompleteControllerTest.php @@ -27,13 +27,17 @@ use OC\Core\Controller\AutoCompleteController; use OCP\Collaboration\AutoComplete\IManager; use OCP\Collaboration\Collaborators\ISearch; use OCP\IRequest; +use PHPUnit\Framework\MockObject\MockObject; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Test\TestCase; class AutoCompleteControllerTest extends TestCase { - /** @var ISearch|\PHPUnit_Framework_MockObject_MockObject */ + /** @var ISearch|MockObject */ protected $collaboratorSearch; - /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */ + /** @var IManager|MockObject */ protected $autoCompleteManager; + /** @var EventDispatcherInterface|MockObject */ + protected $dispatcher; /** @var AutoCompleteController */ protected $controller; @@ -44,12 +48,14 @@ class AutoCompleteControllerTest extends TestCase { $request = $this->createMock(IRequest::class); $this->collaboratorSearch = $this->createMock(ISearch::class); $this->autoCompleteManager = $this->createMock(IManager::class); + $this->dispatcher = $this->createMock(EventDispatcherInterface::class); $this->controller = new AutoCompleteController( 'core', $request, $this->collaboratorSearch, - $this->autoCompleteManager + $this->autoCompleteManager, + $this->dispatcher ); }