Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions core/Controller/AutoCompleteController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
Expand All @@ -25,27 +26,33 @@

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;
}

/**
Expand All @@ -59,10 +66,22 @@ public function __construct(
* @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']);
Expand All @@ -83,7 +102,7 @@ public function get($search, $itemType, $itemId, $sorter = null, $shareTypes = [
}


protected function prepareResultArray(array $results) {
protected function prepareResultArray(array $results): array {
$output = [];
foreach ($results as $type => $subResult) {
foreach ($subResult as $result) {
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
'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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
99 changes: 99 additions & 0 deletions lib/public/Collaboration/AutoComplete/AutoCompleteEvent.php
Original file line number Diff line number Diff line change
@@ -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');
}

}
12 changes: 9 additions & 3 deletions tests/Core/Controller/AutoCompleteControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
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;

Expand All @@ -44,12 +48,14 @@ protected function setUp() {
$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
);
}

Expand Down