Skip to content
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

Expose dashboard layout and statuses API #42973

Merged
merged 5 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 0 additions & 39 deletions apps/dashboard/appinfo/routes.php

This file was deleted.

2 changes: 1 addition & 1 deletion apps/dashboard/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php',
'OCA\\Dashboard\\Controller\\DashboardApiController' => $baseDir . '/../lib/Controller/DashboardApiController.php',
'OCA\\Dashboard\\Controller\\DashboardController' => $baseDir . '/../lib/Controller/DashboardController.php',
'OCA\\Dashboard\\Controller\\LayoutApiController' => $baseDir . '/../lib/Controller/LayoutApiController.php',
'OCA\\Dashboard\\ResponseDefinitions' => $baseDir . '/../lib/ResponseDefinitions.php',
'OCA\\Dashboard\\Service\\DashboardService' => $baseDir . '/../lib/Service/DashboardService.php',
);
2 changes: 1 addition & 1 deletion apps/dashboard/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class ComposerStaticInitDashboard
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
'OCA\\Dashboard\\Controller\\DashboardApiController' => __DIR__ . '/..' . '/../lib/Controller/DashboardApiController.php',
'OCA\\Dashboard\\Controller\\DashboardController' => __DIR__ . '/..' . '/../lib/Controller/DashboardController.php',
'OCA\\Dashboard\\Controller\\LayoutApiController' => __DIR__ . '/..' . '/../lib/Controller/LayoutApiController.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
62 changes: 62 additions & 0 deletions apps/dashboard/lib/Controller/DashboardApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
namespace OCA\Dashboard\Controller;

use OCA\Dashboard\ResponseDefinitions;
use OCA\Dashboard\Service\DashboardService;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\ApiRoute;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\Dashboard\IAPIWidget;
Expand Down Expand Up @@ -60,6 +62,7 @@
private IManager $dashboardManager,
private IConfig $config,
private ?string $userId,
private DashboardService $service,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -96,6 +99,7 @@
*
* 200: Widget items returned
*/
#[ApiRoute(verb: 'GET', url: '/api/v1/widget-items')]
public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
$items = [];
$widgets = $this->getShownWidgets($widgets);
Expand Down Expand Up @@ -124,6 +128,7 @@
*
* 200: Widget items returned
*/
#[ApiRoute(verb: 'GET', url: '/api/v2/widget-items')]
public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
$items = [];
$widgets = $this->getShownWidgets($widgets);
Expand All @@ -148,6 +153,7 @@
*
* 200: Widgets returned
*/
#[ApiRoute(verb: 'GET', url: '/api/v1/widgets')]
public function getWidgets(): DataResponse {
$widgets = $this->dashboardManager->getWidgets();

Expand Down Expand Up @@ -189,4 +195,60 @@

return new DataResponse($items);
}

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

/**
* Update the layout
*
* @NoAdminRequired
* @param list<string> $layout The new layout
* @return DataResponse<Http::STATUS_OK, array{layout: list<string>}, array{}>
*
* 200: Statuses updated successfully
*/
#[ApiRoute(verb: 'POST', url: '/api/v3/layout')]
public function updateLayout(array $layout): DataResponse {
$this->config->setUserValue($this->userId, 'dashboard', 'layout', implode(',', $layout));

Check notice

Code scanning / Psalm

PossiblyNullArgument Note

Argument 1 of OCP\IConfig::setUserValue cannot be null, possibly null value provided
return new DataResponse(['layout' => $layout]);
}

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

/**
* Update the statuses
*
* @NoAdminRequired
* @param list<string> $statuses The new statuses
* @return DataResponse<Http::STATUS_OK, array{statuses: list<string>}, array{}>
*
* 200: Statuses updated successfully
*/
#[ApiRoute(verb: 'POST', url: '/api/v3/statuses')]
public function updateStatuses(array $statuses): DataResponse {
$this->config->setUserValue($this->userId, 'dashboard', 'statuses', implode(',', $statuses));

Check notice

Code scanning / Psalm

PossiblyNullArgument Note

Argument 1 of OCP\IConfig::setUserValue cannot be null, possibly null value provided
return new DataResponse(['statuses' => $statuses]);
}
}
38 changes: 7 additions & 31 deletions apps/dashboard/lib/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
*/
namespace OCA\Dashboard\Controller;

use OCA\Dashboard\Service\DashboardService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
use OCP\AppFramework\Http\Attribute\OpenAPI;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Services\IInitialState;
use OCP\Dashboard\IManager;
Expand All @@ -54,7 +55,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);
}
Expand All @@ -64,12 +66,11 @@ public function __construct(
* @NoAdminRequired
* @return TemplateResponse
*/
#[FrontpageRoute(verb: 'GET', url: '/')]
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 = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault));
$widgets = array_map(function (IWidget $widget) {
return [
'id' => $widget->getId(),
Expand All @@ -78,15 +79,10 @@ public function index(): TemplateResponse {
'url' => $widget->getUrl()
];
}, $this->dashboardManager->getWidgets());
$configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '');
$statuses = json_decode($configStatuses, true);
// We avoid getting an empty array as it will not produce an object in UI's JS
// It does not matter if some statuses are missing from the array, missing ones are considered enabled
$statuses = ($statuses && count($statuses) > 0) ? $statuses : ['weather' => true];

$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');
Expand All @@ -104,24 +100,4 @@ public function index(): TemplateResponse {

return $response;
}

/**
* @NoAdminRequired
* @param string $layout
* @return JSONResponse
*/
public function updateLayout(string $layout): JSONResponse {
$this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout);
return new JSONResponse(['layout' => $layout]);
}

/**
* @NoAdminRequired
* @param string $statuses
* @return JSONResponse
*/
public function updateStatuses(string $statuses): JSONResponse {
$this->config->setUserValue($this->userId, 'dashboard', 'statuses', $statuses);
return new JSONResponse(['statuses' => $statuses]);
}
}
56 changes: 0 additions & 56 deletions apps/dashboard/lib/Controller/LayoutApiController.php

This file was deleted.

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 <[email protected]>
*
* @author Kate Döen <[email protected]>
*
* @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 !== ''));
}
}
}
Loading
Loading