Skip to content

Commit b3555e9

Browse files
committed
feat(Sharing): Allow filtering shares by state
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent 840e933 commit b3555e9

12 files changed

Lines changed: 271 additions & 44 deletions

File tree

‎apps/sharing/lib/Command/GetShares.php‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
use Exception;
1313
use NCU\Sharing\Share;
14+
use NCU\Sharing\ShareState;
1415
use NCU\Sharing\Source\IShareSourceType;
1516
use OC\Core\Command\Base;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Input\InputOption;
1819
use Symfony\Component\Console\Output\OutputInterface;
20+
use ValueError;
1921

2022
final class GetShares extends SharingBase {
2123
#[\Override]
@@ -25,6 +27,7 @@ public function configure(): void {
2527
->setDescription('Get multiple shares.')
2628
->addOption('filter-source-type-class', '', InputOption::VALUE_REQUIRED, 'Source type class to filter by')
2729
->addOption('filter-source-type-value', '', InputOption::VALUE_REQUIRED, 'Source type value to filter by')
30+
->addOption('filter-state', '', InputOption::VALUE_REQUIRED, 'State to filter by')
2831
->addOption('last-share-id', '', InputOption::VALUE_REQUIRED, 'Share ID to use as an offset')
2932
->addOption('limit', '', InputOption::VALUE_REQUIRED, 'Maximum number of shares to return');
3033
parent::configure();
@@ -38,6 +41,17 @@ public function execute(InputInterface $input, OutputInterface $output): int {
3841
$filterSourceTypeClass = $input->getOption('filter-source-type-class');
3942
/** @var ?class-string<IShareSourceType> $filterSourceTypeValue */
4043
$filterSourceTypeValue = $input->getOption('filter-source-type-value');
44+
/** @var ?string $filterState */
45+
$filterState = $input->getOption('filter-state');
46+
if ($filterState !== null) {
47+
try {
48+
$filterState = ShareState::from($filterState);
49+
} catch (ValueError $valueError) {
50+
$output->writeln($valueError->getMessage());
51+
return Base::FAILURE;
52+
}
53+
}
54+
4155
/** @var ?string $lastShareID */
4256
$lastShareID = $input->getOption('last-share-id');
4357
/** @var ?string $limit */
@@ -53,7 +67,7 @@ public function execute(InputInterface $input, OutputInterface $output): int {
5367
try {
5468
$this->dbConnection->beginTransaction();
5569

56-
$shares = $this->manager->getShares($this->accessContext, $filterSourceTypeClass, $filterSourceTypeValue, $lastShareID, $limit);
70+
$shares = $this->manager->getShares($this->accessContext, $filterSourceTypeClass, $filterSourceTypeValue, $filterState, $lastShareID, $limit);
5771
$this->dbConnection->commit();
5872

5973
$data = Share::formatMultiple($this->registry, $this->l10nFactory, $this->urlGenerator, $this->userManager, $shares);

‎apps/sharing/lib/Controller/ApiV1Controller.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ public function getShare(string $id, ?string $secret = null, array $arguments =
560560
*
561561
* @param ?class-string<IShareSourceType> $filterSourceTypeClass Source type class to filter by.
562562
* @param ?non-empty-string $filterSourceTypeValue Source type value to filter by.
563+
* @param ?non-empty-string $filterState State to filter by.
563564
* @param ?string $lastShareID The ID of the previous share. This is used as an offset and only shares with higher IDs are returned.
564565
* @param int<1, 100> $limit The number of shares to return.
565566
* @return DataResponse<Http::STATUS_OK, list<SharingShare>, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, string, array{}>
@@ -569,7 +570,7 @@ public function getShare(string $id, ?string $secret = null, array $arguments =
569570
*/
570571
#[NoAdminRequired]
571572
#[ApiRoute(verb: 'GET', url: '/api/v1/shares')]
572-
public function getShares(?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?string $lastShareID, int $limit = 100): DataResponse {
573+
public function getShares(?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?string $filterState, ?string $lastShareID, int $limit = 100): DataResponse {
573574
/** @psalm-suppress DocblockTypeContradiction */
574575
if ($limit < 1) {
575576
return new DataResponse('The limit is too low.', Http::STATUS_BAD_REQUEST);
@@ -589,10 +590,18 @@ public function getShares(?string $filterSourceTypeClass, ?string $filterSourceT
589590
return new DataResponse('The filter source type is not registered: ' . $filterSourceTypeClass, Http::STATUS_BAD_REQUEST);
590591
}
591592

593+
if ($filterState !== null) {
594+
try {
595+
$filterState = ShareState::from($filterState);
596+
} catch (ValueError $valueError) {
597+
return new DataResponse($valueError->getMessage(), Http::STATUS_BAD_REQUEST);
598+
}
599+
}
600+
592601
try {
593602
$this->dbConnection->beginTransaction();
594603

595-
$shares = $this->manager->getShares($this->accessContext, $filterSourceTypeClass, $filterSourceTypeValue, $lastShareID, $limit);
604+
$shares = $this->manager->getShares($this->accessContext, $filterSourceTypeClass, $filterSourceTypeValue, $filterState, $lastShareID, $limit);
596605
$this->dbConnection->commit();
597606
return new DataResponse(Share::formatMultiple($this->registry, $this->l10nFactory, $this->urlGenerator, $this->userManager, $shares));
598607
} catch (Exception $exception) {

‎apps/sharing/openapi.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,6 +3112,16 @@
31123112
"minLength": 1
31133113
}
31143114
},
3115+
{
3116+
"name": "filterState",
3117+
"in": "query",
3118+
"description": "State to filter by.",
3119+
"schema": {
3120+
"type": "string",
3121+
"nullable": true,
3122+
"minLength": 1
3123+
}
3124+
},
31153125
{
31163126
"name": "lastShareID",
31173127
"in": "query",

‎apps/sharing/tests/Command/CommandTest.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,15 @@ protected function getShare(ShareAccessContext $accessContext, string $id): arra
393393
* @return SharingShare[]
394394
*/
395395
#[Override]
396-
protected function getShares(ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?string $lastShareID, ?int $limit): array {
396+
protected function getShares(ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?ShareState $filterState, ?string $lastShareID, ?int $limit): array {
397397
$stdout = $this->runCommand(
398398
$accessContext,
399399
GetShares::class,
400400
[],
401401
[
402402
['filter-source-type-class', $filterSourceTypeClass],
403403
['filter-source-type-value', $filterSourceTypeValue],
404+
['filter-state', $filterState?->value],
404405
['last-share-id', $lastShareID],
405406
['limit', $limit],
406407
],

‎apps/sharing/tests/Controller/ApiV1ControllerTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ protected function getShare(ShareAccessContext $accessContext, string $id): arra
177177
* @psalm-suppress MixedReturnTypeCoercion
178178
*/
179179
#[Override]
180-
protected function getShares(ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?string $lastShareID, ?int $limit): array {
181-
return $this->executeRequest($accessContext, function (ApiV1Controller $controller) use ($filterSourceTypeClass, $filterSourceTypeValue, $lastShareID, $limit): DataResponse {
180+
protected function getShares(ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?ShareState $filterState, ?string $lastShareID, ?int $limit): array {
181+
return $this->executeRequest($accessContext, function (ApiV1Controller $controller) use ($filterSourceTypeClass, $filterSourceTypeValue, $filterState, $lastShareID, $limit): DataResponse {
182182
if ($limit !== null) {
183183
/** @psalm-suppress ArgumentTypeCoercion */
184-
return $controller->getShares($filterSourceTypeClass, $filterSourceTypeValue, $lastShareID, $limit);
184+
return $controller->getShares($filterSourceTypeClass, $filterSourceTypeValue, $filterState?->value, $lastShareID, $limit);
185185
}
186186

187187
/** @psalm-suppress ArgumentTypeCoercion */
188-
return $controller->getShares($filterSourceTypeClass, $filterSourceTypeValue, $lastShareID);
188+
return $controller->getShares($filterSourceTypeClass, $filterSourceTypeValue, $filterState?->value, $lastShareID);
189189
});
190190
}
191191
}

‎lib/private/Sharing/SharingBackend.php‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ public function deleteShare(string $id): void {
509509

510510
#[\Override]
511511
public function getShare(ShareAccessContext $accessContext, string $id): Share {
512-
$shares = $this->list($accessContext, $id, null, null, null, null);
512+
$shares = $this->list($accessContext, $id, null, null, null, null, null);
513513
if (count($shares) !== 1) {
514514
throw new ShareNotFoundException();
515515
}
@@ -519,9 +519,9 @@ public function getShare(ShareAccessContext $accessContext, string $id): Share {
519519

520520
#[\Override]
521521
public function getShares(
522-
ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?string $lastShareID, ?int $limit,
522+
ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?ShareState $filterState, ?string $lastShareID, ?int $limit,
523523
): array {
524-
return $this->list($accessContext, null, $filterSourceTypeClass, $filterSourceTypeValue, $lastShareID, $limit);
524+
return $this->list($accessContext, null, $filterSourceTypeClass, $filterSourceTypeValue, $filterState, $lastShareID, $limit);
525525
}
526526

527527
#[\Override]
@@ -596,6 +596,7 @@ private function list(
596596
?string $filterShareID,
597597
?string $filterSourceTypeClass,
598598
?string $filterSourceTypeValue,
599+
?ShareState $filterState,
599600
?string $lastShareID,
600601
?int $limit,
601602
): array {
@@ -709,6 +710,10 @@ private function list(
709710
$qb->innerJoin('s', 'sharing_share_sources', 'ss', $qb->expr()->andX(...$sourceTypeFilters));
710711
}
711712

713+
if ($filterState instanceof \NCU\Sharing\ShareState) {
714+
$qb->andWhere($qb->expr()->eq('s.state', $qb->createNamedParameter($filterState->value)));
715+
}
716+
712717
if ($lastShareID !== null) {
713718
$qb->andWhere($qb->expr()->gt('s.id', $qb->createNamedParameter($lastShareID)));
714719
}

‎lib/private/Sharing/SharingManager.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,11 @@ public function getShare(ShareAccessContext $accessContext, string $id): Share {
685685

686686
#[\Override]
687687
public function getShares(
688-
ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?string $lastShareID, ?int $limit,
688+
ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?ShareState $filterState, ?string $lastShareID, ?int $limit,
689689
): array {
690690
$this->assertInTransaction();
691691

692-
return $this->backend->getShares($accessContext, $filterSourceTypeClass, $filterSourceTypeValue, $lastShareID, $limit);
692+
return $this->backend->getShares($accessContext, $filterSourceTypeClass, $filterSourceTypeValue, $filterState, $lastShareID, $limit);
693693
}
694694

695695
#[\Override]

‎lib/unstable/Sharing/ISharingBackend.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function getShare(ShareAccessContext $accessContext, string $id): Share;
179179
* @return list<Share>
180180
* @experimental 35.0.0
181181
*/
182-
public function getShares(ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?string $lastShareID, ?int $limit): array;
182+
public function getShares(ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?ShareState $filterState, ?string $lastShareID, ?int $limit): array;
183183

184184
/**
185185
* Check if a share ID belongs to this backend.

‎lib/unstable/Sharing/ISharingManager.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ public function deleteShare(ShareAccessContext $accessContext, Share $share): vo
194194
*/
195195
public function getShare(ShareAccessContext $accessContext, string $id): Share;
196196

197-
// TODO: Implement filtering by state.
198197
/**
199198
* Get multiple shares.
200199
*
@@ -204,5 +203,5 @@ public function getShare(ShareAccessContext $accessContext, string $id): Share;
204203
* @return list<Share>
205204
* @experimental 35.0.0
206205
*/
207-
public function getShares(ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?string $lastShareID, ?int $limit): array;
206+
public function getShares(ShareAccessContext $accessContext, ?string $filterSourceTypeClass, ?string $filterSourceTypeValue, ?ShareState $filterState, ?string $lastShareID, ?int $limit): array;
208207
}

‎openapi.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40464,6 +40464,16 @@
4046440464
"minLength": 1
4046540465
}
4046640466
},
40467+
{
40468+
"name": "filterState",
40469+
"in": "query",
40470+
"description": "State to filter by.",
40471+
"schema": {
40472+
"type": "string",
40473+
"nullable": true,
40474+
"minLength": 1
40475+
}
40476+
},
4046740477
{
4046840478
"name": "lastShareID",
4046940479
"in": "query",

0 commit comments

Comments
 (0)