You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DashboardApiController.php 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Julien Veyssier <eneiluj@posteo.net>
  5. *
  6. * @author Julien Veyssier <eneiluj@posteo.net>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  8. * @author Richard Steinmetz <richard@steinmetz.cloud>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Dashboard\Controller;
  27. use OCA\Dashboard\ResponseDefinitions;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\OCSController;
  31. use OCP\Dashboard\IAPIWidget;
  32. use OCP\Dashboard\IAPIWidgetV2;
  33. use OCP\Dashboard\IButtonWidget;
  34. use OCP\Dashboard\IIconWidget;
  35. use OCP\Dashboard\IManager;
  36. use OCP\Dashboard\IOptionWidget;
  37. use OCP\Dashboard\IReloadableWidget;
  38. use OCP\Dashboard\IWidget;
  39. use OCP\Dashboard\Model\WidgetButton;
  40. use OCP\Dashboard\Model\WidgetItem;
  41. use OCP\Dashboard\Model\WidgetOptions;
  42. use OCP\IConfig;
  43. use OCP\IRequest;
  44. /**
  45. * @psalm-import-type DashboardWidget from ResponseDefinitions
  46. * @psalm-import-type DashboardWidgetItem from ResponseDefinitions
  47. * @psalm-import-type DashboardWidgetItems from ResponseDefinitions
  48. */
  49. class DashboardApiController extends OCSController {
  50. /** @var IManager */
  51. private $dashboardManager;
  52. /** @var IConfig */
  53. private $config;
  54. /** @var string|null */
  55. private $userId;
  56. public function __construct(
  57. string $appName,
  58. IRequest $request,
  59. IManager $dashboardManager,
  60. IConfig $config,
  61. ?string $userId
  62. ) {
  63. parent::__construct($appName, $request);
  64. $this->dashboardManager = $dashboardManager;
  65. $this->config = $config;
  66. $this->userId = $userId;
  67. }
  68. /**
  69. * @param string[] $widgetIds Limit widgets to given ids
  70. * @return IWidget[]
  71. */
  72. private function getShownWidgets(array $widgetIds): array {
  73. if (empty($widgetIds)) {
  74. $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
  75. $widgetIds = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault));
  76. }
  77. return array_filter(
  78. $this->dashboardManager->getWidgets(),
  79. static function (IWidget $widget) use ($widgetIds) {
  80. return in_array($widget->getId(), $widgetIds);
  81. },
  82. );
  83. }
  84. /**
  85. * @NoAdminRequired
  86. * @NoCSRFRequired
  87. *
  88. * Get the items for the widgets
  89. *
  90. * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items
  91. * @param int $limit Limit number of result items per widget
  92. * @param string[] $widgets Limit results to specific widgets
  93. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItem[]>, array{}>
  94. *
  95. * 200: Widget items returned
  96. */
  97. public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  98. $items = [];
  99. $widgets = $this->getShownWidgets($widgets);
  100. foreach ($widgets as $widget) {
  101. if ($widget instanceof IAPIWidget) {
  102. $items[$widget->getId()] = array_map(static function (WidgetItem $item) {
  103. return $item->jsonSerialize();
  104. }, $widget->getItems($this->userId, $sinceIds[$widget->getId()] ?? null, $limit));
  105. }
  106. }
  107. return new DataResponse($items);
  108. }
  109. /**
  110. * @NoAdminRequired
  111. * @NoCSRFRequired
  112. *
  113. * Get the items for the widgets
  114. *
  115. * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items
  116. * @param int $limit Limit number of result items per widget
  117. * @param string[] $widgets Limit results to specific widgets
  118. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}>
  119. *
  120. * 200: Widget items returned
  121. */
  122. public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  123. $items = [];
  124. $widgets = $this->getShownWidgets($widgets);
  125. foreach ($widgets as $widget) {
  126. if ($widget instanceof IAPIWidgetV2) {
  127. $items[$widget->getId()] = $widget
  128. ->getItemsV2($this->userId, $sinceIds[$widget->getId()] ?? null, $limit)
  129. ->jsonSerialize();
  130. }
  131. }
  132. return new DataResponse($items);
  133. }
  134. /**
  135. * Get the widgets
  136. *
  137. * @NoAdminRequired
  138. * @NoCSRFRequired
  139. *
  140. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidget>, array{}>
  141. *
  142. * 200: Widgets returned
  143. */
  144. public function getWidgets(): DataResponse {
  145. $widgets = $this->dashboardManager->getWidgets();
  146. $items = array_map(function (IWidget $widget) {
  147. $options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault();
  148. $data = [
  149. 'id' => $widget->getId(),
  150. 'title' => $widget->getTitle(),
  151. 'order' => $widget->getOrder(),
  152. 'icon_class' => $widget->getIconClass(),
  153. 'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
  154. 'widget_url' => $widget->getUrl(),
  155. 'item_icons_round' => $options->withRoundItemIcons(),
  156. 'item_api_versions' => [],
  157. 'reload_interval' => 0,
  158. ];
  159. if ($widget instanceof IButtonWidget) {
  160. $data += [
  161. 'buttons' => array_map(function (WidgetButton $button) {
  162. return [
  163. 'type' => $button->getType(),
  164. 'text' => $button->getText(),
  165. 'link' => $button->getLink(),
  166. ];
  167. }, $widget->getWidgetButtons($this->userId)),
  168. ];
  169. }
  170. if ($widget instanceof IReloadableWidget) {
  171. $data['reload_interval'] = $widget->getReloadInterval();
  172. }
  173. if ($widget instanceof IAPIWidget) {
  174. $data['item_api_versions'][] = 1;
  175. }
  176. if ($widget instanceof IAPIWidgetV2) {
  177. $data['item_api_versions'][] = 2;
  178. }
  179. return $data;
  180. }, $widgets);
  181. return new DataResponse($items);
  182. }
  183. }