Skip to content

Commit

Permalink
feat(dashboard): Add endpoints to get the layout and statuses
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Feb 27, 2024
1 parent e18d856 commit 571ce41
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 16 deletions.
2 changes: 2 additions & 0 deletions apps/dashboard/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '/layout', 'verb' => 'GET'],
['name' => 'dashboardApi#updateLayout', 'url' => '/layout', 'verb' => 'POST'],
['name' => 'dashboardApi#getStatuses', 'url' => '/statuses', 'verb' => 'GET'],
['name' => 'dashboardApi#updateStatuses', 'url' => '/statuses', 'verb' => 'POST'],
]
];
1 change: 1 addition & 0 deletions apps/dashboard/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
1 change: 1 addition & 0 deletions apps/dashboard/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 27 additions & 1 deletion apps/dashboard/lib/Controller/DashboardApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +67,8 @@ public function __construct(
IRequest $request,
IManager $dashboardManager,
IConfig $config,
?string $userId
?string $userId,
private DashboardService $service,
) {
parent::__construct($appName, $request);

Expand Down Expand Up @@ -201,6 +203,18 @@ public function getWidgets(): DataResponse {
return new DataResponse($items);
}

/**
* Get the layout
*
* @NoAdminRequired
* @return DataResponse<Http::STATUS_OK, array{layout: list<string>}, array{}>
*
* 200: Layout returned
*/
public function getLayout(): DataResponse {
return new DataResponse(['layout' => $this->service->getLayout()]);
}

/**
* Update the layout
*
Expand All @@ -215,6 +229,18 @@ public function updateLayout(array $layout): DataResponse {
return new DataResponse(['layout' => $layout]);
}

/**
* Get the statuses
*
* @NoAdminRequired
* @return DataResponse<Http::STATUS_OK, array{statuses: list<string>}, array{}>
*
* 200: Statuses returned
*/
public function getStatuses(): DataResponse {
return new DataResponse(['statuses' => $this->service->getStatuses()]);
}

/**
* Update the statuses
*
Expand Down
20 changes: 5 additions & 15 deletions apps/dashboard/lib/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,7 +67,8 @@ public function __construct(
IManager $dashboardManager,
IConfig $config,
IL10N $l10n,
$userId
$userId,

Check notice

Code scanning / Psalm

MissingParamType Note

Parameter $userId has no provided type
private DashboardService $service,
) {
parent::__construct($appName, $request);

Expand All @@ -88,8 +89,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(),
Expand All @@ -98,19 +97,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('firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
$this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');

Expand Down
62 changes: 62 additions & 0 deletions apps/dashboard/lib/Service/DashboardService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024, Kate Döen <kate.doeen@nextcloud.com>
*
* @author Kate Döen <kate.doeen@nextcloud.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Dashboard\Service;

use JsonException;
use OCP\IConfig;

class DashboardService {
public function __construct(
private IConfig $config,
private String $userId,
) {

}

/**
* @return list<string>
*/
public function getLayout(): array {
$systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');

Check notice

Code scanning / Psalm

DeprecatedMethod Note

The method OCP\IConfig::getAppValue has been marked as deprecated
return array_values(array_filter(explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)), fn (string $value) => $value !== ''));
}

/**
* @return list<string>
*/
public function getStatuses() {
$configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '');
try {
// Parse the old format
/** @var array<string, bool> $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 !== ''));
}
}
}
140 changes: 140 additions & 0 deletions apps/dashboard/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,76 @@
}
},
"/ocs/v2.php/apps/dashboard/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",
Expand Down Expand Up @@ -516,6 +586,76 @@
}
},
"/ocs/v2.php/apps/dashboard/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",
Expand Down

0 comments on commit 571ce41

Please sign in to comment.