From 066d89873a28257fad27868033db7868bd7e0c88 Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Wed, 12 Jun 2024 14:24:27 +0200 Subject: [PATCH] added webhook transport (probably only for debugging) --- lib/PushTransports/WebPushTransport.php | 11 ++++ lib/PushTransports/WebhookTransport.php | 68 +++++++++++++++++++++++++ lib/Transport/TransportManager.php | 2 + 3 files changed, 81 insertions(+) create mode 100644 lib/PushTransports/WebhookTransport.php diff --git a/lib/PushTransports/WebPushTransport.php b/lib/PushTransports/WebPushTransport.php index 39c9ebd..0eaa3f3 100644 --- a/lib/PushTransports/WebPushTransport.php +++ b/lib/PushTransports/WebPushTransport.php @@ -30,4 +30,15 @@ use OCA\DavPush\Transport\Transport; class WebPushTransport extends Transport { protected $id = "web-push"; + + public function registerSubscription($options) { + return [ + 'success' => True, + 'response' => "web push test", + ]; + } + + public function notify(string $userId, string $collectionName, $data) { + + } } \ No newline at end of file diff --git a/lib/PushTransports/WebhookTransport.php b/lib/PushTransports/WebhookTransport.php new file mode 100644 index 0000000..c6d74b7 --- /dev/null +++ b/lib/PushTransports/WebhookTransport.php @@ -0,0 +1,68 @@ + + * + * @author Jonathan Treffler + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\DavPush\PushTransports; + +use OCA\DavPush\Transport\Transport; + +class WebhookTransport extends Transport { + protected $id = "webhook"; + + public function registerSubscription($options) { + $url = False; + + foreach($options as $option) { + if($option["name"] == "{DAV:Push}endpoint") { + $url = $option["value"]; + } + } + + if($url) { + return [ + 'success' => True, + 'response' => "", + 'data' => [ "url" => $url ], + ]; + } else { + return [ + 'success' => False, + 'error' => "webhook url not provided", + ]; + } + } + + public function notify(string $userId, string $collectionName, $data) { + $options = [ + 'http' => [ + 'method' => 'POST', + 'content' => "Collection " . $collectionName . "has been changed", + ], + ]; + + $context = stream_context_create($options); + $result = file_get_contents($data["url"], false, $context); + } +} \ No newline at end of file diff --git a/lib/Transport/TransportManager.php b/lib/Transport/TransportManager.php index 95283d7..712322e 100644 --- a/lib/Transport/TransportManager.php +++ b/lib/Transport/TransportManager.php @@ -30,6 +30,7 @@ use OCP\EventDispatcher\IEventDispatcher; use OCA\DavPush\Events\RegisterTransportsEvent; use OCA\DavPush\PushTransports\WebPushTransport; +use OCA\DavPush\PushTransports\WebhookTransport; class TransportManager { private array $transports = []; @@ -37,6 +38,7 @@ class TransportManager { public function __construct(IEventDispatcher $dispatcher) { // register integrated transports $this->registerTransport(new WebPushTransport()); + $this->registerTransport(new WebhookTransport()); // register transports provided by other apps $event = new RegisterTransportsEvent($this);