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 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Dashboard\Controller;
  25. use OCP\AppFramework\OCSController;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\Dashboard\IButtonWidget;
  28. use OCP\Dashboard\IIconWidget;
  29. use OCP\Dashboard\IOptionWidget;
  30. use OCP\Dashboard\IManager;
  31. use OCP\Dashboard\IWidget;
  32. use OCP\Dashboard\Model\WidgetButton;
  33. use OCP\Dashboard\Model\WidgetOptions;
  34. use OCP\IConfig;
  35. use OCP\IRequest;
  36. use OCP\Dashboard\IAPIWidget;
  37. use OCP\Dashboard\Model\WidgetItem;
  38. class DashboardApiController extends OCSController {
  39. /** @var IManager */
  40. private $dashboardManager;
  41. /** @var IConfig */
  42. private $config;
  43. /** @var string|null */
  44. private $userId;
  45. public function __construct(
  46. string $appName,
  47. IRequest $request,
  48. IManager $dashboardManager,
  49. IConfig $config,
  50. ?string $userId
  51. ) {
  52. parent::__construct($appName, $request);
  53. $this->dashboardManager = $dashboardManager;
  54. $this->config = $config;
  55. $this->userId = $userId;
  56. }
  57. /**
  58. * Example request with Curl:
  59. * 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"}}'
  60. *
  61. * @param array $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items
  62. * @param int $limit Limit number of result items per widget
  63. * @param string[] $widgets Limit results to specific widgets
  64. *
  65. * @NoAdminRequired
  66. * @NoCSRFRequired
  67. */
  68. public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
  69. $showWidgets = $widgets;
  70. $items = [];
  71. if (empty($showWidgets)) {
  72. $systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
  73. $showWidgets = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault));
  74. }
  75. $widgets = $this->dashboardManager->getWidgets();
  76. foreach ($widgets as $widget) {
  77. if ($widget instanceof IAPIWidget && in_array($widget->getId(), $showWidgets)) {
  78. $items[$widget->getId()] = array_map(function (WidgetItem $item) {
  79. return $item->jsonSerialize();
  80. }, $widget->getItems($this->userId, $sinceIds[$widget->getId()] ?? null, $limit));
  81. }
  82. }
  83. return new DataResponse($items);
  84. }
  85. /**
  86. * Example request with Curl:
  87. * curl -u user:passwd http://my.nc/ocs/v2.php/apps/dashboard/api/v1/widgets
  88. *
  89. * @NoAdminRequired
  90. * @NoCSRFRequired
  91. */
  92. public function getWidgets(): DataResponse {
  93. $widgets = $this->dashboardManager->getWidgets();
  94. $items = array_map(function (IWidget $widget) {
  95. $options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault();
  96. $data = [
  97. 'id' => $widget->getId(),
  98. 'title' => $widget->getTitle(),
  99. 'order' => $widget->getOrder(),
  100. 'icon_class' => $widget->getIconClass(),
  101. 'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
  102. 'widget_url' => $widget->getUrl(),
  103. 'item_icons_round' => $options->withRoundItemIcons(),
  104. ];
  105. if ($widget instanceof IButtonWidget) {
  106. $data += [
  107. 'buttons' => array_map(function (WidgetButton $button) {
  108. return [
  109. 'type' => $button->getType(),
  110. 'text' => $button->getText(),
  111. 'link' => $button->getLink(),
  112. ];
  113. }, $widget->getWidgetButtons($this->userId)),
  114. ];
  115. }
  116. return $data;
  117. }, $widgets);
  118. return new DataResponse($items);
  119. }
  120. }