Skip to content
Open
Show file tree
Hide file tree
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 Nov 3, 2026
d09e3e3
refactor(contexts): keep the menu routes under the application
rubenvdlinde Nov 6, 2026
bd248eb
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde Nov 6, 2026
ae7ad7f
fix(contexts): follow the reviews of the pages API for the menu
rubenvdlinde Nov 9, 2026
5975901
fix(contexts): lock the application first in every menu write
rubenvdlinde Nov 17, 2026
08d432a
test(contexts): build the widget service with the node relation mapper
rubenvdlinde Nov 18, 2026
fa0ed79
fix(contexts): give every application a menu that opens its start page
rubenvdlinde Nov 19, 2026
037a7ac
fix(contexts): delete the menu items of an application and a node in …
rubenvdlinde Nov 19, 2026
a03d4b8
refactor(contexts): check titles, descriptions and icons through one …
rubenvdlinde Nov 19, 2026
b8641e6
fix(contexts): check links of menu items and link cells by one rule
rubenvdlinde Nov 19, 2026
e857ca8
refactor(contexts): read the target and the order of the menu in the …
rubenvdlinde Nov 19, 2026
a49c5ee
fix(contexts): decide once which items the menu shows
rubenvdlinde Nov 19, 2026
0665f6e
refactor(contexts): leave the menu's permission checks to the route a…
rubenvdlinde Nov 19, 2026
b8a2641
fix(contexts): refuse an order save based on another version of the menu
rubenvdlinde Nov 19, 2026
5ba4049
fix(contexts): lock the table or view a menu item opens before it is …
rubenvdlinde Nov 19, 2026
1f17a5c
perf(contexts): move the items of a deleted group only, within its ap…
rubenvdlinde Nov 19, 2026
4babbde
perf(contexts): save the menu order with one prepared statement
rubenvdlinde Nov 19, 2026
381a13a
fix(contexts): rate limit every menu write at 20 per minute
rubenvdlinde Nov 19, 2026
1488306
fix(api): type the menu target as a closed union and regenerate the spec
rubenvdlinde Nov 19, 2026
ae38c8d
chore(release): bump the app to 3.0.0-dev.3 for the menu
rubenvdlinde Nov 25, 2026
f29e0f6
fix(contexts): type the menu responses and the locked node relation f…
rubenvdlinde Nov 19, 2026
fa7e6e0
test(pages): build the page service of the application lock test with…
rubenvdlinde Nov 19, 2026
e4aa0ed
chore(migration): describe the schema changes of the application menu
rubenvdlinde Nov 21, 2026
4cd9616
fix(menu): let only who manages the application change its menu
rubenvdlinde Dec 1, 2026
8786edd
fix(menu): keep the item that opens the start page instead of adding …
rubenvdlinde Dec 1, 2026
8305179
fix(menu): hide items of tables and views a reader cannot read
rubenvdlinde Dec 1, 2026
13ea9d5
refactor(menu): leave link cells and widget icons as they were
rubenvdlinde Dec 1, 2026
7dfcfd4
fix(menu): trim icons like titles and descriptions
rubenvdlinde Dec 1, 2026
75b8ee6
test(menu): build the application mapper with the circle helper
rubenvdlinde Dec 1, 2026
4236e8f
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde Dec 1, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Share your tables and views with users and groups within your cloud.
Have a good time and manage whatever you want.

]]></description>
<version>3.0.0-dev.1</version>
<version>3.0.0-dev.3</version>
<licence>AGPL-3.0-or-later</licence>
<author>Nextcloud GmbH and Nextcloud contributors</author>
<namespace>Tables</namespace>
Expand Down
241 changes: 241 additions & 0 deletions lib/Controller/MenuController.php
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]
Comment thread
enjeck marked this conversation as resolved.
Resolved
#[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];
}
}
97 changes: 97 additions & 0 deletions lib/Db/MenuItem.php
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 {
Comment thread
enjeck marked this conversation as resolved.
Resolved
$this->setPageId($target['pageId'] ?? null);
$this->setNodeRelId($target['nodeRelId'] ?? null);
$this->setUrl($target['url'] ?? null);
}
}
Loading
Loading