Add an event to the Autocomplete Controller to allow to filter the results
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
fc105d3bb0
commit
55f627d20b
5 changed files with 134 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
||||
*
|
||||
|
@ -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) {
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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',
|
||||
|
|
99
lib/public/Collaboration/AutoComplete/AutoCompleteEvent.php
Normal file
99
lib/public/Collaboration/AutoComplete/AutoCompleteEvent.php
Normal file
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com>
|
||||
*
|
||||
* @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 <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue