Repository navigation
feat(contexts): give applications a menu with its own API #3135
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
Open
+4,828
−104
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
679ddbd
feat(contexts): give applications a menu with its own API
rubenvdlinde d09e3e3
refactor(contexts): keep the menu routes under the application
rubenvdlinde bd248eb
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde ae7ad7f
fix(contexts): follow the reviews of the pages API for the menu
rubenvdlinde 5975901
fix(contexts): lock the application first in every menu write
rubenvdlinde 08d432a
test(contexts): build the widget service with the node relation mapper
rubenvdlinde fa0ed79
fix(contexts): give every application a menu that opens its start page
rubenvdlinde 037a7ac
fix(contexts): delete the menu items of an application and a node in …
rubenvdlinde a03d4b8
refactor(contexts): check titles, descriptions and icons through one …
rubenvdlinde b8641e6
fix(contexts): check links of menu items and link cells by one rule
rubenvdlinde e857ca8
refactor(contexts): read the target and the order of the menu in the …
rubenvdlinde a49c5ee
fix(contexts): decide once which items the menu shows
rubenvdlinde 0665f6e
refactor(contexts): leave the menu's permission checks to the route a…
rubenvdlinde b8a2641
fix(contexts): refuse an order save based on another version of the menu
rubenvdlinde 5ba4049
fix(contexts): lock the table or view a menu item opens before it is …
rubenvdlinde 1f17a5c
perf(contexts): move the items of a deleted group only, within its ap…
rubenvdlinde 4babbde
perf(contexts): save the menu order with one prepared statement
rubenvdlinde 381a13a
fix(contexts): rate limit every menu write at 20 per minute
rubenvdlinde 1488306
fix(api): type the menu target as a closed union and regenerate the spec
rubenvdlinde ae38c8d
chore(release): bump the app to 3.0.0-dev.3 for the menu
rubenvdlinde f29e0f6
fix(contexts): type the menu responses and the locked node relation f…
rubenvdlinde fa7e6e0
test(pages): build the page service of the application lock test with…
rubenvdlinde e4aa0ed
chore(migration): describe the schema changes of the application menu
rubenvdlinde 4cd9616
fix(menu): let only who manages the application change its menu
rubenvdlinde 8786edd
fix(menu): keep the item that opens the start page instead of adding …
rubenvdlinde 8305179
fix(menu): hide items of tables and views a reader cannot read
rubenvdlinde 13ea9d5
refactor(menu): leave link cells and widget icons as they were
rubenvdlinde 7dfcfd4
fix(menu): trim icons like titles and descriptions
rubenvdlinde 75b8ee6
test(menu): build the application mapper with the circle helper
rubenvdlinde 4236e8f
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,241 @@ | ||
| <?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\Errors\PermissionError; | ||
| use OCA\Tables\Middleware\Attribute\RequirePermission; | ||
| use OCA\Tables\Model\MenuOrderEntry; | ||
| use OCA\Tables\Model\MenuTarget; | ||
| use OCA\Tables\ResponseDefinitions; | ||
| use OCA\Tables\Service\MenuService; | ||
| use OCP\AppFramework\Http; | ||
| use OCP\AppFramework\Http\Attribute\ApiRoute; | ||
| use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
| use OCP\AppFramework\Http\Attribute\RequestHeader; | ||
| 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 TablesMenuItem from ResponseDefinitions | ||
| * @psalm-import-type TablesMenuTarget from ResponseDefinitions | ||
| */ | ||
| class MenuController extends AOCSController { | ||
| public function __construct( | ||
| IRequest $request, | ||
| LoggerInterface $logger, | ||
| IL10N $n, | ||
| string $userId, | ||
| private readonly MenuService $menuService, | ||
| ) { | ||
| parent::__construct($request, $logger, $n, $userId); | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Get the menu of an application | ||
| * | ||
| * A flat list in display order; the items of a group carry its id as `parentId`. | ||
| * Readers who cannot manage the application do not get empty groups, nor items whose | ||
| * table or view the application does not let them read. The `ETag` is the version of the order, which | ||
| * a save of the order sends back as `If-Match`. | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @return DataResponse<Http::STATUS_OK, list<TablesMenuItem>, array{ETag: string}>|DataResponse<Http::STATUS_INTERNAL_SERVER_ERROR|Http::STATUS_NOT_FOUND, array{message: string}, array{}> | ||
| * | ||
| * 200: Menu 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}/menu', requirements: ['contextId' => '\d+'])] | ||
| public function index(int $contextId): DataResponse { | ||
| try { | ||
| $menu = $this->menuService->findAll($contextId, $this->userId); | ||
| return (new DataResponse($menu['menu']))->addHeader('ETag', self::entityTag($menu['version'])); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Add an item to the end of the menu of an application | ||
| * | ||
| * The `target` is `{pageId}` of a page or `{nodeRelId}` of a table or view of the | ||
| * application, or `{url}` of an http or https link. Without a target the item is a | ||
| * group, which needs a title. Without a title an item shows the title of what it opens. | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @param string|null $title Title of the item | ||
| * @param string|null $icon Name of the icon shown with the title | ||
| * @param string|null $description Description of the item | ||
| * @param TablesMenuTarget|null $target What the item opens | ||
| * @return DataResponse<Http::STATUS_OK, TablesMenuItem, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> | ||
| * | ||
| * 200: Item created | ||
| * 400: Invalid field, a target outside the application, or a full menu | ||
| * 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}/menu', requirements: ['contextId' => '\d+'])] | ||
| public function create(int $contextId, ?string $title = null, ?string $icon = null, ?string $description = null, ?array $target = null): DataResponse { | ||
| try { | ||
| return new DataResponse($this->menuService->create($contextId, $title, $icon, $description, $target === null ? null : MenuTarget::fromArray($target), $this->userId)); | ||
| } catch (BadRequestError $e) { | ||
| return $this->handleBadRequestError($e); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (PermissionError $e) { | ||
| return $this->handlePermissionError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Save the order and grouping of the menu of an application | ||
| * | ||
| * `items` lists every item of the menu exactly once, in display order, as | ||
| * `{id, parentId}`; `parentId` is a group at the top level listed earlier, or null. | ||
| * The `If-Match` header holds the `ETag` of the menu the list was built from. Either | ||
| * all of it is saved or nothing; the `ETag` of the answer is the new version. | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @param list<array{id: int, parentId: ?int}> $items Every item of the menu | ||
| * @return DataResponse<Http::STATUS_OK, list<TablesMenuItem>, array{ETag: string}>|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: Menu saved | ||
| * 400: No `If-Match`, an item missing or listed twice, or a parent that is not a group | ||
| * 403: No permissions | ||
| * 404: Not found | ||
| * 409: The menu changed since the version in `If-Match` | ||
| */ | ||
| #[NoAdminRequired] | ||
| #[UserRateLimit(limit: 20, period: 60)] | ||
| #[RequestHeader(name: 'if-match', description: 'The ETag of the menu the order was built from')] | ||
| #[RequirePermission(permission: Application::PERMISSION_MANAGE, typeParam: 'context', idParam: 'contextId')] | ||
| #[ApiRoute(verb: 'PUT', url: '/api/2/contexts/{contextId}/menu/order', requirements: ['contextId' => '\d+'])] | ||
| public function order(int $contextId, array $items): DataResponse { | ||
| try { | ||
| $order = MenuOrderEntry::listFromArray($items); | ||
| $version = self::matchedVersion($this->request->getHeader('if-match')); | ||
| $menu = $this->menuService->saveOrder($contextId, $order, $version, $this->userId); | ||
| return (new DataResponse($menu['menu']))->addHeader('ETag', self::entityTag($menu['version'])); | ||
| } catch (BadRequestError $e) { | ||
| return $this->handleBadRequestError($e); | ||
| } catch (ConflictError $e) { | ||
| return $this->handleConflictError($e); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (PermissionError $e) { | ||
| return $this->handlePermissionError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Change an item of a menu | ||
| * | ||
| * Fields left out keep their value; an empty title, icon or description removes it. | ||
| * A target replaces the previous one, except on the only item that opens the start | ||
| * page. Position and group change only through the order. | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @param int $itemId ID of the item | ||
| * @param string|null $title New title, empty to show the title of what the item opens | ||
| * @param string|null $icon New icon name, empty to remove it | ||
| * @param string|null $description New description, empty to remove it | ||
| * @param TablesMenuTarget|null $target New target | ||
| * @return DataResponse<Http::STATUS_OK, TablesMenuItem, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> | ||
| * | ||
| * 200: Item updated | ||
| * 400: Invalid field, a target outside the application, or a new target for the only item that opens 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: 'PATCH', url: '/api/2/contexts/{contextId}/menu/{itemId}', requirements: ['contextId' => '\d+', 'itemId' => '\d+'])] | ||
| public function update(int $contextId, int $itemId, ?string $title = null, ?string $icon = null, ?string $description = null, ?array $target = null): DataResponse { | ||
| try { | ||
| return new DataResponse($this->menuService->update($contextId, $itemId, $title, $icon, $description, $target === null ? null : MenuTarget::fromArray($target), $this->userId)); | ||
| } catch (BadRequestError $e) { | ||
| return $this->handleBadRequestError($e); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (PermissionError $e) { | ||
| return $this->handlePermissionError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * [api v2] Delete an item of a menu | ||
| * | ||
| * The items of a deleted group move to the top level. The only item that opens the | ||
| * start page cannot be deleted; it can be moved and renamed. | ||
| * | ||
| * @param int $contextId ID of the application | ||
| * @param int $itemId ID of the item | ||
| * @return DataResponse<Http::STATUS_OK, TablesMenuItem, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_FORBIDDEN|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string}, array{}> | ||
| * | ||
| * 200: Item deleted | ||
| * 400: The only item that opens 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}/menu/{itemId}', requirements: ['contextId' => '\d+', 'itemId' => '\d+'])] | ||
| public function destroy(int $contextId, int $itemId): DataResponse { | ||
| try { | ||
| return new DataResponse($this->menuService->delete($contextId, $itemId, $this->userId)); | ||
| } catch (BadRequestError $e) { | ||
| return $this->handleBadRequestError($e); | ||
| } catch (NotFoundError $e) { | ||
| return $this->handleNotFoundError($e); | ||
| } catch (PermissionError $e) { | ||
| return $this->handlePermissionError($e); | ||
| } catch (Exception $e) { | ||
| return $this->handleError($e); | ||
| } | ||
| } | ||
|
|
||
| private static function entityTag(string $version): string { | ||
| return '"' . $version . '"'; | ||
| } | ||
|
|
||
| /** | ||
| * The version an `If-Match` header names: one entity tag, compared strongly. | ||
| * | ||
| * @throws BadRequestError | ||
| */ | ||
| private static function matchedVersion(string $ifMatch): string { | ||
| if (preg_match('/^"([^"]+)"$/', trim($ifMatch), $matches) !== 1) { | ||
| throw new BadRequestError('A save of the order names the ETag of the menu it is based on in If-Match.'); | ||
| } | ||
| return $matches[1]; | ||
| } | ||
| } | ||
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,97 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Tables\Db; | ||
|
|
||
| use OCP\AppFramework\Db\Entity; | ||
|
|
||
| /** | ||
| * An item of the menu of an application. It opens at most one thing: a page of the | ||
| * application (`page_id`), a table or view of the application (`node_rel_id`), or a | ||
| * link (`url`); at most one of the three is set, which MenuService enforces. An item | ||
| * that opens nothing is a group; groups are top-level and only groups hold items. | ||
| * | ||
| * @method getContextId(): int | ||
| * @method setContextId(int $contextId): void | ||
| * @method getParentId(): ?int | ||
| * @method setParentId(?int $parentId): void | ||
| * @method getOrder(): int | ||
| * @method setOrder(int $order): void | ||
| * @method getTitle(): ?string | ||
| * @method setTitle(?string $title): void | ||
| * @method getIcon(): ?string | ||
| * @method setIcon(?string $icon): void | ||
| * @method getDescription(): ?string | ||
| * @method setDescription(?string $description): void | ||
| * @method getPageId(): ?int | ||
| * @method setPageId(?int $pageId): void | ||
| * @method getNodeRelId(): ?int | ||
| * @method setNodeRelId(?int $nodeRelId): void | ||
| * @method getUrl(): ?string | ||
| * @method setUrl(?string $url): void | ||
| */ | ||
| class MenuItem extends Entity { | ||
| public const MAX_ITEMS_PER_APPLICATION = 200; | ||
| public const URL_MAX_LENGTH = 2000; | ||
|
|
||
| protected ?int $contextId = null; | ||
| protected ?int $parentId = null; | ||
| protected ?int $order = null; | ||
| protected ?string $title = null; | ||
| protected ?string $icon = null; | ||
| protected ?string $description = null; | ||
| protected ?int $pageId = null; | ||
| protected ?int $nodeRelId = null; | ||
| protected ?string $url = null; | ||
|
|
||
| public function __construct() { | ||
| $this->addType('id', 'integer'); | ||
| $this->addType('contextId', 'integer'); | ||
| $this->addType('parentId', 'integer'); | ||
| $this->addType('order', 'integer'); | ||
| $this->addType('pageId', 'integer'); | ||
| $this->addType('nodeRelId', 'integer'); | ||
| } | ||
|
|
||
| /** | ||
| * The item without a title that opens the start page of an application. | ||
| */ | ||
| public static function forStartPage(int $contextId, int $startPageId): self { | ||
| $item = new self(); | ||
| $item->setContextId($contextId); | ||
| $item->setOrder(0); | ||
| $item->setPageId($startPageId); | ||
| return $item; | ||
| } | ||
|
|
||
| public function isGroup(): bool { | ||
| return $this->pageId === null && $this->nodeRelId === null && $this->url === null; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{pageId: int}|array{nodeRelId: int}|array{url: string}|null | ||
| */ | ||
| public function getTarget(): ?array { | ||
| return match (true) { | ||
| $this->pageId !== null => ['pageId' => $this->pageId], | ||
| $this->nodeRelId !== null => ['nodeRelId' => $this->nodeRelId], | ||
| $this->url !== null => ['url' => $this->url], | ||
| default => null, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @param array{pageId: int}|array{nodeRelId: int}|array{url: string} $target | ||
| */ | ||
| public function setTarget(array $target): void { | ||
|
enjeck marked this conversation as resolved.
Resolved
|
||
| $this->setPageId($target['pageId'] ?? null); | ||
| $this->setNodeRelId($target['nodeRelId'] ?? null); | ||
| $this->setUrl($target['url'] ?? null); | ||
| } | ||
| } | ||
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.