-
Notifications
You must be signed in to change notification settings - Fork 63
Back/webpush #2662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Back/webpush #2662
Changes from 1 commit
2efd214
8b87232
a5b09aa
d9ffa95
ee564f7
80c7390
094eccb
edb1d58
fb3c960
942c3d4
9481d4a
1423ea8
20a9090
0621e1b
1b5cf32
429b50f
4a69214
676fb5c
0c0f2b2
0aa109c
e0c6bb0
aec0a1b
424adbc
6363888
1182172
0027d1b
dc2fb55
4ef6030
c3df6e4
5fa1b47
294025e
e2c0181
180cf92
de4a2bf
1c94e08
c6b6bc8
c57b446
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Signed-off-by: sim <[email protected]>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| use OC\Security\IdentityProof\Key; | ||
| use OC\Security\IdentityProof\Manager; | ||
| use OCA\Notifications\AppInfo\Application; | ||
| use OCA\Notifications\Vendor\Minishlink\WebPush\MessageSentReport; | ||
| use OCP\AppFramework\Http; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\Authentication\Exceptions\InvalidTokenException; | ||
|
|
@@ -175,7 +176,7 @@ public function flushPayloads(): void { | |
| } | ||
|
|
||
| $this->deferPayloads = false; | ||
| $this->wpClient->flush(); | ||
| $this->wpClient->flush(fn ($r) => $this->webPushCallback($r)); | ||
| $this->sendNotificationsToProxies(); | ||
| } | ||
|
|
||
|
|
@@ -329,6 +330,7 @@ public function webPushToDevice(int $id, IUser $user, array $devices, INotificat | |
|
|
||
| if (!$this->validateToken($device['token'], $maxAge)) { | ||
| // Token does not exist anymore | ||
| $this->deleteWebPushToken($device['token']); | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -339,13 +341,13 @@ public function webPushToDevice(int $id, IUser $user, array $devices, INotificat | |
| $this->log->error('JSON error while encoding push notification: ' . $e->getMessage(), ['exception' => $e]); | ||
| } catch (\InvalidArgumentException) { | ||
| // Failed to encrypt message for device: public key is invalid | ||
| //TODO $this->deletePushToken($device['token']); | ||
| $this->deleteWebPushToken($device['token']); | ||
| } | ||
| } | ||
| $this->printInfo(''); | ||
|
|
||
| if (!$this->deferPayloads) { | ||
| $this->wpClient->flush(); | ||
| $this->wpClient->flush(fn ($r) => $this->webPushCallback($r)); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -381,6 +383,7 @@ public function proxyPushToDevice(int $id, IUser $user, array $devices, INotific | |
|
|
||
| if (!$this->validateToken($device['token'], $maxAge)) { | ||
| // Token does not exist anymore | ||
| $this->deleteProxyPushToken($device['token']); | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -396,7 +399,7 @@ public function proxyPushToDevice(int $id, IUser $user, array $devices, INotific | |
| $this->log->error('JSON error while encoding push notification: ' . $e->getMessage(), ['exception' => $e]); | ||
| } catch (\InvalidArgumentException) { | ||
| // Failed to encrypt message for device: public key is invalid | ||
| $this->deletePushToken($device['token']); | ||
| $this->deleteProxyPushToken($device['token']); | ||
| } | ||
| } | ||
| $this->printInfo(''); | ||
|
|
@@ -501,6 +504,7 @@ public function proxyPushDeleteToDevice(string $userId, IUser $user, array $devi | |
| $device['token'] = (int)$device['token']; | ||
| if (!$this->validateToken($device['token'], $maxAge)) { | ||
| // Token does not exist anymore | ||
| $this->deleteProxyPushToken($device['token']); | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -532,7 +536,7 @@ public function proxyPushDeleteToDevice(string $userId, IUser $user, array $devi | |
| } | ||
| } catch (\InvalidArgumentException) { | ||
| // Failed to encrypt message for device: public key is invalid | ||
| $this->deletePushToken($device['token']); | ||
| $this->deleteProxyPushToken($device['token']); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -541,6 +545,15 @@ public function proxyPushDeleteToDevice(string $userId, IUser $user, array $devi | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Delete expired web push subscriptions | ||
| */ | ||
| protected function webPushCallback (MessageSentReport $report): void { | ||
| if ($report->isSubscriptionExpired()) { | ||
| $this->deleteWebPushTokenByEndpoint($report->getEndpoint()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This permanently removes all the device entries, even if the subscription is continued in the future? |
||
| } | ||
| } | ||
|
|
||
| protected function sendNotificationsToProxies(): void { | ||
| $pushNotifications = $this->payloadsToSend; | ||
| $this->payloadsToSend = []; | ||
|
|
@@ -628,7 +641,7 @@ protected function sendNotificationsToProxies(): void { | |
| // Proxy returns null when the array is empty | ||
| foreach ($bodyData['unknown'] as $unknownDevice) { | ||
| $this->printInfo('<comment>Deleting device because it is unknown by the push server: ' . $unknownDevice . '</comment>'); | ||
| $this->deletePushTokenByDeviceIdentifier($unknownDevice); | ||
| $this->deleteProxyPushTokenByDeviceIdentifier($unknownDevice); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -671,7 +684,6 @@ protected function validateToken(int $tokenId, int $maxAge): bool { | |
| if ($type === IToken::WIPE_TOKEN) { | ||
| // Token does not exist any more, should drop the push device entry | ||
| $this->printInfo('Device token is marked for remote wipe'); | ||
| $this->deletePushToken($tokenId); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be kept with |
||
| $this->cache->set('t' . $tokenId, 0, 600); | ||
| return false; | ||
| } | ||
|
|
@@ -685,7 +697,6 @@ protected function validateToken(int $tokenId, int $maxAge): bool { | |
| } catch (InvalidTokenException) { | ||
| // Token does not exist any more, should drop the push device entry | ||
| $this->printInfo('<error>InvalidTokenException is thrown</error>'); | ||
| $this->deletePushToken($tokenId); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be kept with |
||
| $this->cache->set('t' . $tokenId, 0, 600); | ||
| return false; | ||
| } | ||
|
|
@@ -939,7 +950,31 @@ protected function getWebPushDevicesForUsers(array $userIds): array { | |
| * @param int $tokenId | ||
| * @return bool | ||
| */ | ||
| protected function deletePushToken(int $tokenId): bool { | ||
| protected function deleteWebPushToken(int $tokenId): bool { | ||
| $query = $this->db->getQueryBuilder(); | ||
| $query->delete('notifications_webpush') | ||
| ->where($query->expr()->eq('token', $query->createNamedParameter($tokenId, IQueryBuilder::PARAM_INT))); | ||
|
|
||
| return $query->executeStatement() !== 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $endpoint | ||
| * @return bool | ||
| */ | ||
| protected function deleteWebPushTokenByEndpoint(string $endpoint): bool { | ||
| $query = $this->db->getQueryBuilder(); | ||
| $query->delete('notifications_webpush') | ||
| ->where($query->expr()->eq('endpoint', $query->createNamedParameter($endpoint))); | ||
|
|
||
| return $query->executeStatement() !== 0; | ||
| } | ||
|
|
||
| /** | ||
| * @param int $tokenId | ||
| * @return bool | ||
| */ | ||
| protected function deleteProxyPushToken(int $tokenId): bool { | ||
| $query = $this->db->getQueryBuilder(); | ||
| $query->delete('notifications_pushhash') | ||
| ->where($query->expr()->eq('token', $query->createNamedParameter($tokenId, IQueryBuilder::PARAM_INT))); | ||
|
|
@@ -951,7 +986,7 @@ protected function deletePushToken(int $tokenId): bool { | |
| * @param string $deviceIdentifier | ||
| * @return bool | ||
| */ | ||
| protected function deletePushTokenByDeviceIdentifier(string $deviceIdentifier): bool { | ||
| protected function deleteProxyPushTokenByDeviceIdentifier(string $deviceIdentifier): bool { | ||
| $query = $this->db->getQueryBuilder(); | ||
| $query->delete('notifications_pushhash') | ||
| ->where($query->expr()->eq('deviceidentifier', $query->createNamedParameter($deviceIdentifier))); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the device was inactive for 60 days we shouldn't delete it directly.
It's possible that the device/user is coming back online.
The problem is the comment is misleading by now.