diff options
author | Vincent Petry <vincent@nextcloud.com> | 2022-09-16 12:18:24 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-16 12:18:24 +0200 |
commit | 703dd64e7e0cf0e675ef40f4fec7375fc082b7d3 (patch) | |
tree | a18f22b1d4d73bcb6778e3310b64c328f5b7cda2 /apps/dashboard | |
parent | a716308e7b40e986d6ed23c317286913a0caa304 (diff) | |
parent | 3009e023249fc1ec68dae101c1abd4ca52c03125 (diff) | |
download | nextcloud-server-703dd64e7e0cf0e675ef40f4fec7375fc082b7d3.tar.gz nextcloud-server-703dd64e7e0cf0e675ef40f4fec7375fc082b7d3.zip |
Merge pull request #33658 from nextcloud/dashboard-api-widgets
Extend dashboard api to allow listing of widgets
Diffstat (limited to 'apps/dashboard')
-rw-r--r-- | apps/dashboard/appinfo/routes.php | 1 | ||||
-rw-r--r-- | apps/dashboard/lib/Controller/DashboardApiController.php | 68 |
2 files changed, 60 insertions, 9 deletions
diff --git a/apps/dashboard/appinfo/routes.php b/apps/dashboard/appinfo/routes.php index 2d930b2240d..c6891837384 100644 --- a/apps/dashboard/appinfo/routes.php +++ b/apps/dashboard/appinfo/routes.php @@ -31,6 +31,7 @@ return [ ['name' => 'dashboard#updateStatuses', 'url' => '/statuses', 'verb' => 'POST'], ], 'ocs' => [ + ['name' => 'dashboardApi#getWidgets', 'url' => '/api/v1/widgets', 'verb' => 'GET'], ['name' => 'dashboardApi#getWidgetItems', 'url' => '/api/v1/widget-items', 'verb' => 'GET'], ] ]; diff --git a/apps/dashboard/lib/Controller/DashboardApiController.php b/apps/dashboard/lib/Controller/DashboardApiController.php index bb329888b09..1062cf1bdba 100644 --- a/apps/dashboard/lib/Controller/DashboardApiController.php +++ b/apps/dashboard/lib/Controller/DashboardApiController.php @@ -28,7 +28,13 @@ namespace OCA\Dashboard\Controller; use OCP\AppFramework\OCSController; use OCP\AppFramework\Http\DataResponse; +use OCP\Dashboard\IButtonWidget; +use OCP\Dashboard\IIconWidget; +use OCP\Dashboard\IOptionWidget; use OCP\Dashboard\IManager; +use OCP\Dashboard\IWidget; +use OCP\Dashboard\Model\WidgetButton; +use OCP\Dashboard\Model\WidgetOptions; use OCP\IConfig; use OCP\IRequest; @@ -44,11 +50,13 @@ class DashboardApiController extends OCSController { /** @var string|null */ private $userId; - public function __construct(string $appName, - IRequest $request, - IManager $dashboardManager, - IConfig $config, - ?string $userId) { + public function __construct( + string $appName, + IRequest $request, + IManager $dashboardManager, + IConfig $config, + ?string $userId + ) { parent::__construct($appName, $request); $this->dashboardManager = $dashboardManager; @@ -62,19 +70,23 @@ class DashboardApiController extends OCSController { * * @param array $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items * @param int $limit Limit number of result items per widget + * @param string[] $widgets Limit results to specific widgets * * @NoAdminRequired * @NoCSRFRequired */ - public function getWidgetItems(array $sinceIds = [], int $limit = 7): DataResponse { + public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse { + $showWidgets = $widgets; $items = []; - $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar'); - $userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)); + if (empty($showWidgets)) { + $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar'); + $showWidgets = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)); + } $widgets = $this->dashboardManager->getWidgets(); foreach ($widgets as $widget) { - if ($widget instanceof IAPIWidget && in_array($widget->getId(), $userLayout)) { + if ($widget instanceof IAPIWidget && in_array($widget->getId(), $showWidgets)) { $items[$widget->getId()] = array_map(function (WidgetItem $item) { return $item->jsonSerialize(); }, $widget->getItems($this->userId, $sinceIds[$widget->getId()] ?? null, $limit)); @@ -83,4 +95,42 @@ class DashboardApiController extends OCSController { return new DataResponse($items); } + + /** + * Example request with Curl: + * curl -u user:passwd http://my.nc/ocs/v2.php/apps/dashboard/api/v1/widgets + * + * @NoAdminRequired + * @NoCSRFRequired + */ + public function getWidgets(): DataResponse { + $widgets = $this->dashboardManager->getWidgets(); + + $items = array_map(function (IWidget $widget) { + $options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault(); + $data = [ + 'id' => $widget->getId(), + 'title' => $widget->getTitle(), + 'order' => $widget->getOrder(), + 'icon_class' => $widget->getIconClass(), + 'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '', + 'widget_url' => $widget->getUrl(), + 'item_icons_round' => $options->withRoundItemIcons(), + ]; + if ($widget instanceof IButtonWidget) { + $data += [ + 'buttons' => array_map(function (WidgetButton $button) { + return [ + 'type' => $button->getType(), + 'text' => $button->getText(), + 'link' => $button->getLink(), + ]; + }, $widget->getWidgetButtons($this->userId)), + ]; + } + return $data; + }, $widgets); + + return new DataResponse($items); + } } |