Skip to content

Commit f085dfe

Browse files
committed
fix: Use AutoCompleteFilterEvent when returning list of sharees
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent 5446d71 commit f085dfe

19 files changed

Lines changed: 182 additions & 121 deletions

File tree

apps/files_sharing/lib/Controller/ShareesAPIController.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public function __construct(
7777
protected IURLGenerator $urlGenerator,
7878
protected IManager $shareManager,
7979
protected ISearch $collaboratorSearch,
80+
protected FederatedShareProvider $federatedShareProvider,
81+
protected IAppManager $appManager,
8082
) {
8183
parent::__construct($appName, $request);
8284
}
@@ -158,7 +160,7 @@ public function search(string $search = '', ?string $itemType = null, int $page
158160
}
159161

160162
// FIXME: DI
161-
if (Server::get(IAppManager::class)->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
163+
if ($this->appManager->isEnabledForUser('circles') && class_exists('\OCA\Circles\ShareByCircleProvider')) {
162164
$shareTypes[] = IShare::TYPE_CIRCLE;
163165
}
164166

@@ -184,25 +186,26 @@ public function search(string $search = '', ?string $itemType = null, int $page
184186
$this->result['lookupEnabled'] = Server::get(GlobalScaleIConfig::class)->isGlobalScaleEnabled();
185187
// TODO: Reconsider using lookup server for non-global-scale federation
186188

187-
[$result, $hasMoreResults] = $this->collaboratorSearch->search($search, $shareTypes, $this->result['lookupEnabled'], $this->limit, $this->offset);
189+
[$result, $hasMoreResults] = $this->collaboratorSearch->filteredSearch($search, $shareTypes, $this->result['lookupEnabled'], $itemType, null, $this->limit, $this->offset);
188190

189191
// extra treatment for 'exact' subarray, with a single merge expected keys might be lost
190192
if (isset($result['exact'])) {
191193
$result['exact'] = array_merge($this->result['exact'], $result['exact']);
192194
}
193-
$this->result = array_merge($this->result, $result);
194-
$response = new DataResponse($this->result);
195+
/** @var Files_SharingShareesSearchResult $result */
196+
$result = array_merge($this->result, $result);
195197

198+
$headers = [];
196199
if ($hasMoreResults) {
197-
$response->setHeaders(['Link' => $this->getPaginationLink($page, [
200+
$headers['Link'] = $this->getPaginationLink($page, [
198201
'search' => $search,
199202
'itemType' => $itemType,
200203
'shareType' => $shareTypes,
201204
'perPage' => $perPage,
202-
])]);
205+
]);
203206
}
204207

205-
return $response;
208+
return new DataResponse($result, Http::STATUS_OK, $headers);
206209
}
207210

208211
/**

apps/files_sharing/tests/Controller/ShareesAPIControllerTest.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use OCA\Files_Sharing\Controller\ShareesAPIController;
1111
use OCA\Files_Sharing\Tests\TestCase;
12+
use OCP\App\IAppManager;
1213
use OCP\AppFramework\Http\DataResponse;
1314
use OCP\AppFramework\OCS\OCSBadRequestException;
1415
use OCP\Collaboration\Collaborators\ISearch;
@@ -28,23 +29,14 @@
2829
* @package OCA\Files_Sharing\Tests\API
2930
*/
3031
class ShareesAPIControllerTest extends TestCase {
31-
/** @var ShareesAPIController */
32-
protected $sharees;
33-
34-
/** @var string */
35-
protected $uid;
36-
37-
/** @var IRequest|MockObject */
38-
protected $request;
39-
40-
/** @var IManager|MockObject */
32+
protected ShareesAPIController $sharees;
33+
protected string $uid;
34+
protected IRequest&MockObject $request;
4135
protected $shareManager;
42-
43-
/** @var ISearch|MockObject */
44-
protected $collaboratorSearch;
45-
46-
/** @var IConfig|MockObject */
47-
protected $config;
36+
protected ISearch&MockObject $collaboratorSearch;
37+
protected IConfig&MockObject $config;
38+
protected FederatedShareProvider&MockObject $federatedShareProvider;
39+
protected IAppManager&MockObject $appManager;
4840

4941
protected function setUp(): void {
5042
parent::setUp();
@@ -54,10 +46,11 @@ protected function setUp(): void {
5446
$this->shareManager = $this->createMock(IManager::class);
5547
$this->config = $this->createMock(IConfig::class);
5648

57-
/** @var IURLGenerator|MockObject $urlGeneratorMock */
5849
$urlGeneratorMock = $this->createMock(IURLGenerator::class);
5950

6051
$this->collaboratorSearch = $this->createMock(ISearch::class);
52+
$this->federatedShareProvider = $this->createMock(FederatedShareProvider::class);
53+
$this->appManager = $this->createMock(IAppManager::class);
6154

6255
$this->sharees = new ShareesAPIController(
6356
'files_sharing',
@@ -66,7 +59,9 @@ protected function setUp(): void {
6659
$this->config,
6760
$urlGeneratorMock,
6861
$this->shareManager,
69-
$this->collaboratorSearch
62+
$this->collaboratorSearch,
63+
$this->federatedShareProvider
64+
$this->appManager,
7065
);
7166
}
7267

@@ -260,7 +255,9 @@ public function testSearch(
260255
$config,
261256
$urlGenerator,
262257
$this->shareManager,
263-
$this->collaboratorSearch
258+
$this->collaboratorSearch,
259+
$this->federatedShareProvider,
260+
$this->appManager,
264261
])
265262
->onlyMethods(['isRemoteSharingAllowed', 'isRemoteGroupSharingAllowed'])
266263
->getMock();
@@ -269,8 +266,8 @@ public function testSearch(
269266
sort($expectedShareTypes);
270267

271268
$this->collaboratorSearch->expects($this->once())
272-
->method('search')
273-
->with($search, $expectedShareTypes, $this->anything(), $perPage, $perPage * ($page - 1))
269+
->method('filteredSearch')
270+
->with($search, $expectedShareTypes, $this->anything(), $itemType, null, $perPage, $perPage * ($page - 1))
274271
->willReturn([[], false]);
275272

276273
$sharees->expects($this->any())
@@ -359,15 +356,17 @@ public function testSearchInvalid($getData, $message): void {
359356
$config,
360357
$urlGenerator,
361358
$this->shareManager,
362-
$this->collaboratorSearch
359+
$this->collaboratorSearch,
360+
$this->federatedShareProvider,
361+
$this->appManager,
363362
])
364363
->onlyMethods(['isRemoteSharingAllowed'])
365364
->getMock();
366365
$sharees->expects($this->never())
367366
->method('isRemoteSharingAllowed');
368367

369368
$this->collaboratorSearch->expects($this->never())
370-
->method('search');
369+
->method('filteredSearch');
371370

372371
try {
373372
$sharees->search('', null, $page, $perPage, null);

apps/user_status/lib/Controller/StatusesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function findAll(?int $limit = null, ?int $offset = null): DataResponse {
6565

6666
if ($users !== $event->getUsers()) {
6767
$removedUsers = $event->getFilteredOutUsers();
68-
$allStatuses = array_filter($allStatuses, fn (UserStatus $userStatus): bool => in_array($userStatus->getUserId(), $removedUsers, true));
68+
$allStatuses = array_filter($allStatuses, fn (UserStatus $userStatus): bool => !in_array($userStatus->getUserId(), $removedUsers, true));
6969
}
7070

7171
return new DataResponse(array_values(array_map(function ($userStatus) {

apps/user_status/tests/Unit/Controller/StatusesControllerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,24 @@
1313
use OCA\UserStatus\Service\StatusService;
1414
use OCP\AppFramework\Db\DoesNotExistException;
1515
use OCP\AppFramework\OCS\OCSNotFoundException;
16+
use OCP\EventDispatcher\IEventDispatcher;
1617
use OCP\IRequest;
1718
use PHPUnit\Framework\MockObject\MockObject;
1819
use Test\TestCase;
1920

2021
class StatusesControllerTest extends TestCase {
2122
private StatusService&MockObject $service;
23+
private IEventDispatcher&MockObject $eventDispatcher;
2224
private StatusesController $controller;
2325

2426
protected function setUp(): void {
2527
parent::setUp();
2628

2729
$request = $this->createMock(IRequest::class);
2830
$this->service = $this->createMock(StatusService::class);
31+
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
2932

30-
$this->controller = new StatusesController('user_status', $request, $this->service);
33+
$this->controller = new StatusesController('user_status', $request, $this->service, $this->eventDispatcher);
3134
}
3235

3336
public function testFindAll(): void {

build/psalm-baseline.xml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,6 @@
15801580
<code><![CDATA[Circles]]></code>
15811581
</UndefinedClass>
15821582
<UndefinedDocblockClass>
1583-
<code><![CDATA[$this->getRoomShareHelper()]]></code>
15841583
<code><![CDATA[$this->getRoomShareHelper()]]></code>
15851584
<code><![CDATA[$this->getRoomShareHelper()]]></code>
15861585
<code><![CDATA[\OCA\Talk\Share\Helper\ShareAPIController]]></code>
@@ -3062,22 +3061,6 @@
30623061
<code><![CDATA[IToken::PERMANENT_TOKEN]]></code>
30633062
</DeprecatedClass>
30643063
</file>
3065-
<file src="core/Controller/AutoCompleteController.php">
3066-
<DeprecatedClass>
3067-
<code><![CDATA[new AutoCompleteEvent([
3068-
'search' => $search,
3069-
'results' => $results,
3070-
'itemType' => $itemType,
3071-
'itemId' => $itemId,
3072-
'sorter' => $sorter,
3073-
'shareTypes' => $shareTypes,
3074-
'limit' => $limit,
3075-
])]]></code>
3076-
</DeprecatedClass>
3077-
<DeprecatedMethod>
3078-
<code><![CDATA[dispatch]]></code>
3079-
</DeprecatedMethod>
3080-
</file>
30813064
<file src="core/Controller/ClientFlowLoginV2Controller.php">
30823065
<TypeDoesNotContainType>
30833066
<code><![CDATA[!is_string($stateToken)]]></code>

core/Controller/AutoCompleteController.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@
1414
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1515
use OCP\AppFramework\Http\DataResponse;
1616
use OCP\AppFramework\OCSController;
17-
use OCP\Collaboration\AutoComplete\AutoCompleteEvent;
18-
use OCP\Collaboration\AutoComplete\AutoCompleteFilterEvent;
1917
use OCP\Collaboration\AutoComplete\IManager;
2018
use OCP\Collaboration\Collaborators\ISearch;
21-
use OCP\EventDispatcher\IEventDispatcher;
2219
use OCP\IRequest;
2320
use OCP\Share\IShare;
2421

@@ -31,7 +28,6 @@ public function __construct(
3128
IRequest $request,
3229
private ISearch $collaboratorSearch,
3330
private IManager $autoCompleteManager,
34-
private IEventDispatcher $dispatcher,
3531
) {
3632
parent::__construct($appName, $request);
3733
}
@@ -55,31 +51,7 @@ public function __construct(
5551
public function get(string $search, ?string $itemType, ?string $itemId, ?string $sorter = null, array $shareTypes = [IShare::TYPE_USER], int $limit = 10): DataResponse {
5652
// if enumeration/user listings are disabled, we'll receive an empty
5753
// result from search() – thus nothing else to do here.
58-
[$results,] = $this->collaboratorSearch->search($search, $shareTypes, false, $limit, 0);
59-
60-
$event = new AutoCompleteEvent([
61-
'search' => $search,
62-
'results' => $results,
63-
'itemType' => $itemType,
64-
'itemId' => $itemId,
65-
'sorter' => $sorter,
66-
'shareTypes' => $shareTypes,
67-
'limit' => $limit,
68-
]);
69-
$this->dispatcher->dispatch(IManager::class . '::filterResults', $event);
70-
$results = $event->getResults();
71-
72-
$event = new AutoCompleteFilterEvent(
73-
$results,
74-
$search,
75-
$itemType,
76-
$itemId,
77-
$sorter,
78-
$shareTypes,
79-
$limit,
80-
);
81-
$this->dispatcher->dispatchTyped($event);
82-
$results = $event->getResults();
54+
[$results,] = $this->collaboratorSearch->filteredSearch($search, $shareTypes, false, $itemType, $itemId, $limit, 0);
8355

8456
$exactMatches = $results['exact'];
8557
unset($results['exact']);

lib/private/Collaboration/Collaborators/GroupPlugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function __construct(
4040
}
4141
}
4242

43-
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
43+
#[\Override]
44+
public function search(string $search, int $limit, int $offset, ISearchResult $searchResult): bool {
4445
if ($this->groupSharingDisabled) {
4546
return false;
4647
}

lib/private/Collaboration/Collaborators/MailPlugin.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ public function __construct(
5656
}
5757
}
5858

59-
/**
60-
* {@inheritdoc}
61-
*/
62-
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
59+
#[\Override]
60+
public function search(string $search, int $limit, int $offset, ISearchResult $searchResult): bool {
6361
if ($this->shareeEnumerationFullMatch && !$this->shareeEnumerationFullMatchEmail) {
6462
return false;
6563
}

lib/private/Collaboration/Collaborators/RemoteGroupPlugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public function __construct(
3232
}
3333
}
3434

35-
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
35+
#[\Override]
36+
public function search(string $search, int $limit, int $offset, ISearchResult $searchResult): bool {
3637
$result = ['wide' => [], 'exact' => []];
3738
$resultType = new SearchResultType('remote_groups');
3839

lib/private/Collaboration/Collaborators/RemotePlugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public function __construct(
3737
}
3838

3939

40-
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
40+
#[\Override]
41+
public function search(string $search, int $limit, int $offset, ISearchResult $searchResult): bool {
4142
$result = ['wide' => [], 'exact' => []];
4243
$resultType = new SearchResultType('remotes');
4344

0 commit comments

Comments
 (0)