Skip to content

Commit bc4338a

Browse files
committed
add some debug log in contact import
Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
1 parent a2c6081 commit bc4338a

File tree

5 files changed

+35
-51
lines changed

5 files changed

+35
-51
lines changed

lib/Service/GoogleAPIService.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@
2626
use Throwable;
2727

2828
class GoogleAPIService {
29-
/**
30-
* @var string
31-
*/
32-
private $appName;
3329
/**
3430
* @var LoggerInterface
3531
*/
@@ -60,7 +56,6 @@ public function __construct (string $appName,
6056
IConfig $config,
6157
INotificationManager $notificationManager,
6258
IClientService $clientService) {
63-
$this->appName = $appName;
6459
$this->logger = $logger;
6560
$this->l10n = $l10n;
6661
$this->config = $config;
@@ -137,7 +132,7 @@ public function request(string $userId, string $endPoint, array $params = [],
137132
$this->logger->debug(
138133
'REQUESTING Google API, method '.$method.', URL: ' . $url . ' , params: ' . json_encode($params)
139134
. 'token length: ' . strlen($accessToken),
140-
['app' => $this->appName]
135+
['app' => Application::APP_ID]
141136
);
142137

143138
if ($method === 'GET') {
@@ -157,14 +152,14 @@ public function request(string $userId, string $endPoint, array $params = [],
157152
if ($respCode >= 400) {
158153
$this->logger->debug(
159154
'Google API request 400 FAILURE, method '.$method.', URL: ' . $url . ' , body: ' . $body,
160-
['app' => $this->appName]
155+
['app' => Application::APP_ID]
161156
);
162157
return ['error' => 'Bad credentials'];
163158
} else {
164159
$this->logger->debug(
165160
'Google API request SUCCESS: , method ' . $method . ', URL: ' . $url
166161
. ' , body:' . substr($body, 0, 30) . '...',
167-
['app' => $this->appName]
162+
['app' => Application::APP_ID]
168163
);
169164
return json_decode($body, true);
170165
}
@@ -175,15 +170,15 @@ public function request(string $userId, string $endPoint, array $params = [],
175170
'Google API ServerException|ClientException error : '.$e->getMessage()
176171
. ' status code: ' .$response->getStatusCode()
177172
. ' body: ' . $body,
178-
['app' => $this->appName]
173+
['app' => Application::APP_ID]
179174
);
180175
return [
181176
'error' => 'ServerException|ClientException, message:'
182177
. $e->getMessage()
183178
. ' status code: ' . $response->getStatusCode(),
184179
];
185180
} catch (ConnectException $e) {
186-
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => $this->appName]);
181+
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => Application::APP_ID]);
187182
return ['error' => 'Connection error: ' . $e->getMessage()];
188183
}
189184
}
@@ -231,7 +226,7 @@ public function requestOAuthAccessToken(array $params = [], string $method = 'GE
231226
return json_decode($body, true);
232227
}
233228
} catch (Exception $e) {
234-
$this->logger->warning('Google OAuth error : '.$e->getMessage(), ['app' => $this->appName]);
229+
$this->logger->warning('Google OAuth error : '.$e->getMessage(), ['app' => Application::APP_ID]);
235230
return ['error' => $e->getMessage()];
236231
}
237232
}
@@ -287,10 +282,10 @@ public function simpleRequest(string $userId, string $url, array $params = [], s
287282
];
288283
}
289284
} catch (ServerException | ClientException $e) {
290-
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => $this->appName]);
285+
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => Application::APP_ID]);
291286
return ['error' => $e->getMessage()];
292287
} catch (ConnectException $e) {
293-
$this->logger->error('Google API request connection error: ' . $e->getMessage(), ['app' => $this->appName]);
288+
$this->logger->error('Google API request connection error: ' . $e->getMessage(), ['app' => Application::APP_ID]);
294289
return ['error' => 'Connection error: ' . $e->getMessage()];
295290
}
296291
}
@@ -354,10 +349,10 @@ public function simpleDownload(string $userId, string $url, $resource, array $pa
354349
return ['success' => true];
355350
}
356351
} catch (ServerException | ClientException $e) {
357-
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => $this->appName]);
352+
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => Application::APP_ID]);
358353
return ['error' => $e->getMessage()];
359354
} catch (ConnectException $e) {
360-
$this->logger->error('Google API request connection error: ' . $e->getMessage(), ['app' => $this->appName]);
355+
$this->logger->error('Google API request connection error: ' . $e->getMessage(), ['app' => Application::APP_ID]);
361356
return ['error' => 'Connection error: ' . $e->getMessage()];
362357
} catch (Throwable | Exception $e) {
363358
return ['error' => 'Unknown error: ' . $e->getMessage()];
@@ -378,7 +373,7 @@ private function checkTokenExpiration(string $userId): void {
378373
}
379374

380375
public function refreshToken(string $userId): array {
381-
$this->logger->debug('Trying to REFRESH the access token', ['app' => $this->appName]);
376+
$this->logger->debug('Trying to REFRESH the access token', ['app' => Application::APP_ID]);
382377
$refreshToken = $this->config->getUserValue($userId, Application::APP_ID, 'refresh_token');
383378
$clientID = $this->config->getAppValue(Application::APP_ID, 'client_id');
384379
$clientSecret = $this->config->getAppValue(Application::APP_ID, 'client_secret');
@@ -390,7 +385,7 @@ public function refreshToken(string $userId): array {
390385
], 'POST');
391386

392387
if (isset($result['access_token'])) {
393-
$this->logger->debug('Google access token successfully refreshed', ['app' => $this->appName]);
388+
$this->logger->debug('Google access token successfully refreshed', ['app' => Application::APP_ID]);
394389
$this->config->setUserValue($userId, Application::APP_ID, 'token', $result['access_token']);
395390
if (isset($result['expires_in'])) {
396391
$nowTs = (new Datetime())->getTimestamp();
@@ -399,7 +394,7 @@ public function refreshToken(string $userId): array {
399394
}
400395
} else {
401396
$responseTxt = json_encode($result);
402-
$this->logger->warning('Google API error, impossible to refresh the token. Response: ' . $responseTxt, ['app' => $this->appName]);
397+
$this->logger->warning('Google API error, impossible to refresh the token. Response: ' . $responseTxt, ['app' => Application::APP_ID]);
403398
}
404399

405400
return $result;

lib/Service/GoogleCalendarAPIService.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use DateTimeZone;
1616
use Exception;
1717
use Generator;
18+
use OCA\Google\AppInfo\Application;
1819
use OCP\IL10N;
1920
use OCA\DAV\CalDAV\CalDavBackend;
2021
use Sabre\DAV\Exception\BadRequest;
@@ -26,10 +27,6 @@
2627
use Throwable;
2728

2829
class GoogleCalendarAPIService {
29-
/**
30-
* @var string
31-
*/
32-
private $appName;
3330
/**
3431
* @var LoggerInterface
3532
*/
@@ -55,7 +52,6 @@ public function __construct (string $appName,
5552
IL10N $l10n,
5653
CalDavBackend $caldavBackend,
5754
GoogleAPIService $googleApiService) {
58-
$this->appName = $appName;
5955
$this->logger = $logger;
6056
$this->l10n = $l10n;
6157
$this->caldavBackend = $caldavBackend;
@@ -263,12 +259,12 @@ public function importCalendar(string $userId, string $calId, string $calName, ?
263259
$nbAdded++;
264260
} catch (BadRequest $ex) {
265261
if (strpos($ex->getMessage(), 'uid already exists') !== false) {
266-
$this->logger->debug('Skip existing event', ['app' => $this->appName]);
262+
$this->logger->debug('Skip existing event', ['app' => Application::APP_ID]);
267263
} else {
268-
$this->logger->warning('Error when creating calendar event "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => $this->appName]);
264+
$this->logger->warning('Error when creating calendar event "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => Application::APP_ID]);
269265
}
270266
} catch (Exception | Throwable $ex) {
271-
$this->logger->warning('Error when creating calendar event "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => $this->appName]);
267+
$this->logger->warning('Error when creating calendar event "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => Application::APP_ID]);
272268
}
273269
}
274270

lib/Service/GoogleContactsAPIService.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414
use Datetime;
1515
use Exception;
1616
use Generator;
17+
use OCA\Google\AppInfo\Application;
1718
use OCP\Contacts\IManager as IContactManager;
1819
use Sabre\VObject\Component\VCard;
1920
use OCA\DAV\CardDAV\CardDavBackend;
2021
use Psr\Log\LoggerInterface;
2122
use Throwable;
2223

2324
class GoogleContactsAPIService {
24-
/**
25-
* @var string
26-
*/
27-
private $appName;
2825
/**
2926
* @var LoggerInterface
3027
*/
@@ -50,7 +47,6 @@ public function __construct (string $appName,
5047
IContactManager $contactsManager,
5148
CardDavBackend $cdBackend,
5249
GoogleAPIService $googleApiService) {
53-
$this->appName = $appName;
5450
$this->logger = $logger;
5551
$this->contactsManager = $contactsManager;
5652
$this->cdBackend = $cdBackend;
@@ -189,9 +185,12 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
189185
$groupsById = $this->getContactGroupsById($userId);
190186
$contacts = $this->getContactList($userId);
191187
$nbAdded = 0;
188+
$totalContactNumber = 0;
192189
foreach ($contacts as $k => $c) {
190+
$totalContactNumber++;
193191
// avoid existing contacts
194192
if ($this->contactExists($c, $key)) {
193+
$this->logger->debug('Skipping contact which already exists', ['contact' => $c, 'app' => Application::APP_ID]);
195194
continue;
196195
}
197196
$vCard = new VCard();
@@ -201,6 +200,7 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
201200
$firstName = null;
202201
// we just take first name
203202
if (!isset($c['names']) || !is_array($c['names'])) {
203+
$this->logger->debug('Skipping contact with no names array', ['app' => Application::APP_ID]);
204204
continue;
205205
}
206206
foreach ($c['names'] as $n) {
@@ -219,6 +219,7 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
219219
}
220220
// we don't want empty names
221221
if (!$displayName && !$familyName && !$firstName) {
222+
$this->logger->debug('Skipping contact with no displayname/familyname/firstname', ['app' => Application::APP_ID]);
222223
continue;
223224
}
224225

@@ -307,7 +308,7 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
307308
);
308309
$vCard->add($prop);
309310
} catch (Exception|Throwable $ex) {
310-
$this->logger->warning('Error when setting contact photo "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => $this->appName]);
311+
$this->logger->warning('Error when setting contact photo "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => Application::APP_ID]);
311312
}
312313
break;
313314
}
@@ -418,9 +419,11 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
418419
$this->cdBackend->createCard($key, 'goog' . $k, $vCard->serialize());
419420
$nbAdded++;
420421
} catch (Throwable | Exception $e) {
421-
$this->logger->warning('Error when creating contact', ['app' => $this->appName]);
422+
$this->logger->warning('Error when creating contact', ['exception' => $e, 'app' => Application::APP_ID]);
422423
}
423424
}
425+
$this->logger->debug($totalContactNumber . ' contacts seen', ['app' => Application::APP_ID]);
426+
$this->logger->debug($nbAdded . ' contacts imported', ['app' => Application::APP_ID]);
424427
$contactGeneratorReturn = $contacts->getReturn();
425428
if (isset($contactGeneratorReturn['error'])) {
426429
return $contactGeneratorReturn;

lib/Service/GoogleDriveAPIService.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030
use Throwable;
3131

3232
class GoogleDriveAPIService {
33-
/**
34-
* @var string
35-
*/
36-
private $appName;
3733
/**
3834
* @var LoggerInterface
3935
*/
@@ -75,7 +71,6 @@ public function __construct (string $appName,
7571
IJobList $jobList,
7672
UserScopeService $userScopeService,
7773
GoogleAPIService $googleApiService) {
78-
$this->appName = $appName;
7974
$this->logger = $logger;
8075
$this->config = $config;
8176
$this->root = $root;
@@ -221,7 +216,7 @@ public function importDriveJob(string $userId): void {
221216
]);
222217
}
223218
if (isset($result['error'])) {
224-
$this->logger->error('Google Drive import error: ' . $result['error'], ['app' => $this->appName]);
219+
$this->logger->error('Google Drive import error: ' . $result['error'], ['app' => Application::APP_ID]);
225220
}
226221
$this->config->setUserValue($userId, Application::APP_ID, 'importing_drive', '0');
227222
$this->config->setUserValue($userId, Application::APP_ID, 'nb_imported_files', '0');

lib/Service/GooglePhotosAPIService.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@
2727
use Throwable;
2828

2929
class GooglePhotosAPIService {
30-
/**
31-
* @var string
32-
*/
33-
private $appName;
3430
/**
3531
* @var LoggerInterface
3632
*/
@@ -66,7 +62,6 @@ public function __construct (string $appName,
6662
IJobList $jobList,
6763
UserScopeService $userScopeService,
6864
GoogleAPIService $googleApiService) {
69-
$this->appName = $appName;
7065
$this->logger = $logger;
7166
$this->config = $config;
7267
$this->root = $root;
@@ -87,7 +82,7 @@ public function getPhotoNumber(string $userId): array {
8782
do {
8883
$this->logger->debug(
8984
'Photos service::getPhotoNumber LAUNCHING ALBUM LIST REQUEST, userid: "' . $userId . '"',
90-
['app' => $this->appName]
85+
['app' => Application::APP_ID]
9186
);
9287
$result = $this->googleApiService->request($userId, 'v1/albums', $params, 'GET', 'https://photoslibrary.googleapis.com/');
9388
if (isset($result['error'])) {
@@ -139,7 +134,7 @@ public function getPhotoNumber(string $userId): array {
139134
$this->logger->warning(
140135
'Google API error getting media items list to get photo number, no "mediaItems" key in '
141136
. json_encode($result),
142-
['app' => $this->appName]
137+
['app' => Application::APP_ID]
143138
);
144139
}
145140
}
@@ -224,7 +219,7 @@ public function importPhotosJob(string $userId): void {
224219
]);
225220
}
226221
if (isset($result['error'])) {
227-
$this->logger->error('Google Photo import error: ' . $result['error'], ['app' => $this->appName]);
222+
$this->logger->error('Google Photo import error: ' . $result['error'], ['app' => Application::APP_ID]);
228223
}
229224
} else {
230225
$ts = (new Datetime())->getTimestamp();
@@ -261,7 +256,7 @@ public function importPhotos(string $userId, string $targetPath,
261256
do {
262257
$this->logger->debug(
263258
'Photos service::importPhotos LAUNCHING ALBUM LIST REQUEST, userid: "' . $userId . '"',
264-
['app' => $this->appName]
259+
['app' => Application::APP_ID]
265260
);
266261
$result = $this->googleApiService->request($userId, 'v1/albums', $params, 'GET', 'https://photoslibrary.googleapis.com/');
267262
if (isset($result['error'])) {
@@ -298,7 +293,7 @@ public function importPhotos(string $userId, string $targetPath,
298293
// get the photos
299294
$this->logger->debug(
300295
'Photos service::importPhotos GETTING PHOTOS, nb albums: "' . count($albums) . '"',
301-
['app' => $this->appName]
296+
['app' => Application::APP_ID]
302297
);
303298
$downloadedSize = 0;
304299
$nbDownloaded = 0;
@@ -414,7 +409,7 @@ private function getPhoto(string $userId, array $photo, Folder $albumFolder): ?i
414409
try {
415410
$resource = $savedFile->fopen('w');
416411
} catch (LockedException $e) {
417-
$this->logger->warning('Google Photo, error opening target file ' . '<redacted>' . ' : file is locked', ['app' => $this->appName]);
412+
$this->logger->warning('Google Photo, error opening target file ' . '<redacted>' . ' : file is locked', ['app' => Application::APP_ID]);
418413
return null;
419414
}
420415
$res = $this->googleApiService->simpleDownload($userId, $photoUrl, $resource);
@@ -432,7 +427,7 @@ private function getPhoto(string $userId, array $photo, Folder $albumFolder): ?i
432427
$stat = $savedFile->stat();
433428
return $stat['size'] ?? 0;
434429
} else {
435-
$this->logger->warning('Google API error downloading photo ' . '<redacted>' . ' : ' . $res['error'], ['app' => $this->appName]);
430+
$this->logger->warning('Google API error downloading photo ' . '<redacted>' . ' : ' . $res['error'], ['app' => Application::APP_ID]);
436431
if ($savedFile->isDeletable()) {
437432
$savedFile->unlock(ILockingProvider::LOCK_EXCLUSIVE);
438433
$savedFile->delete();

0 commit comments

Comments
 (0)