-
Notifications
You must be signed in to change notification settings - Fork 19
/
OpenProjectAPIController.php
134 lines (125 loc) · 3.71 KB
/
OpenProjectAPIController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* Nextcloud - openproject
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Julien Veyssier <eneiluj@posteo.net>
* @copyright Julien Veyssier 2020
*/
namespace OCA\OpenProject\Controller;
use OCP\AppFramework\Http\DataDisplayResponse;
use OCP\AppFramework\Http\DataDownloadResponse;
use OCP\IConfig;
use OCP\IRequest;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Controller;
use OCA\OpenProject\Service\OpenProjectAPIService;
use OCA\OpenProject\AppInfo\Application;
class OpenProjectAPIController extends Controller {
/**
* @var OpenProjectAPIService
*/
private $openprojectAPIService;
/**
* @var string|null
*/
private $userId;
/**
* @var string
*/
private $accessToken;
/**
* @var string
*/
private $tokenType;
/**
* @var string
*/
private $refreshToken;
/**
* @var string
*/
private $clientID;
/**
* @var string
*/
private $clientSecret;
/**
* @var string
*/
private $openprojectUrl;
public function __construct(string $appName,
IRequest $request,
IConfig $config,
OpenProjectAPIService $openprojectAPIService,
?string $userId) {
parent::__construct($appName, $request);
$this->openprojectAPIService = $openprojectAPIService;
$this->userId = $userId;
$this->accessToken = $config->getUserValue($userId, Application::APP_ID, 'token');
$this->tokenType = $config->getUserValue($userId, Application::APP_ID, 'token_type');
$this->refreshToken = $config->getUserValue($userId, Application::APP_ID, 'refresh_token');
$this->clientID = $config->getAppValue(Application::APP_ID, 'client_id');
$this->clientSecret = $config->getAppValue(Application::APP_ID, 'client_secret');
$this->openprojectUrl = $config->getUserValue($userId, Application::APP_ID, 'url');
}
/**
* get openproject instance URL
* @NoAdminRequired
*
* @return DataResponse
*/
public function getOpenProjectUrl(): DataResponse {
return new DataResponse($this->openprojectUrl);
}
/**
* get openproject user avatar
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $userId
* @param string $userName
* @return DataDisplayResponse|DataDownloadResponse
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Files\NotPermittedException
* @throws \OCP\Lock\LockedException
*/
public function getOpenProjectAvatar(string $userId = '', string $userName = '') {
$result = $this->openprojectAPIService->getOpenProjectAvatar(
$this->openprojectUrl, $this->accessToken, $this->tokenType, $this->refreshToken,
$this->clientID, $this->clientSecret, $userId, $userName
);
// TODO this will never happen, because `getOpenProjectAvatar` does not return ever an error result
// @phpstan-ignore-next-line
if (isset($result['error'])) {
$response = new DataDisplayResponse($result['error'], 404);
} else {
$response = new DataDownloadResponse($result['avatar'], 'avatar', $result['type'] ?? '');
$response->cacheFor(60 * 60 * 24);
}
return $response;
}
/**
* get notifications list
* @NoAdminRequired
*
* @param ?string $since
* @return DataResponse
*/
public function getNotifications(?string $since = null): DataResponse {
if ($this->accessToken === '' || !OpenProjectAPIService::validateOpenProjectURL($this->openprojectUrl)) {
return new DataResponse('', 400);
}
$result = $this->openprojectAPIService->getNotifications(
$this->openprojectUrl, $this->accessToken, $this->tokenType, $this->refreshToken, $this->clientID, $this->clientSecret, $this->userId, $since, 7
);
if (!isset($result['error'])) {
$response = new DataResponse($result);
} else {
$response = new DataResponse($result, 401);
}
return $response;
}
}