-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathGoogleAPIController.php
More file actions
179 lines (166 loc) · 5.15 KB
/
GoogleAPIController.php
File metadata and controls
179 lines (166 loc) · 5.15 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Nextcloud - google
*
* 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\Google\Controller;
use OCA\Google\AppInfo\Application;
use OCA\Google\Service\GoogleCalendarAPIService;
use OCA\Google\Service\GoogleContactsAPIService;
use OCA\Google\Service\GoogleDriveAPIService;
use OCA\Google\Service\SecretService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\DataResponse;
use OCP\IConfig;
use OCP\IRequest;
class GoogleAPIController extends Controller {
private string $accessToken;
public function __construct(
string $appName,
IRequest $request,
private IConfig $config,
private GoogleContactsAPIService $googleContactsAPIService,
private GoogleDriveAPIService $googleDriveAPIService,
private GoogleCalendarAPIService $googleCalendarAPIService,
private ?string $userId,
private SecretService $secretService,
) {
parent::__construct($appName, $request);
$this->accessToken = $this->userId !== null ? $this->secretService->getEncryptedUserValue($this->userId, 'token') : '';
}
/**
* @NoAdminRequired
*
* @return DataResponse
*/
public function getImportDriveInformation(): DataResponse {
if ($this->accessToken === '') {
return new DataResponse([], 400);
}
return new DataResponse([
'importing_drive' => $this->config->getUserValue($this->userId, Application::APP_ID, 'importing_drive') === '1',
'last_drive_import_timestamp' => (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'last_drive_import_timestamp', '0'),
'nb_imported_files' => (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'nb_imported_files', '0'),
'drive_imported_size' => (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'drive_imported_size', '0'),
]);
}
/**
* @NoAdminRequired
*
* @return DataResponse
*/
public function getContactNumber(): DataResponse {
if ($this->accessToken === '' || $this->userId === null) {
return new DataResponse([], 400);
}
/** @var array{error?:string} $result */
$result = $this->googleContactsAPIService->getContactNumber($this->userId);
if (isset($result['error'])) {
$response = new DataResponse($result['error'], 401);
} else {
$response = new DataResponse($result);
}
return $response;
}
/**
* @NoAdminRequired
*
* @return DataResponse
*/
public function getCalendarList(): DataResponse {
if ($this->accessToken === '' || $this->userId === null) {
return new DataResponse([], 400);
}
/** @var array{error?:string} $result */
$result = $this->googleCalendarAPIService->getCalendarList($this->userId);
if (isset($result['error'])) {
$response = new DataResponse($result['error'], 401);
} else {
$response = new DataResponse($result);
}
return $response;
}
/**
* @NoAdminRequired
*
* @return DataResponse
*/
public function getDriveSize(): DataResponse {
if ($this->accessToken === '' || $this->userId === null) {
return new DataResponse([], 400);
}
/** @var array{error?:string} $result */
$result = $this->googleDriveAPIService->getDriveSize($this->userId);
if (isset($result['error'])) {
$response = new DataResponse($result['error'], 401);
} else {
$response = new DataResponse($result);
}
return $response;
}
/**
* @NoAdminRequired
*
* @return DataResponse
*/
public function importDrive(): DataResponse {
if ($this->accessToken === '' || $this->userId === null) {
return new DataResponse([], 400);
}
/** @var array{error?:string} $result */
$result = $this->googleDriveAPIService->startImportDrive($this->userId);
if (isset($result['error'])) {
$response = new DataResponse($result['error'], 401);
} else {
$response = new DataResponse($result);
}
return $response;
}
/**
* @NoAdminRequired
*
* @param string $calId
* @param string $calName
* @param ?string $color
* @return DataResponse
*/
public function importCalendar(string $calId, string $calName, ?string $color = null): DataResponse {
if ($this->accessToken === '' || $this->userId === null) {
return new DataResponse([], 400);
}
/** @var array{error?:string} $result */
$result = $this->googleCalendarAPIService->importCalendar($this->userId, $calId, $calName, $color);
if (isset($result['error'])) {
$response = new DataResponse($result['error'], 401);
} else {
$response = new DataResponse($result);
}
return $response;
}
/**
* @NoAdminRequired
*
* @param ?string $uri
* @param int $key
* @param ?string $newAddressBookName
* @return DataResponse
*/
public function importContacts(?string $uri = '', int $key = 0, ?string $newAddressBookName = ''): DataResponse {
if ($this->accessToken === '' || $this->userId === null) {
return new DataResponse([], 400);
}
/** @var array{error?:string} $result */
$result = $this->googleContactsAPIService->importContacts($this->userId, $uri, $key, $newAddressBookName);
if (isset($result['error'])) {
$response = new DataResponse($result['error'], 401);
} else {
$response = new DataResponse($result);
}
return $response;
}
}