DashboardApiController.php 5.8 KB

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