Skip to content
Merged
Show file tree
Hide file tree
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 Oct 22, 2026
1a3b8c6
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde Oct 22, 2026
10f8f81
feat(api): read a page of an application by its technical name
rubenvdlinde Nov 6, 2026
4989909
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde Nov 6, 2026
d739b8d
fix(api): follow the review of the pages API
rubenvdlinde Nov 9, 2026
9421c5c
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde Nov 9, 2026
7737689
fix(api): skip the row lock of a new page on SQLite
rubenvdlinde Nov 10, 2026
035ec31
test(api): write the technical name lookup as an arrow function, as R…
rubenvdlinde Nov 10, 2026
86c9c06
refactor(pages): lock the application through ContextMapper when a pa…
rubenvdlinde Nov 16, 2026
c6da029
test(api): list the tables widget type
rubenvdlinde Nov 18, 2026
9ede006
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde Nov 18, 2026
640d66c
fix(api): lock the application first when a page is saved or deleted
rubenvdlinde Nov 19, 2026
522f127
fix(api): refuse a widget of another page and answer with the stored …
rubenvdlinde Nov 19, 2026
67448e4
fix(contexts): lock the applications first when a table or view leave…
rubenvdlinde Nov 20, 2026
0fe78d9
fix(api): keep the slug of a page and add a grid page with createGrid…
rubenvdlinde Nov 20, 2026
aa360a5
chore(api): regenerate the OpenAPI spec and TypeScript types
rubenvdlinde Nov 20, 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
9 changes: 9 additions & 0 deletions lib/Controller/AOCSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Exception;
use OCA\Tables\AppInfo\Application;
use OCA\Tables\Errors\BadRequestError;
use OCA\Tables\Errors\ConflictError;
use OCA\Tables\Errors\InternalError;
use OCA\Tables\Errors\NotFoundError;
use OCA\Tables\Errors\PermissionError;
Expand Down Expand Up @@ -58,6 +59,14 @@ protected function handleNotFoundError(NotFoundError $e): DataResponse {
return new DataResponse(['message' => $this->n->t('A not found error occurred. More details can be found in the logs. Please reach out to your administration.')], Http::STATUS_NOT_FOUND);
}

/**
* @return DataResponse<Http::STATUS_CONFLICT, array{message: string}, array{}>
*/
protected function handleConflictError(ConflictError $e): DataResponse {
$this->logger->info('A conflict was encountered: ' . $e->getMessage(), ['exception' => $e]);
return new DataResponse(['message' => $e->getMessage()], Http::STATUS_CONFLICT);
}

/**
* @param BadRequestError $e
* @return DataResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}>
Expand Down
218 changes: 218 additions & 0 deletions lib/Controller/ContextPageController.php
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+'])]
Comment thread
AndyScherzinger marked this conversation as resolved.
Resolved
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(),
)));
}
}
20 changes: 20 additions & 0 deletions lib/Db/ContextNodeRelationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ public function deleteAllByContextId(int $contextId): void {
$qb->executeStatement();
}

/**
* The ids of the applications that hold a node.
*
* @return list<int>
* @throws Exception
*/
public function getContextIdsForNode(int $nodeId, int $nodeType): array {
$qb = $this->db->getQueryBuilder();
$qb->selectDistinct('context_id')->from($this->table)
->where($qb->expr()->eq('node_id', $qb->createNamedParameter($nodeId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('node_type', $qb->createNamedParameter($nodeType, IQueryBuilder::PARAM_INT)));
$result = $qb->executeQuery();
$contextIds = [];
while ($row = $result->fetchAssociative()) {
$contextIds[] = (int)$row['context_id'];
}
$result->closeCursor();
return $contextIds;
}

public function getRelIdsForNode(int $nodeId, int $nodeType): array {
$qb = $this->db->getQueryBuilder();
$qb->select('id')->from($this->table)
Expand Down
3 changes: 1 addition & 2 deletions lib/Db/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class Page extends Entity implements \JsonSerializable {
public const TYPE_GRID = 'grid';
public const START_PAGE_SLUG = 'dashboard';
public const DEFAULT_SLUG_PREFIX = 'page_';
public const TITLE_MAX_LENGTH = 200;
public const DESCRIPTION_MAX_LENGTH = 2000;

protected ?int $contextId = null;
protected ?string $pageType = null;
Expand Down Expand Up @@ -64,6 +62,7 @@ public function jsonSerialize(): array {
'title' => $this->getTitle(),
'icon' => $this->getIcon(),
'description' => $this->getDescription(),
'version' => (int)$this->getVersion(),
];
}
}
68 changes: 68 additions & 0 deletions lib/Db/PageMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace OCA\Tables\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\AppFramework\Db\TTransactional;
use OCP\DB\Exception;
Expand Down Expand Up @@ -37,6 +39,72 @@ public function insertWithDefaultName(Page $page): Page {
}, $this->db);
}

/**
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws Exception
*/
public function findById(int $pageId): Page {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->table)
->where($qb->expr()->eq('id', $qb->createNamedParameter($pageId, IQueryBuilder::PARAM_INT)));

return $this->findEntity($qb);
}

/**
* @throws DoesNotExistException
* @throws MultipleObjectsReturnedException
* @throws Exception
*/
public function findBySlug(int $contextId, string $slug): Page {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from($this->table)
->where($qb->expr()->eq('context_id', $qb->createNamedParameter($contextId, IQueryBuilder::PARAM_INT)))
->andWhere($qb->expr()->eq('slug', $qb->createNamedParameter($slug)));

return $this->findEntity($qb);
}

/**
* Raises the version of a page, the first write of a save. It locks the page row until
* the transaction ends, so saves and the delete of one page run one after the other.
*
* @param ?int $expected the version the save was based on, or null to take any
* @return bool false when the page is gone or has another version
* @throws Exception
*/
public function raiseVersion(int $pageId, ?int $expected): bool {
$qb = $this->db->getQueryBuilder();
$qb->update($this->table)
->set('version', $qb->func()->add('version', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
->where($qb->expr()->eq('id', $qb->createNamedParameter($pageId, IQueryBuilder::PARAM_INT)));
if ($expected !== null) {
$qb->andWhere($qb->expr()->eq('version', $qb->createNamedParameter($expected, IQueryBuilder::PARAM_INT)));
}
return $qb->executeStatement() === 1;
}

/**
* @throws DoesNotExistException
* @throws Exception
*/
public function findVersion(int $pageId): int {
$qb = $this->db->getQueryBuilder();
$qb->select('version')
->from($this->table)
->where($qb->expr()->eq('id', $qb->createNamedParameter($pageId, IQueryBuilder::PARAM_INT)));
$result = $qb->executeQuery();
$version = $result->fetchOne();
$result->closeCursor();
if ($version === false) {
throw new DoesNotExistException('Page ' . $pageId . ' does not exist');
}
return (int)$version;
}

/**
* @return int[]
*/
Expand Down
10 changes: 6 additions & 4 deletions lib/Db/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,14 @@ public function setSettingsArray(array $settings): void {
}

/**
* The settings as an object, so they serialise as a JSON object also when empty,
* ordered by name: a JSON column does not keep the stored order on every database.
* The settings for a response, ordered by name: an array, which OCS also renders as
* XML, or an empty object when there are none, so JSON shows `{}` and not `[]`.
*
* @return array<string, TablesWidgetSettingValue>|\stdClass
*/
public function getSettingsObject(): \stdClass {
public function getSettingsForResponse(): array|\stdClass {
$settings = $this->getSettingsArray();
ksort($settings);
return (object)$settings;
return $settings === [] ? new \stdClass() : $settings;
}
}
Loading
Loading