DashboardApiController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Dashboard\Controller;
  8. use OCA\Dashboard\ResponseDefinitions;
  9. use OCA\Dashboard\Service\DashboardService;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\ApiRoute;
  12. use OCP\AppFramework\Http\DataResponse;
  13. use OCP\AppFramework\OCSController;
  14. use OCP\Dashboard\IAPIWidget;
  15. use OCP\Dashboard\IAPIWidgetV2;
  16. use OCP\Dashboard\IButtonWidget;
  17. use OCP\Dashboard\IIconWidget;
  18. use OCP\Dashboard\IManager;
  19. use OCP\Dashboard\IOptionWidget;
  20. use OCP\Dashboard\IReloadableWidget;
  21. use OCP\Dashboard\IWidget;
  22. use OCP\Dashboard\Model\WidgetButton;
  23. use OCP\Dashboard\Model\WidgetItem;
  24. use OCP\Dashboard\Model\WidgetOptions;
  25. use OCP\IConfig;
  26. use OCP\IRequest;
  27. /**
  28. * @psalm-import-type DashboardWidget from ResponseDefinitions
  29. * @psalm-import-type DashboardWidgetItem from ResponseDefinitions
  30. * @psalm-import-type DashboardWidgetItems from ResponseDefinitions
  31. */
  32. class DashboardApiController extends OCSController {
  33. public function __construct(
  34. string $appName,
  35. IRequest $request,
  36. private IManager $dashboardManager,
  37. private IConfig $config,
  38. private ?string $userId,
  39. private DashboardService $service,
  40. ) {
  41. parent::__construct($appName, $request);
  42. }
  43. /**
  44. * @param string[] $widgetIds Limit widgets to given ids
  45. * @return IWidget[]
  46. */
  47. private function getShownWidgets(array $widgetIds): array {
  48. if (empty($widgetIds)) {
  49. $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
  50. $widgetIds = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault));
  51. }
  52. return array_filter(
  53. $this->dashboardManager->getWidgets(),
  54. static function (IWidget $widget) use ($widgetIds) {
  55. return in_array($widget->getId(), $widgetIds);
  56. },
  57. );
  58. }
  59. /**
  60. * @NoAdminRequired
  61. * @NoCSRFRequired
  62. *
  63. * Get the items for the widgets
  64. *
  65. * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items
  66. * @param int $limit Limit number of result items per widget
  67. * @psalm-param int<1, 30> $limit
  68. * @param string[] $widgets Limit results to specific widgets
  69. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItem[]>, array{}>
  70. *
  71. * 200: Widget items returned
  72. */
  73. #[ApiRoute(verb: 'GET', url: '/api/v1/widget-items')]
  74. public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  75. $items = [];
  76. $widgets = $this->getShownWidgets($widgets);
  77. foreach ($widgets as $widget) {
  78. if ($widget instanceof IAPIWidget) {
  79. $items[$widget->getId()] = array_map(static function (WidgetItem $item) {
  80. return $item->jsonSerialize();
  81. }, $widget->getItems($this->userId, $sinceIds[$widget->getId()] ?? null, $limit));
  82. }
  83. }
  84. return new DataResponse($items);
  85. }
  86. /**
  87. * @NoAdminRequired
  88. * @NoCSRFRequired
  89. *
  90. * Get the items for the widgets
  91. *
  92. * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items
  93. * @param int $limit Limit number of result items per widget, not more than 30 are allowed
  94. * @psalm-param int<1, 30> $limit
  95. * @param string[] $widgets Limit results to specific widgets
  96. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}>
  97. *
  98. * 200: Widget items returned
  99. */
  100. #[ApiRoute(verb: 'GET', url: '/api/v2/widget-items')]
  101. public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  102. $items = [];
  103. $widgets = $this->getShownWidgets($widgets);
  104. foreach ($widgets as $widget) {
  105. if ($widget instanceof IAPIWidgetV2) {
  106. $items[$widget->getId()] = $widget
  107. ->getItemsV2($this->userId, $sinceIds[$widget->getId()] ?? null, $limit)
  108. ->jsonSerialize();
  109. }
  110. }
  111. return new DataResponse($items);
  112. }
  113. /**
  114. * Get the widgets
  115. *
  116. * @NoAdminRequired
  117. * @NoCSRFRequired
  118. *
  119. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidget>, array{}>
  120. *
  121. * 200: Widgets returned
  122. */
  123. #[ApiRoute(verb: 'GET', url: '/api/v1/widgets')]
  124. public function getWidgets(): DataResponse {
  125. $widgets = $this->dashboardManager->getWidgets();
  126. $items = array_map(function (IWidget $widget) {
  127. $options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault();
  128. $data = [
  129. 'id' => $widget->getId(),
  130. 'title' => $widget->getTitle(),
  131. 'order' => $widget->getOrder(),
  132. 'icon_class' => $widget->getIconClass(),
  133. 'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
  134. 'widget_url' => $widget->getUrl(),
  135. 'item_icons_round' => $options->withRoundItemIcons(),
  136. 'item_api_versions' => [],
  137. 'reload_interval' => 0,
  138. ];
  139. if ($widget instanceof IButtonWidget) {
  140. $data += [
  141. 'buttons' => array_map(function (WidgetButton $button) {
  142. return [
  143. 'type' => $button->getType(),
  144. 'text' => $button->getText(),
  145. 'link' => $button->getLink(),
  146. ];
  147. }, $widget->getWidgetButtons($this->userId)),
  148. ];
  149. }
  150. if ($widget instanceof IReloadableWidget) {
  151. $data['reload_interval'] = $widget->getReloadInterval();
  152. }
  153. if ($widget instanceof IAPIWidget) {
  154. $data['item_api_versions'][] = 1;
  155. }
  156. if ($widget instanceof IAPIWidgetV2) {
  157. $data['item_api_versions'][] = 2;
  158. }
  159. return $data;
  160. }, $widgets);
  161. return new DataResponse($items);
  162. }
  163. /**
  164. * Get the layout
  165. *
  166. * @NoAdminRequired
  167. * @return DataResponse<Http::STATUS_OK, array{layout: list<string>}, array{}>
  168. *
  169. * 200: Layout returned
  170. */
  171. #[ApiRoute(verb: 'GET', url: '/api/v3/layout')]
  172. public function getLayout(): DataResponse {
  173. return new DataResponse(['layout' => $this->service->getLayout()]);
  174. }
  175. /**
  176. * Update the layout
  177. *
  178. * @NoAdminRequired
  179. * @param list<string> $layout The new layout
  180. * @return DataResponse<Http::STATUS_OK, array{layout: list<string>}, array{}>
  181. *
  182. * 200: Statuses updated successfully
  183. */
  184. #[ApiRoute(verb: 'POST', url: '/api/v3/layout')]
  185. public function updateLayout(array $layout): DataResponse {
  186. $this->config->setUserValue($this->userId, 'dashboard', 'layout', implode(',', $layout));
  187. return new DataResponse(['layout' => $layout]);
  188. }
  189. /**
  190. * Get the statuses
  191. *
  192. * @NoAdminRequired
  193. * @return DataResponse<Http::STATUS_OK, array{statuses: list<string>}, array{}>
  194. *
  195. * 200: Statuses returned
  196. */
  197. #[ApiRoute(verb: 'GET', url: '/api/v3/statuses')]
  198. public function getStatuses(): DataResponse {
  199. return new DataResponse(['statuses' => $this->service->getStatuses()]);
  200. }
  201. /**
  202. * Update the statuses
  203. *
  204. * @NoAdminRequired
  205. * @param list<string> $statuses The new statuses
  206. * @return DataResponse<Http::STATUS_OK, array{statuses: list<string>}, array{}>
  207. *
  208. * 200: Statuses updated successfully
  209. */
  210. #[ApiRoute(verb: 'POST', url: '/api/v3/statuses')]
  211. public function updateStatuses(array $statuses): DataResponse {
  212. $this->config->setUserValue($this->userId, 'dashboard', 'statuses', implode(',', $statuses));
  213. return new DataResponse(['statuses' => $statuses]);
  214. }
  215. }