Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
namespace OCA\Notifications;


use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use OC\Authentication\Exceptions\InvalidTokenException;
use OC\Authentication\Token\IProvider;
use OC\Security\IdentityProof\Key;
Expand Down Expand Up @@ -189,40 +191,56 @@ protected function sendNotificationsToProxies(array $pushNotifications): void {
'notifications' => $notifications,
],
]);
} catch (ClientException $e) {
// Server responded with 4xx (400 Bad Request mostlikely)
$response = $e->getResponse();
} catch (ServerException $e) {
// Server responded with 5xx
$response = $e->getResponse();
$body = $response->getBody();
$error = \is_string($body) ? $body : ('no reason given (' . $response->getStatusCode() . ')');

$this->log->debug('Could not send notification to push server [{url}]: {error}', [
'error' => $error,
'url' => $proxyServer,
'app' => 'notifications',
]);

continue;
} catch (\Exception $e) {
$this->log->logException($e, [
'app' => 'notifications',
'level' => $e->getCode() === Http::STATUS_BAD_REQUEST ? ILogger::INFO : ILogger::WARN,
'level' => ILogger::ERROR,
]);
continue;
}

$status = $response->getStatusCode();
if ($status === Http::STATUS_SERVICE_UNAVAILABLE && $this->config->getSystemValue('debug', false)) {
$body = $response->getBody();
$this->log->debug('Could not send notification to push server [{url}]: {error}',[
'error' => \is_string($body) ? $body : 'no reason given',
$body = $response->getBody();
$bodyData = json_decode($body, true);

if (is_array($bodyData) && isset($bodyData['unknown'], $bodyData['failed'])) {
if (is_array($bodyData['unknown'])) {
// Proxy returns null when the array is empty
foreach ($bodyData['unknown'] as $unknownDevice) {
$this->deletePushTokenByDeviceIdentifier($unknownDevice);
}
}
} elseif ($status !== Http::STATUS_OK) {
$error = \is_string($body) && $bodyData === null ? $body : 'no reason given';
$this->log->warning('Could not send notification to push server [{url}]: {error}', [
'error' => $error,
'url' => $proxyServer,
'app' => 'notifications',
]);
continue;
}

$body = $response->getBody();
$bodyData = json_decode($body, true);
if ($status !== Http::STATUS_OK) {
$this->log->error('Could not send notification to push server [{url}]: {error}',[
'error' => \is_string($body) && $bodyData === null ? $body : 'no reason given',
} else {
$error = \is_string($body) && $bodyData === null ? $body : 'no reason given';
$this->log->info('Push notification sent but response was not parsable, using an outdated push proxy? [{url}]: {error}', [
'error' => $error,
'url' => $proxyServer,
'app' => 'notifications',
]);
}

if (is_array($bodyData) && !empty($bodyData['unknown']) && is_array($bodyData['unknown'])) {
foreach ($bodyData['unknown'] as $unknownDevice) {
$this->deletePushTokenByDeviceIdentifier($unknownDevice);
}
}
}
}

Expand Down
11 changes: 5 additions & 6 deletions tests/Unit/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,9 @@ public function testPushToDeviceSending($isDebug) {
],
]);

$this->config->expects($this->exactly(2))
$this->config->expects($this->exactly(1))
->method('getSystemValue')
->willReturnMap([
['debug', false, $isDebug],
['force_language', false, false],
]);

Expand Down Expand Up @@ -490,7 +489,7 @@ public function testPushToDeviceSending($isDebug) {
->method('logException')
->with($e, [
'app' => 'notifications',
'level' => ILogger::WARN,
'level' => ILogger::ERROR,
]);

/** @var IResponse|\PHPUnit_Framework_MockObject_MockObject $response1 */
Expand All @@ -511,7 +510,7 @@ public function testPushToDeviceSending($isDebug) {
->willReturn($response1);

$this->logger->expects($this->at(1))
->method('error')
->method('warning')
->with('Could not send notification to push server [{url}]: {error}', [
'error' => 'no reason given',
'url' => 'badrequest',
Expand All @@ -535,8 +534,8 @@ public function testPushToDeviceSending($isDebug) {
])
->willReturn($response2);

$this->logger->expects($isDebug ? $this->at(2) : $this->never())
->method('debug')
$this->logger->expects($this->at(2))
->method('warning')
->with('Could not send notification to push server [{url}]: {error}', [
'error' => 'Maintenance',
'url' => 'unavailable',
Expand Down