-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(dashboard): implement widget item api v2 #39937
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| * | ||
| * @author Julien Veyssier <[email protected]> | ||
| * @author Julius Härtl <[email protected]> | ||
| * @author Richard Steinmetz <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
|
|
@@ -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'], | ||
| ] | ||
| ]; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| * | ||
| * @author Julien Veyssier <[email protected]> | ||
| * @author Kate Döen <[email protected]> | ||
| * @author Richard Steinmetz <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
|
|
@@ -35,18 +36,22 @@ | |
| 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; | ||
|
|
||
| /** | ||
| * @psalm-import-type DashboardWidget from ResponseDefinitions | ||
| * @psalm-import-type DashboardWidgetItem from ResponseDefinitions | ||
| * @psalm-import-type DashboardWidgetItems from ResponseDefinitions | ||
| */ | ||
| class DashboardApiController extends OCSController { | ||
|
|
||
|
|
@@ -71,6 +76,24 @@ public function __construct( | |
| $this->userId = $userId; | ||
| } | ||
|
|
||
| /** | ||
| * @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); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @NoAdminRequired | ||
| * @NoCSRFRequired | ||
|
|
@@ -83,18 +106,11 @@ public function __construct( | |
| * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItem[]>, array{}> | ||
| */ | ||
| 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)); | ||
| } | ||
|
|
@@ -103,6 +119,31 @@ public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widg | |
| return new DataResponse($items); | ||
| } | ||
|
|
||
| /** | ||
| * @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<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}> | ||
| */ | ||
| 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 | ||
| * | ||
|
|
@@ -124,6 +165,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 += [ | ||
|
|
@@ -136,6 +179,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); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| * @copyright Copyright (c) 2023 Kate Döen <[email protected]> | ||
| * | ||
| * @author Kate Döen <[email protected]> | ||
| * @author Richard Steinmetz <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
|
|
@@ -34,6 +35,8 @@ | |
| * icon_url: string, | ||
| * widget_url: ?string, | ||
| * item_icons_round: bool, | ||
| * item_api_versions: int[], | ||
| * reload_interval: int, | ||
| * buttons?: array{ | ||
| * type: string, | ||
| * text: string, | ||
|
|
@@ -46,8 +49,15 @@ | |
| * title: string, | ||
| * link: string, | ||
| * iconUrl: string, | ||
| * overlayIconUrl: string, | ||
| * sinceId: string, | ||
| * } | ||
| * | ||
| * @psalm-type DashboardWidgetItems = array{ | ||
| * items: DashboardWidgetItem[], | ||
| * emptyContentMessage: string, | ||
| * halfEmptyContentMessage: string, | ||
| * } | ||
| */ | ||
| class ResponseDefinitions { | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Psalm
PossiblyNullArgument