Skip to content

Commit

Permalink
add some debug log in contact import
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Dec 22, 2022
1 parent a2c6081 commit bc4338a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 51 deletions.
31 changes: 13 additions & 18 deletions lib/Service/GoogleAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
use Throwable;

class GoogleAPIService {
/**
* @var string
*/
private $appName;
/**
* @var LoggerInterface
*/
Expand Down Expand Up @@ -60,7 +56,6 @@ public function __construct (string $appName,
IConfig $config,
INotificationManager $notificationManager,
IClientService $clientService) {
$this->appName = $appName;
$this->logger = $logger;
$this->l10n = $l10n;
$this->config = $config;
Expand Down Expand Up @@ -137,7 +132,7 @@ public function request(string $userId, string $endPoint, array $params = [],
$this->logger->debug(
'REQUESTING Google API, method '.$method.', URL: ' . $url . ' , params: ' . json_encode($params)
. 'token length: ' . strlen($accessToken),
['app' => $this->appName]
['app' => Application::APP_ID]
);

if ($method === 'GET') {
Expand All @@ -157,14 +152,14 @@ public function request(string $userId, string $endPoint, array $params = [],
if ($respCode >= 400) {
$this->logger->debug(
'Google API request 400 FAILURE, method '.$method.', URL: ' . $url . ' , body: ' . $body,
['app' => $this->appName]
['app' => Application::APP_ID]
);
return ['error' => 'Bad credentials'];
} else {
$this->logger->debug(
'Google API request SUCCESS: , method ' . $method . ', URL: ' . $url
. ' , body:' . substr($body, 0, 30) . '...',
['app' => $this->appName]
['app' => Application::APP_ID]
);
return json_decode($body, true);
}
Expand All @@ -175,15 +170,15 @@ public function request(string $userId, string $endPoint, array $params = [],
'Google API ServerException|ClientException error : '.$e->getMessage()
. ' status code: ' .$response->getStatusCode()
. ' body: ' . $body,
['app' => $this->appName]
['app' => Application::APP_ID]
);
return [
'error' => 'ServerException|ClientException, message:'
. $e->getMessage()
. ' status code: ' . $response->getStatusCode(),
];
} catch (ConnectException $e) {
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => $this->appName]);
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => Application::APP_ID]);
return ['error' => 'Connection error: ' . $e->getMessage()];
}
}
Expand Down Expand Up @@ -231,7 +226,7 @@ public function requestOAuthAccessToken(array $params = [], string $method = 'GE
return json_decode($body, true);
}
} catch (Exception $e) {
$this->logger->warning('Google OAuth error : '.$e->getMessage(), ['app' => $this->appName]);
$this->logger->warning('Google OAuth error : '.$e->getMessage(), ['app' => Application::APP_ID]);
return ['error' => $e->getMessage()];
}
}
Expand Down Expand Up @@ -287,10 +282,10 @@ public function simpleRequest(string $userId, string $url, array $params = [], s
];
}
} catch (ServerException | ClientException $e) {
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => $this->appName]);
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => Application::APP_ID]);
return ['error' => $e->getMessage()];
} catch (ConnectException $e) {
$this->logger->error('Google API request connection error: ' . $e->getMessage(), ['app' => $this->appName]);
$this->logger->error('Google API request connection error: ' . $e->getMessage(), ['app' => Application::APP_ID]);
return ['error' => 'Connection error: ' . $e->getMessage()];
}
}
Expand Down Expand Up @@ -354,10 +349,10 @@ public function simpleDownload(string $userId, string $url, $resource, array $pa
return ['success' => true];
}
} catch (ServerException | ClientException $e) {
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => $this->appName]);
$this->logger->warning('Google API error : '.$e->getMessage(), ['app' => Application::APP_ID]);
return ['error' => $e->getMessage()];
} catch (ConnectException $e) {
$this->logger->error('Google API request connection error: ' . $e->getMessage(), ['app' => $this->appName]);
$this->logger->error('Google API request connection error: ' . $e->getMessage(), ['app' => Application::APP_ID]);
return ['error' => 'Connection error: ' . $e->getMessage()];
} catch (Throwable | Exception $e) {
return ['error' => 'Unknown error: ' . $e->getMessage()];
Expand All @@ -378,7 +373,7 @@ private function checkTokenExpiration(string $userId): void {
}

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

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

return $result;
Expand Down
12 changes: 4 additions & 8 deletions lib/Service/GoogleCalendarAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use DateTimeZone;
use Exception;
use Generator;
use OCA\Google\AppInfo\Application;
use OCP\IL10N;
use OCA\DAV\CalDAV\CalDavBackend;
use Sabre\DAV\Exception\BadRequest;
Expand All @@ -26,10 +27,6 @@
use Throwable;

class GoogleCalendarAPIService {
/**
* @var string
*/
private $appName;
/**
* @var LoggerInterface
*/
Expand All @@ -55,7 +52,6 @@ public function __construct (string $appName,
IL10N $l10n,
CalDavBackend $caldavBackend,
GoogleAPIService $googleApiService) {
$this->appName = $appName;
$this->logger = $logger;
$this->l10n = $l10n;
$this->caldavBackend = $caldavBackend;
Expand Down Expand Up @@ -263,12 +259,12 @@ public function importCalendar(string $userId, string $calId, string $calName, ?
$nbAdded++;
} catch (BadRequest $ex) {
if (strpos($ex->getMessage(), 'uid already exists') !== false) {
$this->logger->debug('Skip existing event', ['app' => $this->appName]);
$this->logger->debug('Skip existing event', ['app' => Application::APP_ID]);
} else {
$this->logger->warning('Error when creating calendar event "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => $this->appName]);
$this->logger->warning('Error when creating calendar event "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => Application::APP_ID]);
}
} catch (Exception | Throwable $ex) {
$this->logger->warning('Error when creating calendar event "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => $this->appName]);
$this->logger->warning('Error when creating calendar event "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => Application::APP_ID]);
}
}

Expand Down
17 changes: 10 additions & 7 deletions lib/Service/GoogleContactsAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
use Datetime;
use Exception;
use Generator;
use OCA\Google\AppInfo\Application;
use OCP\Contacts\IManager as IContactManager;
use Sabre\VObject\Component\VCard;
use OCA\DAV\CardDAV\CardDavBackend;
use Psr\Log\LoggerInterface;
use Throwable;

class GoogleContactsAPIService {
/**
* @var string
*/
private $appName;
/**
* @var LoggerInterface
*/
Expand All @@ -50,7 +47,6 @@ public function __construct (string $appName,
IContactManager $contactsManager,
CardDavBackend $cdBackend,
GoogleAPIService $googleApiService) {
$this->appName = $appName;
$this->logger = $logger;
$this->contactsManager = $contactsManager;
$this->cdBackend = $cdBackend;
Expand Down Expand Up @@ -189,9 +185,12 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
$groupsById = $this->getContactGroupsById($userId);
$contacts = $this->getContactList($userId);
$nbAdded = 0;
$totalContactNumber = 0;
foreach ($contacts as $k => $c) {
$totalContactNumber++;
// avoid existing contacts
if ($this->contactExists($c, $key)) {
$this->logger->debug('Skipping contact which already exists', ['contact' => $c, 'app' => Application::APP_ID]);
continue;
}
$vCard = new VCard();
Expand All @@ -201,6 +200,7 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
$firstName = null;
// we just take first name
if (!isset($c['names']) || !is_array($c['names'])) {
$this->logger->debug('Skipping contact with no names array', ['app' => Application::APP_ID]);
continue;
}
foreach ($c['names'] as $n) {
Expand All @@ -219,6 +219,7 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
}
// we don't want empty names
if (!$displayName && !$familyName && !$firstName) {
$this->logger->debug('Skipping contact with no displayname/familyname/firstname', ['app' => Application::APP_ID]);
continue;
}

Expand Down Expand Up @@ -307,7 +308,7 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
);
$vCard->add($prop);
} catch (Exception|Throwable $ex) {
$this->logger->warning('Error when setting contact photo "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => $this->appName]);
$this->logger->warning('Error when setting contact photo "' . '<redacted>' . '" ' . $ex->getMessage(), ['app' => Application::APP_ID]);
}
break;
}
Expand Down Expand Up @@ -418,9 +419,11 @@ public function importContacts(string $userId, ?string $uri, int $key, ?string $
$this->cdBackend->createCard($key, 'goog' . $k, $vCard->serialize());
$nbAdded++;
} catch (Throwable | Exception $e) {
$this->logger->warning('Error when creating contact', ['app' => $this->appName]);
$this->logger->warning('Error when creating contact', ['exception' => $e, 'app' => Application::APP_ID]);
}
}
$this->logger->debug($totalContactNumber . ' contacts seen', ['app' => Application::APP_ID]);
$this->logger->debug($nbAdded . ' contacts imported', ['app' => Application::APP_ID]);
$contactGeneratorReturn = $contacts->getReturn();
if (isset($contactGeneratorReturn['error'])) {
return $contactGeneratorReturn;
Expand Down
7 changes: 1 addition & 6 deletions lib/Service/GoogleDriveAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@
use Throwable;

class GoogleDriveAPIService {
/**
* @var string
*/
private $appName;
/**
* @var LoggerInterface
*/
Expand Down Expand Up @@ -75,7 +71,6 @@ public function __construct (string $appName,
IJobList $jobList,
UserScopeService $userScopeService,
GoogleAPIService $googleApiService) {
$this->appName = $appName;
$this->logger = $logger;
$this->config = $config;
$this->root = $root;
Expand Down Expand Up @@ -221,7 +216,7 @@ public function importDriveJob(string $userId): void {
]);
}
if (isset($result['error'])) {
$this->logger->error('Google Drive import error: ' . $result['error'], ['app' => $this->appName]);
$this->logger->error('Google Drive import error: ' . $result['error'], ['app' => Application::APP_ID]);
}
$this->config->setUserValue($userId, Application::APP_ID, 'importing_drive', '0');
$this->config->setUserValue($userId, Application::APP_ID, 'nb_imported_files', '0');
Expand Down
19 changes: 7 additions & 12 deletions lib/Service/GooglePhotosAPIService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
use Throwable;

class GooglePhotosAPIService {
/**
* @var string
*/
private $appName;
/**
* @var LoggerInterface
*/
Expand Down Expand Up @@ -66,7 +62,6 @@ public function __construct (string $appName,
IJobList $jobList,
UserScopeService $userScopeService,
GoogleAPIService $googleApiService) {
$this->appName = $appName;
$this->logger = $logger;
$this->config = $config;
$this->root = $root;
Expand All @@ -87,7 +82,7 @@ public function getPhotoNumber(string $userId): array {
do {
$this->logger->debug(
'Photos service::getPhotoNumber LAUNCHING ALBUM LIST REQUEST, userid: "' . $userId . '"',
['app' => $this->appName]
['app' => Application::APP_ID]
);
$result = $this->googleApiService->request($userId, 'v1/albums', $params, 'GET', 'https://photoslibrary.googleapis.com/');
if (isset($result['error'])) {
Expand Down Expand Up @@ -139,7 +134,7 @@ public function getPhotoNumber(string $userId): array {
$this->logger->warning(
'Google API error getting media items list to get photo number, no "mediaItems" key in '
. json_encode($result),
['app' => $this->appName]
['app' => Application::APP_ID]
);
}
}
Expand Down Expand Up @@ -224,7 +219,7 @@ public function importPhotosJob(string $userId): void {
]);
}
if (isset($result['error'])) {
$this->logger->error('Google Photo import error: ' . $result['error'], ['app' => $this->appName]);
$this->logger->error('Google Photo import error: ' . $result['error'], ['app' => Application::APP_ID]);
}
} else {
$ts = (new Datetime())->getTimestamp();
Expand Down Expand Up @@ -261,7 +256,7 @@ public function importPhotos(string $userId, string $targetPath,
do {
$this->logger->debug(
'Photos service::importPhotos LAUNCHING ALBUM LIST REQUEST, userid: "' . $userId . '"',
['app' => $this->appName]
['app' => Application::APP_ID]
);
$result = $this->googleApiService->request($userId, 'v1/albums', $params, 'GET', 'https://photoslibrary.googleapis.com/');
if (isset($result['error'])) {
Expand Down Expand Up @@ -298,7 +293,7 @@ public function importPhotos(string $userId, string $targetPath,
// get the photos
$this->logger->debug(
'Photos service::importPhotos GETTING PHOTOS, nb albums: "' . count($albums) . '"',
['app' => $this->appName]
['app' => Application::APP_ID]
);
$downloadedSize = 0;
$nbDownloaded = 0;
Expand Down Expand Up @@ -414,7 +409,7 @@ private function getPhoto(string $userId, array $photo, Folder $albumFolder): ?i
try {
$resource = $savedFile->fopen('w');
} catch (LockedException $e) {
$this->logger->warning('Google Photo, error opening target file ' . '<redacted>' . ' : file is locked', ['app' => $this->appName]);
$this->logger->warning('Google Photo, error opening target file ' . '<redacted>' . ' : file is locked', ['app' => Application::APP_ID]);
return null;
}
$res = $this->googleApiService->simpleDownload($userId, $photoUrl, $resource);
Expand All @@ -432,7 +427,7 @@ private function getPhoto(string $userId, array $photo, Folder $albumFolder): ?i
$stat = $savedFile->stat();
return $stat['size'] ?? 0;
} else {
$this->logger->warning('Google API error downloading photo ' . '<redacted>' . ' : ' . $res['error'], ['app' => $this->appName]);
$this->logger->warning('Google API error downloading photo ' . '<redacted>' . ' : ' . $res['error'], ['app' => Application::APP_ID]);
if ($savedFile->isDeletable()) {
$savedFile->unlock(ILockingProvider::LOCK_EXCLUSIVE);
$savedFile->delete();
Expand Down

0 comments on commit bc4338a

Please sign in to comment.