1
0

DashboardApiController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  97. $items = [];
  98. $widgets = $this->getShownWidgets($widgets);
  99. foreach ($widgets as $widget) {
  100. if ($widget instanceof IAPIWidget) {
  101. $items[$widget->getId()] = array_map(static function (WidgetItem $item) {
  102. return $item->jsonSerialize();
  103. }, $widget->getItems($this->userId, $sinceIds[$widget->getId()] ?? null, $limit));
  104. }
  105. }
  106. return new DataResponse($items);
  107. }
  108. /**
  109. * @NoAdminRequired
  110. * @NoCSRFRequired
  111. *
  112. * Get the items for the widgets
  113. *
  114. * @param array<string, string> $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items
  115. * @param int $limit Limit number of result items per widget
  116. * @param string[] $widgets Limit results to specific widgets
  117. * @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}>
  118. */
  119. public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  120. $items = [];
  121. $widgets = $this->getShownWidgets($widgets);
  122. foreach ($widgets as $widget) {
  123. if ($widget instanceof IAPIWidgetV2) {
  124. $items[$widget->getId()] = $widget
  125. ->getItemsV2($this->userId, $sinceIds[$widget->getId()] ?? null, $limit)
  126. ->jsonSerialize();
  127. }
  128. }
  129. return new DataResponse($items);
  130. }
  131. /**
  132. * Get the widgets
  133. *
  134. * @NoAdminRequired
  135. * @NoCSRFRequired
  136. *
  137. * @return DataResponse<Http::STATUS_OK, DashboardWidget[], array{}>
  138. */
  139. public function getWidgets(): DataResponse {
  140. $widgets = $this->dashboardManager->getWidgets();
  141. $items = array_map(function (IWidget $widget) {
  142. $options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault();
  143. $data = [
  144. 'id' => $widget->getId(),
  145. 'title' => $widget->getTitle(),
  146. 'order' => $widget->getOrder(),
  147. 'icon_class' => $widget->getIconClass(),
  148. 'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
  149. 'widget_url' => $widget->getUrl(),
  150. 'item_icons_round' => $options->withRoundItemIcons(),
  151. 'item_api_versions' => [],
  152. 'reload_interval' => 0,
  153. ];
  154. if ($widget instanceof IButtonWidget) {
  155. $data += [
  156. 'buttons' => array_map(function (WidgetButton $button) {
  157. return [
  158. 'type' => $button->getType(),
  159. 'text' => $button->getText(),
  160. 'link' => $button->getLink(),
  161. ];
  162. }, $widget->getWidgetButtons($this->userId)),
  163. ];
  164. }
  165. if ($widget instanceof IReloadableWidget) {
  166. $data['reload_interval'] = $widget->getReloadInterval();
  167. }
  168. if ($widget instanceof IAPIWidget) {
  169. $data['item_api_versions'][] = 1;
  170. }
  171. if ($widget instanceof IAPIWidgetV2) {
  172. $data['item_api_versions'][] = 2;
  173. }
  174. return $data;
  175. }, $widgets);
  176. return new DataResponse($items);
  177. }
  178. }