Skip to content

Commit e44d9b8

Browse files
author
Julien Veyssier
committed
implement dashboard API for clients, new WidgetItem class, new IAPIWidget interface
Signed-off-by: Julien Veyssier <eneiluj@posteo.net>
1 parent 2056b76 commit e44d9b8

4 files changed

Lines changed: 259 additions & 0 deletions

File tree

apps/dashboard/appinfo/routes.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@
3131
['name' => 'dashboard#updateStatuses', 'url' => '/statuses', 'verb' => 'POST'],
3232
['name' => 'dashboard#getBackground', 'url' => '/background', 'verb' => 'GET'],
3333
['name' => 'dashboard#setBackground', 'url' => '/background/{type}', 'verb' => 'POST'],
34+
],
35+
'ocs' => [
36+
['name' => 'dashboardApi#getWidgetItems', 'url' => '/api/v1/widget-items', 'verb' => 'GET'],
3437
]
3538
];
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2021 Julien Veyssier <eneiluj@posteo.net>
7+
*
8+
* @author Julien Veyssier <eneiluj@posteo.net>
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+
27+
namespace OCA\Dashboard\Controller;
28+
29+
use OCP\AppFramework\OCSController;
30+
use OCP\AppFramework\Http\DataResponse;
31+
use OCP\Dashboard\IManager;
32+
use OCP\IConfig;
33+
use OCP\IRequest;
34+
35+
use OCP\Dashboard\IAPIWidget;
36+
use OCP\Dashboard\Model\WidgetItem;
37+
38+
class DashboardApiController extends OCSController {
39+
40+
/** @var IManager */
41+
private $dashboardManager;
42+
/** @var IConfig */
43+
private $config;
44+
/** @var string */
45+
private $userId;
46+
47+
public function __construct(string $appName,
48+
IRequest $request,
49+
IManager $dashboardManager,
50+
IConfig $config,
51+
?string $userId) {
52+
parent::__construct($appName, $request);
53+
54+
$this->dashboardManager = $dashboardManager;
55+
$this->config = $config;
56+
$this->userId = $userId;
57+
}
58+
59+
/**
60+
* Example request with Curl:
61+
* 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"}}'
62+
*
63+
* @param array $sinceIds Array indexed by widget Ids, contains date/id from which we want the new items
64+
* @param int $limit Limit number of result items per widget
65+
*
66+
* @NoAdminRequired
67+
* @NoCSRFRequired
68+
*/
69+
public function getWidgetItems(array $sinceIds = [], int $limit = 7): DataResponse {
70+
$items = [];
71+
72+
$systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
73+
$userLayout = 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(), $userLayout)) {
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+
84+
return new DataResponse($items);
85+
}
86+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2021 Julien Veyssier <eneiluj@posteo.net>
7+
*
8+
* @author Julien Veyssier <eneiluj@posteo.net>
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+
27+
namespace OCP\Dashboard;
28+
29+
/**
30+
* interface IAPIWidget
31+
*
32+
* @since 22.0.0
33+
*/
34+
interface IAPIWidget extends IWidget {
35+
36+
/**
37+
* @return WidgetItem[] The widget items
38+
* @since 22.0.0
39+
*/
40+
public function getItems(string $userId, ?string $since = null, int $limit = 7): array;
41+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2021, Julien Veyssier <eneiluj@posteo.net>
7+
*
8+
* @author Julien Veyssier <eneiluj@posteo.net>
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+
27+
namespace OCP\Dashboard\Model;
28+
29+
/**
30+
* Class WidgetItem
31+
*
32+
* This class is used by IAPIWidget interface.
33+
* It represents an widget item data that can be provided to clients via the Dashboard API
34+
* @see IAPIWidget::getWidgetItems
35+
*
36+
* @since 22.0.0
37+
*
38+
*/
39+
final class WidgetItem {
40+
/** @var string */
41+
private $title = '';
42+
43+
/** @var string */
44+
private $subtitle = '';
45+
46+
/** @var string */
47+
private $iconUrl = '';
48+
49+
/** @var string
50+
* Timestamp or ID used by the dashboard API to avoid getting already retrieved items
51+
*/
52+
private $sinceId = '';
53+
54+
55+
/**
56+
* WidgetItem constructor
57+
*
58+
* @since 22.0.0
59+
*
60+
* @param string $type
61+
*/
62+
public function __construct(string $title = '',
63+
string $subtitle = '',
64+
string $iconUrl = '',
65+
string $sinceId = '') {
66+
$this->title = $title;
67+
$this->subtitle = $subtitle;
68+
$this->iconUrl = $iconUrl;
69+
$this->sinceId = $sinceId;
70+
}
71+
72+
/**
73+
* Get the item title
74+
*
75+
* @since 22.0.0
76+
*
77+
* @return string
78+
*/
79+
public function getTitle(): string {
80+
return $this->title;
81+
}
82+
83+
/**
84+
* Get the item subtitle
85+
*
86+
* @since 22.0.0
87+
*
88+
* @return string
89+
*/
90+
public function getSubtitle(): string {
91+
return $this->subtitle;
92+
}
93+
94+
/**
95+
* Get the item icon URL
96+
*
97+
* @since 22.0.0
98+
*
99+
* @return string
100+
*/
101+
public function getIconUrl(): string {
102+
return $this->iconUrl;
103+
}
104+
105+
/**
106+
* Get the item since ID
107+
*
108+
* @since 22.0.0
109+
*
110+
* @return string
111+
*/
112+
public function getSinceId(): string {
113+
return $this->sinceId;
114+
}
115+
116+
/**
117+
* @since 22.0.0
118+
*
119+
* @return array
120+
*/
121+
public function jsonSerialize(): array {
122+
return [
123+
'subtitle' => $this->getSubtitle(),
124+
'title' => $this->getTitle(),
125+
'iconUrl' => $this->getIconUrl(),
126+
'sinceId' => $this->getSinceId(),
127+
];
128+
}
129+
}

0 commit comments

Comments
 (0)