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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ nbproject
/tests/Unit/coverage*
/tests/Unit/clover.xml
/tests/Unit/js/node_modules
/tests/Unit/.phpunit.result.cache

/tests/Integration/output
/tests/Integration/vendor
Expand Down
78 changes: 34 additions & 44 deletions lib/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function pushToDevice(int $id, INotification $notification): void {
$userKey = $this->keyManager->getKey($user);

$isTalkNotification = \in_array($notification->getApp(), ['spreed', 'talk'], true);
$talkApps = array_filter($devices, function($device) {
$talkApps = array_filter($devices, function ($device) {
return $device['apptype'] === 'talk';
});
$hasTalkApps = !empty($talkApps);
Expand Down Expand Up @@ -131,43 +131,7 @@ public function pushToDevice(int $id, INotification $notification): void {
}
}

if (empty($pushNotifications)) {
return;
}

$client = $this->clientService->newClient();
foreach ($pushNotifications as $proxyServer => $notifications) {
try {
$response = $client->post($proxyServer . '/notifications', [
'body' => [
'notifications' => $notifications,
],
]);
} catch (\Exception $e) {
$this->log->logException($e, [
'app' => 'notifications',
'level' => $e->getCode() === Http::STATUS_BAD_REQUEST ? ILogger::INFO : ILogger::WARN,
]);
continue;
}

$status = $response->getStatusCode();
if ($status !== Http::STATUS_OK && $status !== Http::STATUS_SERVICE_UNAVAILABLE) {
$body = $response->getBody();
$this->log->error('Could not send notification to push server [{url}]: {error}',[
'error' => \is_string($body) ? $body : 'no reason given',
'url' => $proxyServer,
'app' => 'notifications',
]);
} else 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',
'url' => $proxyServer,
'app' => 'notifications',
]);
}
}
$this->sendNotificationsToProxies($pushNotifications);
}

public function pushDeleteToDevice(string $userId, int $notificationId): void {
Expand Down Expand Up @@ -201,6 +165,10 @@ public function pushDeleteToDevice(string $userId, int $notificationId): void {
}
}

$this->sendNotificationsToProxies($pushNotifications);
}

protected function sendNotificationsToProxies(array $pushNotifications): void {
if (empty($pushNotifications)) {
return;
}
Expand All @@ -222,21 +190,31 @@ public function pushDeleteToDevice(string $userId, int $notificationId): void {
}

$status = $response->getStatusCode();
if ($status !== Http::STATUS_OK && $status !== Http::STATUS_SERVICE_UNAVAILABLE) {
if ($status === Http::STATUS_SERVICE_UNAVAILABLE && $this->config->getSystemValue('debug', false)) {
$body = $response->getBody();
$this->log->error('Could not send notification to push server [{url}]: {error}',[
$this->log->debug('Could not send notification to push server [{url}]: {error}',[
'error' => \is_string($body) ? $body : 'no reason given',
'url' => $proxyServer,
'app' => 'notifications',
]);
} else 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',
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',
'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 Expand Up @@ -388,4 +366,16 @@ protected function deletePushToken(int $tokenId): bool {

return $query->execute() !== 0;
}

/**
* @param string $deviceIdentifier
* @return bool
*/
protected function deletePushTokenByDeviceIdentifier(string $deviceIdentifier): bool {
$query = $this->db->getQueryBuilder();
$query->delete('notifications_pushtokens')
->where($query->expr()->eq('deviceidentifier', $query->createNamedParameter($deviceIdentifier)));

return $query->execute() !== 0;
}
}
4 changes: 2 additions & 2 deletions tests/Unit/AppInfo/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
class RoutesTest extends TestCase {
public function testRoutes() {
$routes = include __DIR__ . '/../../../appinfo/routes.php';
$this->assertInternalType('array', $routes);
$this->assertIsArray($routes);
$this->assertCount(1, $routes);
$this->assertArrayHasKey('ocs', $routes);
$this->assertInternalType('array', $routes['ocs']);
$this->assertIsArray($routes['ocs']);
$this->assertCount(7, $routes['ocs']);
}
}
8 changes: 4 additions & 4 deletions tests/Unit/Controller/EndpointControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,15 @@ public function testDeleteAllNotifications($_, $username) {

public function dataNotificationToArray() {
return [
['v1', 42, 'app1', 'user1', 1234, 'type1', 42, 'subject1', '', [], 'message1', 'richMessage 1', ['richMessage param'], 'link1', 'icon1', [], []],
['v1', 1337, 'app2', 'user2', 1337, 'type2', 21, 'subject2', 'richSubject 2', ['richSubject param'], 'message2', '', [], 'link2', 'icon2', [
['v1', 42, 'app1', 'user1', 1234, 'type1', '42', 'subject1', '', [], 'message1', 'richMessage 1', ['richMessage param'], 'link1', 'icon1', [], []],
['v1', 1337, 'app2', 'user2', 1337, 'type2', '21', 'subject2', 'richSubject 2', ['richSubject param'], 'message2', '', [], 'link2', 'icon2', [
$this->getMockBuilder(IAction::class)
->getMock(),
$this->getMockBuilder(IAction::class)
->getMock(),
], [['action'], ['action']]],
['v2', 42, 'app1', 'user1', 1234, 'type1', 42, 'subject1', '', [], 'message1', 'richMessage 1', ['richMessage param'], 'link1', 'icon1', [], []],
['v2', 1337, 'app2', 'user2', 1337, 'type2', 21, 'subject2', 'richSubject 2', ['richSubject param'], 'message2', '', [], 'link2', 'icon2', [
['v2', 42, 'app1', 'user1', 1234, 'type1', '42', 'subject1', '', [], 'message1', 'richMessage 1', ['richMessage param'], 'link1', 'icon1', [], []],
['v2', 1337, 'app2', 'user2', 1337, 'type2', '21', 'subject2', 'richSubject 2', ['richSubject param'], 'message2', '', [], 'link2', 'icon2', [
$this->getMockBuilder(IAction::class)
->getMock(),
$this->getMockBuilder(IAction::class)
Expand Down
12 changes: 4 additions & 8 deletions tests/Unit/Notifier/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ protected function setUp(): void {
);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unknown app
*/
public function testPrepareWrongApp() {
/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
$notification = $this->createMock(INotification::class);
Expand All @@ -75,13 +71,11 @@ public function testPrepareWrongApp() {
$notification->expects($this->never())
->method('getSubject');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown app');
$this->notifier->prepare($notification, 'en');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Unknown subject
*/
public function testPrepareWrongSubject() {
/** @var INotification|\PHPUnit_Framework_MockObject_MockObject $notification */
$notification = $this->createMock(INotification::class);
Expand All @@ -93,6 +87,8 @@ public function testPrepareWrongSubject() {
->method('getSubject')
->willReturn('wrong subject');

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown subject');
$this->notifier->prepare($notification, 'en');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ public function testPushToDeviceSending($isDebug) {
$response2->expects($this->once())
->method('getStatusCode')
->willReturn(Http::STATUS_SERVICE_UNAVAILABLE);
$response2->expects($isDebug ? $this->once() : $this->never())
$response2->expects($this->once())
->method('getBody')
->willReturn('Maintenance');
$client->expects($this->at(2))
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="bootstrap.php"
strict="true"
verbose="true"
timeoutForSmallTests="900"
timeoutForMediumTests="900"
Expand Down