diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2018-08-07 14:52:52 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-07 14:52:52 +0300 |
commit | be2bfb102805d2713b177969eb83ed8c0695cce3 (patch) | |
tree | a623c68ba008962985cfffa9299a54b484ed988b | |
parent | d6e3f9006b2d9f5c3e38cf286b5d641a63e90f2e (diff) | |
parent | 862bcbbae23d951365ea48eff0e6a9cc31b4e611 (diff) |
Merge pull request #150 from nextcloud/feature/149/add-notification-id-to-push
Add the notification ID to the push to allow getting more details
-rw-r--r-- | docs/push-v2.md | 27 | ||||
-rw-r--r-- | lib/App.php | 2 | ||||
-rw-r--r-- | lib/Capabilities.php | 1 | ||||
-rw-r--r-- | lib/Push.php | 11 | ||||
-rw-r--r-- | tests/Unit/AppTest.php | 2 | ||||
-rw-r--r-- | tests/Unit/CapabilitiesTest.php | 1 | ||||
-rw-r--r-- | tests/Unit/PushTest.php | 16 |
7 files changed, 43 insertions, 17 deletions
diff --git a/docs/push-v2.md b/docs/push-v2.md index 84aed3e..3facf4c 100644 --- a/docs/push-v2.md +++ b/docs/push-v2.md @@ -17,7 +17,8 @@ In order to find out if notifications support push on the server you can run a r "notifications": { "push": [ ... - "devices" + "devices", + "object-data" ] } } @@ -208,6 +209,30 @@ The pushed notifications is defined by the [Firebase Cloud Messaging HTTP Protoc | `subject` | The subject is encrypted with the device´s *public key*. | | `signature` | The signature is a sha512 signature over the encrypted subject using the user´s private key. | +### Encrypted subject data + +If you are missing any information necessary to parse the notification in a more usable way, use the `nid` to get the full notification information via [OCS API](ocs-endpoint-v2.md) + +```json +{ + "app" : "spreed", + "subject" : "Test mentioned you in a private conversation", + "type" : "chat", + "id" : "t0k3n", + "nid" : 1337 + } +} +``` + +| Attribute | Meaning | Capability | +| ----------- | ---------------------------------------- |------------| +| `app` | The nextcloud app sending the notification | -| +| `subject` | The subject of the actual notification | -| +| `type` | Type of the object this notification is about | `object-data` | +| `id` | Identifier of the object this notification is about | `object-data` | +| `nid` | Numeric identifier of the notification in order to get more information via the [OCS API](ocs-endpoint-v2.md) | `object-data` | + + ### Verification So a device should verify the signature using the user´s public key. If the signature is okay, the subject can be decrypted using the device´s private key. diff --git a/lib/App.php b/lib/App.php index c1b966b..6be3dde 100644 --- a/lib/App.php +++ b/lib/App.php @@ -47,7 +47,7 @@ class App implements IApp { try { $notificationToPush = $this->handler->getById($notificationId, $notification->getUser()); - $this->push->pushToDevice($notificationToPush); + $this->push->pushToDevice($notificationId, $notificationToPush); } catch (NotificationNotFoundException $e) { throw new \InvalidArgumentException('Error while preparing push notification'); } diff --git a/lib/Capabilities.php b/lib/Capabilities.php index 127d498..be654c1 100644 --- a/lib/Capabilities.php +++ b/lib/Capabilities.php @@ -48,6 +48,7 @@ class Capabilities implements ICapability { ], 'push' => [ 'devices', + 'object-data', ], 'admin-notifications' => [ 'ocs', diff --git a/lib/Push.php b/lib/Push.php index 11b091f..beade47 100644 --- a/lib/Push.php +++ b/lib/Push.php @@ -66,10 +66,7 @@ class Push { $this->log = $log; } - /** - * @param INotification $notification - */ - public function pushToDevice(INotification $notification) { + public function pushToDevice(int $id, INotification $notification) { $user = $this->userManager->get($notification->getUser()); if (!($user instanceof IUser)) { return; @@ -118,7 +115,7 @@ class Push { } try { - $payload = json_encode($this->encryptAndSign($userKey, $device, $notification, $isTalkNotification)); + $payload = json_encode($this->encryptAndSign($userKey, $device, $id, $notification, $isTalkNotification)); $proxyServer = rtrim($device['proxyserver'], '/'); if (!isset($pushNotifications[$proxyServer])) { @@ -175,17 +172,19 @@ class Push { /** * @param Key $userKey * @param array $device + * @param int $id * @param INotification $notification * @param bool $isTalkNotification * @return array * @throws InvalidTokenException * @throws \InvalidArgumentException */ - protected function encryptAndSign(Key $userKey, array $device, INotification $notification, bool $isTalkNotification): array { + protected function encryptAndSign(Key $userKey, array $device, int $id, INotification $notification, bool $isTalkNotification): array { // Check if the token is still valid... $this->tokenProvider->getTokenById($device['token']); $data = [ + 'nid' => $id, 'app' => $notification->getApp(), 'subject' => $notification->getParsedSubject(), 'type' => $notification->getObjectType(), diff --git a/tests/Unit/AppTest.php b/tests/Unit/AppTest.php index 64e395f..f1e49f0 100644 --- a/tests/Unit/AppTest.php +++ b/tests/Unit/AppTest.php @@ -80,7 +80,7 @@ class AppTest extends TestCase { ->willReturn($this->notification); $this->push->expects($this->once()) ->method('pushToDevice') - ->with($this->notification); + ->with($id, $this->notification); $this->app->notify($this->notification); } diff --git a/tests/Unit/CapabilitiesTest.php b/tests/Unit/CapabilitiesTest.php index 27af8ab..8b4640f 100644 --- a/tests/Unit/CapabilitiesTest.php +++ b/tests/Unit/CapabilitiesTest.php @@ -41,6 +41,7 @@ class CapabilitiesTest extends TestCase { ], 'push' => [ 'devices', + 'object-data', ], 'admin-notifications' => [ 'ocs', diff --git a/tests/Unit/PushTest.php b/tests/Unit/PushTest.php index c545e6f..4b4862a 100644 --- a/tests/Unit/PushTest.php +++ b/tests/Unit/PushTest.php @@ -128,7 +128,7 @@ class PushTest extends TestCase { ->with('invalid') ->willReturn(null); - $push->pushToDevice($notification); + $push->pushToDevice(23, $notification); } public function testPushToDeviceNoDevices() { @@ -156,7 +156,7 @@ class PushTest extends TestCase { ->method('getDevicesForUser') ->willReturn([]); - $push->pushToDevice($notification); + $push->pushToDevice(42, $notification); } public function testPushToDeviceNotPrepared() { @@ -201,7 +201,7 @@ class PushTest extends TestCase { ->with($notification, 'de') ->willThrowException(new \InvalidArgumentException()); - $push->pushToDevice($notification); + $push->pushToDevice(1337, $notification); } public function testPushToDeviceInvalidToken() { @@ -262,7 +262,7 @@ class PushTest extends TestCase { ->method('deletePushToken') ->with(23); - $push->pushToDevice($notification); + $push->pushToDevice(2018, $notification); } public function testPushToDeviceEncryptionError() { @@ -321,7 +321,7 @@ class PushTest extends TestCase { ->method('deletePushToken') ->with(23); - $push->pushToDevice($notification); + $push->pushToDevice(1970, $notification); } public function dataPushToDeviceSending() { @@ -501,7 +501,7 @@ class PushTest extends TestCase { ]) ->willReturn($response3); - $push->pushToDevice($notification); + $push->pushToDevice(207787, $notification); } public function dataPushToDeviceTalkNotification() { @@ -593,7 +593,7 @@ class PushTest extends TestCase { } else { $push->expects($this->exactly(1)) ->method('encryptAndSign') - ->with($this->anything(), $devices[$pushedDevice], $this->anything(), $isTalkNotification) + ->with($this->anything(), $devices[$pushedDevice], $this->anything(), $this->anything(), $isTalkNotification) ->willReturn(['Payload']); /** @var IClient|\PHPUnit_Framework_MockObject_MockObject $client */ @@ -621,7 +621,7 @@ class PushTest extends TestCase { ->willReturn($response); } - $push->pushToDevice($notification); + $push->pushToDevice(200718, $notification); } |