Skip to content

Commit f457cbc

Browse files
committed
feat(FolderController): Add pagination for getFolders()
Signed-off-by: provokateurin <kate@provokateurin.de>
1 parent 48c64c0 commit f457cbc

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

lib/Controller/FolderController.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,21 @@ private function formatFolder(array $folder): array {
9393
* Gets all Groupfolders
9494
*
9595
* @param bool $applicable Filter by applicable groups
96+
* @param non-negative-int $offset Number of items to skip.
97+
* @param ?positive-int $limit Number of items to return.
9698
* @return DataResponse<Http::STATUS_OK, array<string, GroupFoldersFolder>, array{}>
9799
* @throws OCSNotFoundException Storage not found
100+
* @throws OCSBadRequestException Wrong limit used
98101
*
99102
* 200: Groupfolders returned
100103
*/
101104
#[NoAdminRequired]
102105
#[FrontpageRoute(verb: 'GET', url: '/folders')]
103-
public function getFolders(bool $applicable = false): DataResponse {
106+
public function getFolders(bool $applicable = false, int $offset = 0, ?int $limit = null): DataResponse {
107+
if ($limit !== null && $limit <= 0) {
108+
throw new OCSBadRequestException('The limit must be greater than 0.');
109+
}
110+
104111
$storageId = $this->getRootFolderStorageId();
105112
if ($storageId === null) {
106113
throw new OCSNotFoundException();
@@ -111,8 +118,17 @@ public function getFolders(bool $applicable = false): DataResponse {
111118
// Make them string-indexed for OpenAPI JSON output
112119
$folders[(string)$id] = $this->formatFolder($folder);
113120
}
121+
122+
// Make sure the order is always the same, otherwise pagination could break.
123+
ksort($folders);
124+
114125
$isAdmin = $this->delegationService->isAdminNextcloud() || $this->delegationService->isDelegatedAdmin();
115126
if ($isAdmin && !$applicable) {
127+
// If only the default values are provided the pagination can be skipped.
128+
if ($offset !== 0 || $limit !== null) {
129+
$folders = array_slice($folders, $offset, $limit, true);
130+
}
131+
116132
return new DataResponse($folders);
117133
}
118134

@@ -124,6 +140,11 @@ public function getFolders(bool $applicable = false): DataResponse {
124140
$folders = array_filter(array_map($this->filterNonAdminFolder(...), $folders));
125141
}
126142

143+
// If only the default values are provided the pagination can be skipped.
144+
if ($offset !== 0 || $limit !== null) {
145+
$folders = array_slice($folders, $offset, $limit, true);
146+
}
147+
127148
return new DataResponse($folders);
128149
}
129150

openapi.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,29 @@
472472
]
473473
}
474474
},
475+
{
476+
"name": "offset",
477+
"in": "query",
478+
"description": "Number of items to skip.",
479+
"schema": {
480+
"type": "integer",
481+
"format": "int64",
482+
"default": 0,
483+
"minimum": 0
484+
}
485+
},
486+
{
487+
"name": "limit",
488+
"in": "query",
489+
"description": "Number of items to return.",
490+
"schema": {
491+
"type": "integer",
492+
"format": "int64",
493+
"nullable": true,
494+
"default": null,
495+
"minimum": 1
496+
}
497+
},
475498
{
476499
"name": "OCS-APIRequest",
477500
"in": "header",
@@ -544,6 +567,34 @@
544567
}
545568
}
546569
}
570+
},
571+
"400": {
572+
"description": "Wrong limit used",
573+
"content": {
574+
"application/json": {
575+
"schema": {
576+
"type": "object",
577+
"required": [
578+
"ocs"
579+
],
580+
"properties": {
581+
"ocs": {
582+
"type": "object",
583+
"required": [
584+
"meta",
585+
"data"
586+
],
587+
"properties": {
588+
"meta": {
589+
"$ref": "#/components/schemas/OCSMeta"
590+
},
591+
"data": {}
592+
}
593+
}
594+
}
595+
}
596+
}
597+
}
547598
}
548599
}
549600
},

src/types/openapi/openapi.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,10 @@ export interface operations {
410410
query?: {
411411
/** @description Filter by applicable groups */
412412
applicable?: 0 | 1;
413+
/** @description Number of items to skip. */
414+
offset?: number;
415+
/** @description Number of items to return. */
416+
limit?: number | null;
413417
};
414418
header: {
415419
/** @description Required to be true for the API request to pass */
@@ -436,6 +440,20 @@ export interface operations {
436440
};
437441
};
438442
};
443+
/** @description Wrong limit used */
444+
400: {
445+
headers: {
446+
[name: string]: unknown;
447+
};
448+
content: {
449+
"application/json": {
450+
ocs: {
451+
meta: components["schemas"]["OCSMeta"];
452+
data: unknown;
453+
};
454+
};
455+
};
456+
};
439457
/** @description Storage not found */
440458
404: {
441459
headers: {

0 commit comments

Comments
 (0)