Passed
Push — master ( 6b6049...51add7 )
by Julius
28:38 queued 13:15
created

DashboardApiController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 18
c 1
b 0
f 1
dl 0
loc 47
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getWidgetItems() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2021 Julien Veyssier <[email protected]>
7
 *
8
 * @author Julien Veyssier <[email protected]>
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|null */
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
}
87