From 35252b9caf88ba46fa8821eb41c8a557170851d8 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Fri, 19 Jan 2024 23:12:25 +0100 Subject: [PATCH] feat(dashboard): Add endpoints to get the layout and statuses Signed-off-by: provokateurin --- apps/dashboard/appinfo/routes.php | 2 + .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + .../lib/Controller/DashboardApiController.php | 26 ++++ .../lib/Controller/DashboardController.php | 20 +-- .../lib/Service/DashboardService.php | 62 ++++++++ apps/dashboard/openapi.json | 140 ++++++++++++++++++ 7 files changed, 237 insertions(+), 15 deletions(-) create mode 100644 apps/dashboard/lib/Service/DashboardService.php diff --git a/apps/dashboard/appinfo/routes.php b/apps/dashboard/appinfo/routes.php index 21c8ea7b2de05..6fcc92097416a 100644 --- a/apps/dashboard/appinfo/routes.php +++ b/apps/dashboard/appinfo/routes.php @@ -33,7 +33,9 @@ ['name' => 'dashboardApi#getWidgets', 'url' => '/api/v1/widgets', 'verb' => 'GET'], ['name' => 'dashboardApi#getWidgetItems', 'url' => '/api/v1/widget-items', 'verb' => 'GET'], ['name' => 'dashboardApi#getWidgetItemsV2', 'url' => '/api/v2/widget-items', 'verb' => 'GET'], + ['name' => 'dashboardApi#getLayout', 'url' => '/api/v3/layout', 'verb' => 'GET'], ['name' => 'dashboardApi#updateLayout', 'url' => '/api/v3/layout', 'verb' => 'POST'], + ['name' => 'dashboardApi#getStatuses', 'url' => '/api/v3/statuses', 'verb' => 'GET'], ['name' => 'dashboardApi#updateStatuses', 'url' => '/api/v3/statuses', 'verb' => 'POST'], ] ]; diff --git a/apps/dashboard/composer/composer/autoload_classmap.php b/apps/dashboard/composer/composer/autoload_classmap.php index 0bf9a3f8d9b77..4a8609daeb0cc 100644 --- a/apps/dashboard/composer/composer/autoload_classmap.php +++ b/apps/dashboard/composer/composer/autoload_classmap.php @@ -10,4 +10,5 @@ 'OCA\\Dashboard\\Controller\\DashboardApiController' => $baseDir . '/../lib/Controller/DashboardApiController.php', 'OCA\\Dashboard\\Controller\\DashboardController' => $baseDir . '/../lib/Controller/DashboardController.php', 'OCA\\Dashboard\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php', + 'OCA\\Dashboard\\Service\\DashboardService' => $baseDir . '/../lib/Service/DashboardService.php', ); diff --git a/apps/dashboard/composer/composer/autoload_static.php b/apps/dashboard/composer/composer/autoload_static.php index fc0264a32c1d3..72f839d33150b 100644 --- a/apps/dashboard/composer/composer/autoload_static.php +++ b/apps/dashboard/composer/composer/autoload_static.php @@ -25,6 +25,7 @@ class ComposerStaticInitDashboard 'OCA\\Dashboard\\Controller\\DashboardApiController' => __DIR__ . '/..' . '/../lib/Controller/DashboardApiController.php', 'OCA\\Dashboard\\Controller\\DashboardController' => __DIR__ . '/..' . '/../lib/Controller/DashboardController.php', 'OCA\\Dashboard\\ResponseDefinitions' => __DIR__ . '/..' . '/../lib/ResponseDefinitions.php', + 'OCA\\Dashboard\\Service\\DashboardService' => __DIR__ . '/..' . '/../lib/Service/DashboardService.php', ); public static function getInitializer(ClassLoader $loader) diff --git a/apps/dashboard/lib/Controller/DashboardApiController.php b/apps/dashboard/lib/Controller/DashboardApiController.php index 65ec8235a7e0b..bd7cb40260094 100644 --- a/apps/dashboard/lib/Controller/DashboardApiController.php +++ b/apps/dashboard/lib/Controller/DashboardApiController.php @@ -29,6 +29,7 @@ namespace OCA\Dashboard\Controller; use OCA\Dashboard\ResponseDefinitions; +use OCA\Dashboard\Service\DashboardService; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\OCSController; @@ -60,6 +61,7 @@ public function __construct( private IManager $dashboardManager, private IConfig $config, private ?string $userId, + private DashboardService $service, ) { parent::__construct($appName, $request); } @@ -190,6 +192,18 @@ public function getWidgets(): DataResponse { return new DataResponse($items); } + /** + * Get the layout + * + * @NoAdminRequired + * @return DataResponse}, array{}> + * + * 200: Layout returned + */ + public function getLayout(): DataResponse { + return new DataResponse(['layout' => $this->service->getLayout()]); + } + /** * Update the layout * @@ -204,6 +218,18 @@ public function updateLayout(array $layout): DataResponse { return new DataResponse(['layout' => $layout]); } + /** + * Get the statuses + * + * @NoAdminRequired + * @return DataResponse}, array{}> + * + * 200: Statuses returned + */ + public function getStatuses(): DataResponse { + return new DataResponse(['statuses' => $this->service->getStatuses()]); + } + /** * Update the statuses * diff --git a/apps/dashboard/lib/Controller/DashboardController.php b/apps/dashboard/lib/Controller/DashboardController.php index 61b7ccc83ad6c..8375858cbee1c 100644 --- a/apps/dashboard/lib/Controller/DashboardController.php +++ b/apps/dashboard/lib/Controller/DashboardController.php @@ -30,7 +30,7 @@ */ namespace OCA\Dashboard\Controller; -use JsonException; +use OCA\Dashboard\Service\DashboardService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\Attribute\OpenAPI; @@ -54,7 +54,8 @@ public function __construct( private IManager $dashboardManager, private IConfig $config, private IL10N $l10n, - private ?string $userId + private ?string $userId, + private DashboardService $service, ) { parent::__construct($appName, $request); } @@ -68,8 +69,6 @@ public function index(): TemplateResponse { \OCP\Util::addStyle('dashboard', 'dashboard'); \OCP\Util::addScript('dashboard', 'main', 'theming'); - $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar'); - $userLayout = array_filter(explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)), fn (string $value) => $value !== ''); $widgets = array_map(function (IWidget $widget) { return [ 'id' => $widget->getId(), @@ -78,19 +77,10 @@ public function index(): TemplateResponse { 'url' => $widget->getUrl() ]; }, $this->dashboardManager->getWidgets()); - $configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', ''); - try { - // Parse the old format - $statuses = json_decode($configStatuses, true, 512, JSON_THROW_ON_ERROR); - // We avoid getting an empty array as it will not produce an object in UI's JS - $statuses = array_keys(array_filter($statuses, static fn (bool $value) => $value)); - } catch (JsonException $e) { - $statuses = array_filter(explode(',', $configStatuses), fn (string $value) => $value !== ''); - } $this->initialState->provideInitialState('panels', $widgets); - $this->initialState->provideInitialState('statuses', $statuses); - $this->initialState->provideInitialState('layout', $userLayout); + $this->initialState->provideInitialState('statuses', $this->service->getStatuses()); + $this->initialState->provideInitialState('layout', $this->service->getLayout()); $this->initialState->provideInitialState('appStoreEnabled', $this->config->getSystemValueBool('appstoreenabled', true)); $this->initialState->provideInitialState('firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1'); $this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0'); diff --git a/apps/dashboard/lib/Service/DashboardService.php b/apps/dashboard/lib/Service/DashboardService.php new file mode 100644 index 0000000000000..8b8808266282b --- /dev/null +++ b/apps/dashboard/lib/Service/DashboardService.php @@ -0,0 +1,62 @@ + + * + * @author Kate Döen + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\Dashboard\Service; + +use JsonException; +use OCP\IConfig; + +class DashboardService { + public function __construct( + private IConfig $config, + private String $userId, + ) { + + } + + /** + * @return list + */ + public function getLayout(): array { + $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar'); + return array_values(array_filter(explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)), fn (string $value) => $value !== '')); + } + + /** + * @return list + */ + public function getStatuses() { + $configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', ''); + try { + // Parse the old format + /** @var array $statuses */ + $statuses = json_decode($configStatuses, true, 512, JSON_THROW_ON_ERROR); + // We avoid getting an empty array as it will not produce an object in UI's JS + return array_keys(array_filter($statuses, static fn (bool $value) => $value)); + } catch (JsonException $e) { + return array_values(array_filter(explode(',', $configStatuses), fn (string $value) => $value !== '')); + } + } +} diff --git a/apps/dashboard/openapi.json b/apps/dashboard/openapi.json index 495479ae83776..7c537b4604cab 100644 --- a/apps/dashboard/openapi.json +++ b/apps/dashboard/openapi.json @@ -432,6 +432,76 @@ } }, "/ocs/v2.php/apps/dashboard/api/v3/layout": { + "get": { + "operationId": "dashboard_api-get-layout", + "summary": "Get the layout", + "tags": [ + "dashboard_api" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "parameters": [ + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Layout returned", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": { + "type": "object", + "required": [ + "layout" + ], + "properties": { + "layout": { + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + } + } + } + } + } + } + }, "post": { "operationId": "dashboard_api-update-layout", "summary": "Update the layout", @@ -516,6 +586,76 @@ } }, "/ocs/v2.php/apps/dashboard/api/v3/statuses": { + "get": { + "operationId": "dashboard_api-get-statuses", + "summary": "Get the statuses", + "tags": [ + "dashboard_api" + ], + "security": [ + { + "bearer_auth": [] + }, + { + "basic_auth": [] + } + ], + "parameters": [ + { + "name": "OCS-APIRequest", + "in": "header", + "description": "Required to be true for the API request to pass", + "required": true, + "schema": { + "type": "boolean", + "default": true + } + } + ], + "responses": { + "200": { + "description": "Statuses returned", + "content": { + "application/json": { + "schema": { + "type": "object", + "required": [ + "ocs" + ], + "properties": { + "ocs": { + "type": "object", + "required": [ + "meta", + "data" + ], + "properties": { + "meta": { + "$ref": "#/components/schemas/OCSMeta" + }, + "data": { + "type": "object", + "required": [ + "statuses" + ], + "properties": { + "statuses": { + "type": "array", + "items": { + "type": "string" + } + } + } + } + } + } + } + } + } + } + } + } + }, "post": { "operationId": "dashboard_api-update-statuses", "summary": "Update the statuses",