Skip to content

Commit

Permalink
Merge pull request #39993 from nextcloud/backport/39937/stable27
Browse files Browse the repository at this point in the history
[stable27] feat(dashboard): implement widget item api v2
  • Loading branch information
nickvergessen authored Aug 22, 2023
2 parents 2a435e8 + 900439e commit 7d795d0
Show file tree
Hide file tree
Showing 24 changed files with 531 additions and 703 deletions.
2 changes: 2 additions & 0 deletions apps/dashboard/appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
* @author Julien Veyssier <eneiluj@posteo.net>
* @author Julius Härtl <jus@bitgrid.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -33,5 +34,6 @@
'ocs' => [
['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'],
]
];
73 changes: 63 additions & 10 deletions apps/dashboard/lib/Controller/DashboardApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @copyright Copyright (c) 2021 Julien Veyssier <eneiluj@posteo.net>
*
* @author Julien Veyssier <eneiluj@posteo.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -32,14 +33,17 @@
use OCP\Dashboard\IIconWidget;
use OCP\Dashboard\IOptionWidget;
use OCP\Dashboard\IManager;
use OCP\Dashboard\IReloadableWidget;
use OCP\Dashboard\IWidget;
use OCP\Dashboard\Model\WidgetButton;
use OCP\Dashboard\Model\WidgetOptions;
use OCP\IConfig;
use OCP\IRequest;

use OCP\Dashboard\IAPIWidget;
use OCP\Dashboard\IAPIWidgetV2;
use OCP\Dashboard\Model\WidgetItem;
use OCP\Dashboard\Model\WidgetItems;

class DashboardApiController extends OCSController {

Expand Down Expand Up @@ -68,6 +72,24 @@ public function __construct(
* Example request with Curl:
* curl -u user:passwd http://my.nc/ocs/v2.php/apps/dashboard/api/v1/widget-items -H Content-Type:application/json -X GET -d '{"sinceIds":{"github_notifications":"2021-03-22T15:01:10Z"}}'
*
* @param string[] $widgetIds Limit widgets to given ids
* @return IWidget[]
*/
private function getShownWidgets(array $widgetIds): array {
if (empty($widgetIds)) {
$systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
$widgetIds = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault));
}

return array_filter(
$this->dashboardManager->getWidgets(),
static function (IWidget $widget) use ($widgetIds) {
return in_array($widget->getId(), $widgetIds);
},
);
}

/**
* @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
Expand All @@ -76,18 +98,11 @@ public function __construct(
* @NoCSRFRequired
*/
public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
$showWidgets = $widgets;
$items = [];

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();
$widgets = $this->getShownWidgets($widgets);
foreach ($widgets as $widget) {
if ($widget instanceof IAPIWidget && in_array($widget->getId(), $showWidgets)) {
$items[$widget->getId()] = array_map(function (WidgetItem $item) {
if ($widget instanceof IAPIWidget) {
$items[$widget->getId()] = array_map(static function (WidgetItem $item) {
return $item->jsonSerialize();
}, $widget->getItems($this->userId, $sinceIds[$widget->getId()] ?? null, $limit));
}
Expand All @@ -102,6 +117,33 @@ public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widg
*
* @NoAdminRequired
* @NoCSRFRequired
*
* Get the items for the widgets
*
* @param array<string, string> $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
* @return DataResponse
*/
public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
$items = [];
$widgets = $this->getShownWidgets($widgets);
foreach ($widgets as $widget) {
if ($widget instanceof IAPIWidgetV2) {
$items[$widget->getId()] = $widget
->getItemsV2($this->userId, $sinceIds[$widget->getId()] ?? null, $limit)
->jsonSerialize();
}
}

return new DataResponse($items);
}

/**
* Get the widgets
*
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getWidgets(): DataResponse {
$widgets = $this->dashboardManager->getWidgets();
Expand All @@ -116,6 +158,8 @@ public function getWidgets(): DataResponse {
'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
'widget_url' => $widget->getUrl(),
'item_icons_round' => $options->withRoundItemIcons(),
'item_api_versions' => [],
'reload_interval' => 0,
];
if ($widget instanceof IButtonWidget) {
$data += [
Expand All @@ -128,6 +172,15 @@ public function getWidgets(): DataResponse {
}, $widget->getWidgetButtons($this->userId)),
];
}
if ($widget instanceof IReloadableWidget) {
$data['reload_interval'] = $widget->getReloadInterval();
}
if ($widget instanceof IAPIWidget) {
$data['item_api_versions'][] = 1;
}
if ($widget instanceof IAPIWidgetV2) {
$data['item_api_versions'][] = 2;
}
return $data;
}, $widgets);

Expand Down
Loading

0 comments on commit 7d795d0

Please sign in to comment.