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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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\OCSController;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\Dashboard\IButtonWidget;
  32. use OCP\Dashboard\IIconWidget;
  33. use OCP\Dashboard\IOptionWidget;
  34. use OCP\Dashboard\IManager;
  35. use OCP\Dashboard\IReloadableWidget;
  36. use OCP\Dashboard\IWidget;
  37. use OCP\Dashboard\Model\WidgetButton;
  38. use OCP\Dashboard\Model\WidgetOptions;
  39. use OCP\IConfig;
  40. use OCP\IRequest;
  41. use OCP\Dashboard\IAPIWidget;
  42. use OCP\Dashboard\IAPIWidgetV2;
  43. use OCP\Dashboard\Model\WidgetItem;
  44. use OCP\Dashboard\Model\WidgetItems;
  45. /**
  46. * @psalm-import-type DashboardWidget from ResponseDefinitions
  47. * @psalm-import-type DashboardWidgetItem from ResponseDefinitions
  48. * @psalm-import-type DashboardWidgetItems from ResponseDefinitions
  49. */
  50. class DashboardApiController extends OCSController {
  51. /** @var IManager */
  52. private $dashboardManager;
  53. /** @var IConfig */
  54. private $config;
  55. /** @var string|null */
  56. private $userId;
  57. public function __construct(
  58. string $appName,
  59. IRequest $request,
  60. IManager $dashboardManager,
  61. IConfig $config,
  62. ?string $userId
  63. ) {
  64. parent::__construct($appName, $request);
  65. $this->dashboardManager = $dashboardManager;
  66. $this->config = $config;
  67. $this->userId = $userId;
  68. }
  69. /**
  70. * @param string[] $widgetIds Limit widgets to given ids
  71. * @return IWidget[]
  72. */
  73. private function getShownWidgets(array $widgetIds): array {
  74. if (empty($widgetIds)) {
  75. $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
  76. $widgetIds = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault));
  77. }
  78. return array_filter(
  79. $this->dashboardManager->getWidgets(),
  80. static function (IWidget $widget) use ($widgetIds) {
  81. return in_array($widget->getId(), $widgetIds);
  82. },
  83. );
  84. }
  85. /**
  86. * @NoAdminRequired
  87. * @NoCSRFRequired
  88. *
  89. * Get the items for the widgets
  90. *
  91. * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items
  92. * @param int $limit Limit number of result items per widget
  93. * @param string[] $widgets Limit results to specific widgets
  94. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItem[]>, array{}>
  95. *
  96. * 200: Widget items returned
  97. */
  98. public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  99. $items = [];
  100. $widgets = $this->getShownWidgets($widgets);
  101. foreach ($widgets as $widget) {
  102. if ($widget instanceof IAPIWidget) {
  103. $items[$widget->getId()] = array_map(static function (WidgetItem $item) {
  104. return $item->jsonSerialize();
  105. }, $widget->getItems($this->userId, $sinceIds[$widget->getId()] ?? null, $limit));
  106. }
  107. }
  108. return new DataResponse($items);
  109. }
  110. /**
  111. * @NoAdminRequired
  112. * @NoCSRFRequired
  113. *
  114. * Get the items for the widgets
  115. *
  116. * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items
  117. * @param int $limit Limit number of result items per widget
  118. * @param string[] $widgets Limit results to specific widgets
  119. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}>
  120. *
  121. * 200: Widget items returned
  122. */
  123. public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  124. $items = [];
  125. $widgets = $this->getShownWidgets($widgets);
  126. foreach ($widgets as $widget) {
  127. if ($widget instanceof IAPIWidgetV2) {
  128. $items[$widget->getId()] = $widget
  129. ->getItemsV2($this->userId, $sinceIds[$widget->getId()] ?? null, $limit)
  130. ->jsonSerialize();
  131. }
  132. }
  133. return new DataResponse($items);
  134. }
  135. /**
  136. * Get the widgets
  137. *
  138. * @NoAdminRequired
  139. * @NoCSRFRequired
  140. *
  141. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidget>, array{}>
  142. *
  143. * 200: Widgets returned
  144. */
  145. public function getWidgets(): DataResponse {
  146. $widgets = $this->dashboardManager->getWidgets();
  147. $items = array_map(function (IWidget $widget) {
  148. $options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault();
  149. $data = [
  150. 'id' => $widget->getId(),
  151. 'title' => $widget->getTitle(),
  152. 'order' => $widget->getOrder(),
  153. 'icon_class' => $widget->getIconClass(),
  154. 'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
  155. 'widget_url' => $widget->getUrl(),
  156. 'item_icons_round' => $options->withRoundItemIcons(),
  157. 'item_api_versions' => [],
  158. 'reload_interval' => 0,
  159. ];
  160. if ($widget instanceof IButtonWidget) {
  161. $data += [
  162. 'buttons' => array_map(function (WidgetButton $button) {
  163. return [
  164. 'type' => $button->getType(),
  165. 'text' => $button->getText(),
  166. 'link' => $button->getLink(),
  167. ];
  168. }, $widget->getWidgetButtons($this->userId)),
  169. ];
  170. }
  171. if ($widget instanceof IReloadableWidget) {
  172. $data['reload_interval'] = $widget->getReloadInterval();
  173. }
  174. if ($widget instanceof IAPIWidget) {
  175. $data['item_api_versions'][] = 1;
  176. }
  177. if ($widget instanceof IAPIWidgetV2) {
  178. $data['item_api_versions'][] = 2;
  179. }
  180. return $data;
  181. }, $widgets);
  182. return new DataResponse($items);
  183. }
  184. }