Repository navigation
feat(api): pages and widgets on api/2 #3133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
57efac7
feat(api): add grid pages and widget types to api/2
rubenvdlinde 1a3b8c6
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde 10f8f81
feat(api): read a page of an application by its technical name
rubenvdlinde 4989909
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde d739b8d
fix(api): follow the review of the pages API
rubenvdlinde 9421c5c
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde 7737689
fix(api): skip the row lock of a new page on SQLite
rubenvdlinde 035ec31
test(api): write the technical name lookup as an arrow function, as R…
rubenvdlinde 86c9c06
refactor(pages): lock the application through ContextMapper when a pa…
rubenvdlinde c6da029
test(api): list the tables widget type
rubenvdlinde 9ede006
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde 640d66c
fix(api): lock the application first when a page is saved or deleted
rubenvdlinde 522f127
fix(api): refuse a widget of another page and answer with the stored …
rubenvdlinde 67448e4
fix(contexts): lock the applications first when a table or view leave…
rubenvdlinde 0fe78d9
fix(api): keep the slug of a page and add a grid page with createGrid…
rubenvdlinde aa360a5
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Tables\Controller; | ||
|
|
||
| use OCA\Tables\AppInfo\Application; | ||
| use OCA\Tables\Errors\BadRequestError; | ||
| use OCA\Tables\Errors\ConflictError; | ||
| use OCA\Tables\Errors\NotFoundError; | ||
| use OCA\Tables\Middleware\Attribute\RequirePermission; | ||
| use OCA\Tables\ResponseDefinitions; | ||
| use OCA\Tables\Service\GridWidgetTypes; | ||
| use OCA\Tables\Service\PageService; | ||
| use OCP\AppFramework\Http; | ||
| use OCP\AppFramework\Http\Attribute\ApiRoute; | ||
| use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
| use OCP\AppFramework\Http\Attribute\UserRateLimit; | ||
| use OCP\AppFramework\Http\DataResponse; | ||
| use OCP\DB\Exception; | ||
| use OCP\IL10N; | ||
| use OCP\IRequest; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| /** | ||
| * @psalm-import-type TablesPage from ResponseDefinitions | ||
| * @psalm-import-type TablesWidgetType from ResponseDefinitions | ||
| * @psalm-import-type TablesWidgetInput from ResponseDefinitions | ||
| */ | ||
| class ContextPageController extends AOCSController { | ||
| public function __construct( | ||
| IRequest $request, | ||
| LoggerInterface $logger, | ||
| IL10N $n, | ||
| string $userId, | ||
| private readonly PageService $pageService, | ||
| private readonly GridWidgetTypes $gridWidgetTypes, | ||
| ) { | ||
| parent::__construct($request, $logger, $n, $userId); | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Get a page of an application with its widgets | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @param int $pageId ID of the page | ||
| * @return DataResponse<Http::STATUS_OK, TablesPage, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}> | ||
| * | ||
| * 200: Page returned | ||
| * 404: Not found, also when the user has no access to the application | ||
| */ | ||
| #[NoAdminRequired] | ||
| #[RequirePermission(permission: Application::PERMISSION_READ, typeParam: 'context', idParam: 'contextId')] | ||
| #[ApiRoute(verb: 'GET', url: '/api/2/contexts/{contextId}/pages/{pageId}', requirements: ['contextId' => '\d+', 'pageId' => '\d+'])] | ||
| public function show(int $contextId, int $pageId): DataResponse { | ||
| try { | ||
| return new DataResponse($this->pageService->find($contextId, $pageId)); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Get a page of an application by its slug | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @param string $slug Slug of the page | ||
| * @return DataResponse<Http::STATUS_OK, TablesPage, array{}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}> | ||
| * | ||
| * 200: Page returned | ||
| * 404: Not found, also when the user has no access to the application | ||
| */ | ||
| #[NoAdminRequired] | ||
| #[RequirePermission(permission: Application::PERMISSION_READ, typeParam: 'context', idParam: 'contextId')] | ||
| #[ApiRoute(verb: 'GET', url: '/api/2/contexts/{contextId}/pages/{slug}', requirements: ['contextId' => '\d+', 'slug' => '[a-z][a-z0-9_]*'])] | ||
| public function showBySlug(int $contextId, string $slug): DataResponse { | ||
| try { | ||
| return new DataResponse($this->pageService->findBySlug($contextId, $slug)); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Add a grid page to an application | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @param string $title Title of the page | ||
| * @param string|null $slug Slug of the page, unique in the application and never changed; `page_<id>` when left out, a form reserved for automatic names | ||
| * @param string|null $icon Name of the icon shown with the title | ||
| * @param string|null $description Description of the page | ||
| * @return DataResponse<Http::STATUS_OK, TablesPage, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> | ||
| * | ||
| * 200: Page created | ||
| * 400: Invalid title, icon or description, or an invalid or taken slug | ||
| * 403: No permissions | ||
| * 404: Not found | ||
| */ | ||
| #[NoAdminRequired] | ||
| #[UserRateLimit(limit: 20, period: 60)] | ||
| #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] | ||
| #[ApiRoute(verb: 'POST', url: '/api/2/contexts/{contextId}/pages', requirements: ['contextId' => '\d+'])] | ||
| public function createGridPage(int $contextId, string $title, ?string $slug = null, ?string $icon = null, ?string $description = null): DataResponse { | ||
| try { | ||
| return new DataResponse($this->pageService->createGridPage($contextId, $title, $slug, $icon, $description)); | ||
| } catch (BadRequestError $e) { | ||
| return $this->handleBadRequestError($e); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Change a page or its widgets | ||
| * | ||
| * Fields left out keep their value; an empty icon or description removes it. A given | ||
| * `widgets` list replaces the widgets of the grid page: listed widgets with an `id` | ||
| * are updated, others are added, and widgets not listed are deleted. | ||
| * | ||
| * A widget has a `type`, `gridX`, `gridY`, `gridWidth` and `gridHeight` on a grid of | ||
| * 12 columns, `settings` following the schema of its type, and optionally an `id`, | ||
| * `title`, `icon` and `description`. A data widget's `source` is `{nodeRelId}` of a | ||
| * table or view of this application, or null. | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @param int $pageId ID of the page | ||
| * @param string|null $title New title | ||
| * @param string|null $slug Slug of the page; it never changes, so only the current slug is accepted | ||
| * @param string|null $icon New icon name, empty to remove it | ||
| * @param string|null $description New description, empty to remove it | ||
| * @param list<TablesWidgetInput>|null $widgets All widgets of the page | ||
| * @param int|null $version The version of the page the widgets were edited from, needed with widgets | ||
| * @return DataResponse<Http::STATUS_OK, TablesPage, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_CONFLICT|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> | ||
| * | ||
| * 200: Page updated | ||
| * 400: Invalid page field or widget, a slug other than the one of the page, a widget id that is not on the page, or too many widgets | ||
| * 403: No permissions | ||
| * 404: Not found | ||
| * 409: The page was saved by someone else since the given version | ||
| */ | ||
| #[NoAdminRequired] | ||
| #[UserRateLimit(limit: 60, period: 60)] | ||
| #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] | ||
| #[ApiRoute(verb: 'PATCH', url: '/api/2/contexts/{contextId}/pages/{pageId}', requirements: ['contextId' => '\d+', 'pageId' => '\d+'])] | ||
| public function update(int $contextId, int $pageId, ?string $title = null, ?string $slug = null, ?string $icon = null, ?string $description = null, ?array $widgets = null, ?int $version = null): DataResponse { | ||
| try { | ||
| return new DataResponse($this->pageService->update($contextId, $pageId, $title, $slug, $icon, $description, $widgets, $version)); | ||
| } catch (BadRequestError $e) { | ||
| return $this->handleBadRequestError($e); | ||
| } catch (ConflictError $e) { | ||
| return $this->handleConflictError($e); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Delete a grid page with its widgets | ||
| * | ||
| * The start page of an application cannot be deleted. | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @param int $pageId ID of the page | ||
| * @return DataResponse<Http::STATUS_OK, TablesPage, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> | ||
| * | ||
| * 200: Page deleted | ||
| * 400: The page is the start page | ||
| * 403: No permissions | ||
| * 404: Not found | ||
| */ | ||
| #[NoAdminRequired] | ||
| #[UserRateLimit(limit: 20, period: 60)] | ||
| #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] | ||
| #[ApiRoute(verb: 'DELETE', url: '/api/2/contexts/{contextId}/pages/{pageId}', requirements: ['contextId' => '\d+', 'pageId' => '\d+'])] | ||
| public function destroy(int $contextId, int $pageId): DataResponse { | ||
| try { | ||
| return new DataResponse($this->pageService->delete($contextId, $pageId)); | ||
| } catch (BadRequestError $e) { | ||
| return $this->handleBadRequestError($e); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Get the widget types a grid page can hold | ||
| * | ||
| * Every type carries the schema of its settings and whether it shows a table or view. | ||
| * | ||
| * @return DataResponse<Http::STATUS_OK, list<TablesWidgetType>, array{}> | ||
| * | ||
| * 200: Widget types returned | ||
| */ | ||
| #[NoAdminRequired] | ||
| #[ApiRoute(verb: 'GET', url: '/api/2/widget-types')] | ||
| public function widgetTypes(): DataResponse { | ||
| // a type without settings shows them as {}, the object the schema promises | ||
| return new DataResponse(array_values(array_map( | ||
| static fn (array $type): array => $type['settings'] === [] ? ['settings' => new \stdClass()] + $type : $type, | ||
| $this->gridWidgetTypes->all(), | ||
| ))); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.