From 71ecda7b63c433d6a5d0b27951939b3e4583f36c Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Tue, 23 Jul 2024 02:16:55 +0200 Subject: [PATCH] adapt web push transport to transport provider api changes --- lib/PushTransports/WebPushTransport.php | 51 ++++++++++++++++++++----- 1 file changed, 42 insertions(+), 9 deletions(-) diff --git a/lib/PushTransports/WebPushTransport.php b/lib/PushTransports/WebPushTransport.php index 499b8d0..b0e9dad 100644 --- a/lib/PushTransports/WebPushTransport.php +++ b/lib/PushTransports/WebPushTransport.php @@ -27,34 +27,57 @@ declare(strict_types=1); namespace OCA\DavPush\PushTransports; use OCA\DavPush\Transport\Transport; +use OCA\DavPush\Service\WebPushSubscriptionService; + use Sabre\Xml\Service; class WebPushTransport extends Transport { protected $id = "web-push"; - public function registerSubscription($options) { - $pushResource = False; + public function __construct( + private WebPushSubscriptionService $webPushSubscriptionService, + ) {} + + private function parseOptions(array $options): array { + $result = []; foreach($options as $option) { if ($option["name"] == "{DAV:Push}push-resource") { - $pushResource = $option["value"]; + $result["pushResource"] = $option["value"]; } } - if($pushResource) { + return $result; + } + + public function validateOptions($options): array { + ['pushResource' => $pushResource] = $this->parseOptions($options); + + // TODO: check if string is valid URL + + if(isset($pushResource) && $pushResource !== '') { return [ - 'success' => True, - 'response' => "", - 'data' => [ "pushResource" => $pushResource ], + 'valid' => True, ]; } else { return [ - 'success' => False, - 'error' => "push resource not provided", + 'valid' => False, + 'errors' => ["push resource not provided"] ]; } } + public function registerSubscription($subsciptionId, $options) { + ['pushResource' => $pushResource] = $this->parseOptions($options); + + $this->webPushSubscriptionService->create($subsciptionId, $pushResource); + + return [ + 'success' => True, + 'response' => "", + ]; + } + public function notify(string $userId, string $collectionName, $data) { $xmlService = new Service(); @@ -72,4 +95,14 @@ class WebPushTransport extends Transport { $context = stream_context_create($options); $result = file_get_contents($data["pushResource"], false, $context); } + + public function getSubscriptionIdFromOptions($options): int { + ['pushResource' => $pushResource] = $this->parseOptions($options); + + return $this->webPushSubscriptionService->findByPushResource($pushResource)->getSubscriptionId(); + } + + public function updateSubscription($subsciptionId, $options) { + // there are no options which can be edited -> NOOP + } }