2024-04-05 20:26:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-05-03 18:08:19 +02:00
|
|
|
/**
|
|
|
|
* @copyright 2024 Jonathan Treffler <mail@jonathan-treffler.de>
|
|
|
|
*
|
|
|
|
* @author Jonathan Treffler <mail@jonathan-treffler.de>
|
|
|
|
*
|
|
|
|
* @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 <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2024-04-05 20:26:15 +02:00
|
|
|
namespace OCA\DavPush\PushTransports;
|
|
|
|
|
|
|
|
use OCA\DavPush\Transport\Transport;
|
2024-07-23 02:16:55 +02:00
|
|
|
use OCA\DavPush\Service\WebPushSubscriptionService;
|
2024-07-24 14:19:24 +02:00
|
|
|
use OCA\DavPush\Errors\WebPushSubscriptionNotFound;
|
2024-07-23 02:16:55 +02:00
|
|
|
|
2024-06-17 15:23:21 +02:00
|
|
|
use Sabre\Xml\Service;
|
2024-04-05 20:26:15 +02:00
|
|
|
|
|
|
|
class WebPushTransport extends Transport {
|
2024-04-05 20:30:30 +02:00
|
|
|
protected $id = "web-push";
|
2024-06-12 14:24:27 +02:00
|
|
|
|
2024-07-23 02:16:55 +02:00
|
|
|
public function __construct(
|
|
|
|
private WebPushSubscriptionService $webPushSubscriptionService,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
private function parseOptions(array $options): array {
|
|
|
|
$result = [];
|
2024-06-17 15:23:21 +02:00
|
|
|
|
|
|
|
foreach($options as $option) {
|
|
|
|
if ($option["name"] == "{DAV:Push}push-resource") {
|
2024-07-23 02:16:55 +02:00
|
|
|
$result["pushResource"] = $option["value"];
|
2024-06-17 15:23:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-23 02:16:55 +02:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function validateOptions($options): array {
|
|
|
|
['pushResource' => $pushResource] = $this->parseOptions($options);
|
|
|
|
|
|
|
|
// TODO: check if string is valid URL
|
|
|
|
|
|
|
|
if(isset($pushResource) && $pushResource !== '') {
|
2024-06-17 15:23:21 +02:00
|
|
|
return [
|
2024-07-24 13:52:09 +02:00
|
|
|
'success' => True,
|
2024-06-17 15:23:21 +02:00
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return [
|
2024-07-24 13:52:09 +02:00
|
|
|
'success' => False,
|
2024-07-23 02:16:55 +02:00
|
|
|
'errors' => ["push resource not provided"]
|
2024-06-17 15:23:21 +02:00
|
|
|
];
|
|
|
|
}
|
2024-06-12 14:24:27 +02:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:16:55 +02:00
|
|
|
public function registerSubscription($subsciptionId, $options) {
|
|
|
|
['pushResource' => $pushResource] = $this->parseOptions($options);
|
|
|
|
|
|
|
|
$this->webPushSubscriptionService->create($subsciptionId, $pushResource);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'success' => True,
|
|
|
|
'response' => "",
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-07-24 14:36:24 +02:00
|
|
|
public function notify(string $userId, string $collectionName, int $subscriptionId) {
|
2024-06-17 15:23:21 +02:00
|
|
|
$xmlService = new Service();
|
|
|
|
|
2024-07-24 14:36:24 +02:00
|
|
|
$pushResource = $this->webPushSubscriptionService->findBySubscriptionId($subscriptionId)->getPushResource();
|
|
|
|
|
2024-06-17 15:23:21 +02:00
|
|
|
$content = $xmlService->write('{DAV:Push}push-message', [
|
2024-07-01 22:28:31 +02:00
|
|
|
'{DAV:Push}topic' => $collectionName,
|
2024-06-17 15:23:21 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$options = [
|
|
|
|
'http' => [
|
|
|
|
'method' => 'POST',
|
|
|
|
'content' => $content,
|
|
|
|
],
|
|
|
|
];
|
2024-06-12 14:24:27 +02:00
|
|
|
|
2024-06-17 15:23:21 +02:00
|
|
|
$context = stream_context_create($options);
|
2024-07-24 14:36:24 +02:00
|
|
|
$result = file_get_contents($pushResource, false, $context);
|
2024-06-12 14:24:27 +02:00
|
|
|
}
|
2024-07-23 02:16:55 +02:00
|
|
|
|
2024-07-24 14:19:24 +02:00
|
|
|
public function getSubscriptionIdFromOptions($options): ?int {
|
2024-07-23 02:16:55 +02:00
|
|
|
['pushResource' => $pushResource] = $this->parseOptions($options);
|
|
|
|
|
2024-07-24 14:19:24 +02:00
|
|
|
try {
|
|
|
|
return $this->webPushSubscriptionService->findByPushResource($pushResource)->getSubscriptionId();
|
|
|
|
} catch (WebPushSubscriptionNotFound $e) {
|
|
|
|
return null;
|
|
|
|
}
|
2024-07-23 02:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function updateSubscription($subsciptionId, $options) {
|
|
|
|
// there are no options which can be edited -> NOOP
|
2024-07-24 13:52:09 +02:00
|
|
|
return [
|
|
|
|
'success' => True,
|
2024-07-24 14:23:16 +02:00
|
|
|
'response' => "",
|
2024-07-24 13:52:09 +02:00
|
|
|
];
|
2024-07-23 02:16:55 +02:00
|
|
|
}
|
2024-06-17 15:23:21 +02:00
|
|
|
}
|