DashboardApiController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * @psalm-param int<1, 30> $limit
  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, not more than 30 are allowed
  118. * @psalm-param int<1, 30> $limit
  119. * @param string[] $widgets Limit results to specific widgets
  120. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}>
  121. *
  122. * 200: Widget items returned
  123. */
  124. public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  125. $items = [];
  126. $widgets = $this->getShownWidgets($widgets);
  127. foreach ($widgets as $widget) {
  128. if ($widget instanceof IAPIWidgetV2) {
  129. $items[$widget->getId()] = $widget
  130. ->getItemsV2($this->userId, $sinceIds[$widget->getId()] ?? null, $limit)
  131. ->jsonSerialize();
  132. }
  133. }
  134. return new DataResponse($items);
  135. }
  136. /**
  137. * Get the widgets
  138. *
  139. * @NoAdminRequired
  140. * @NoCSRFRequired
  141. *
  142. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidget>, array{}>
  143. *
  144. * 200: Widgets returned
  145. */
  146. public function getWidgets(): DataResponse {
  147. $widgets = $this->dashboardManager->getWidgets();
  148. $items = array_map(function (IWidget $widget) {
  149. $options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault();
  150. $data = [
  151. 'id' => $widget->getId(),
  152. 'title' => $widget->getTitle(),
  153. 'order' => $widget->getOrder(),
  154. 'icon_class' => $widget->getIconClass(),
  155. 'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
  156. 'widget_url' => $widget->getUrl(),
  157. 'item_icons_round' => $options->withRoundItemIcons(),
  158. 'item_api_versions' => [],
  159. 'reload_interval' => 0,
  160. ];
  161. if ($widget instanceof IButtonWidget) {
  162. $data += [
  163. 'buttons' => array_map(function (WidgetButton $button) {
  164. return [
  165. 'type' => $button->getType(),
  166. 'text' => $button->getText(),
  167. 'link' => $button->getLink(),
  168. ];
  169. }, $widget->getWidgetButtons($this->userId)),
  170. ];
  171. }
  172. if ($widget instanceof IReloadableWidget) {
  173. $data['reload_interval'] = $widget->getReloadInterval();
  174. }
  175. if ($widget instanceof IAPIWidget) {
  176. $data['item_api_versions'][] = 1;
  177. }
  178. if ($widget instanceof IAPIWidgetV2) {
  179. $data['item_api_versions'][] = 2;
  180. }
  181. return $data;
  182. }, $widgets);
  183. return new DataResponse($items);
  184. }
  185. }