Skip to content

Commit

Permalink
Adapt to new namespace and property names / structure (#34)
Browse files Browse the repository at this point in the history
* Unify new Push namespace and property names

* Handle new property names / namespace

* Update Push message to spec

* minor code style updates

---------

Co-authored-by: Jonathan Treffler <[email protected]>
  • Loading branch information
rfc2822 and JonathanTreffler authored Jan 13, 2025
1 parent c5e9704 commit 9e880fd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 19 deletions.
35 changes: 35 additions & 0 deletions lib/Dav/PushSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/**
* @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/>.
*
*/

namespace OCA\DavPush\Dav;

abstract class PushSpec {

public const PUSH_PREFIX = '{https://bitfire.at/webdav-push}';

public const PUSH_MESSAGE = self::PUSH_PREFIX . 'push-message';

public const PROPERTY_PUSH_RESOURCE = self::PUSH_PREFIX . 'push-resource';
public const PROPERTY_TOPIC = self::PUSH_PREFIX . 'topic';
public const PROPERTY_TRANSPORTS = self::PUSH_PREFIX . 'transports';

}
18 changes: 6 additions & 12 deletions lib/Dav/ServiceDetectionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace OCA\DavPush\Dav;

use OCA\DavPush\Dav\PushSpec;
use OCA\DavPush\Transport\TransportManager;

use OCP\IUser;
Expand All @@ -42,11 +43,6 @@

class ServiceDetectionPlugin extends ServerPlugin {

public const PUSH_PREFIX = '{DAV:Push}';
public const PROPERTY_PUSH_TRANSPORTS = self::PUSH_PREFIX . 'push-transports';
public const PROPERTY_PUSH_TOPIC = self::PUSH_PREFIX . 'topic';


public function __construct(
private IUserSession $userSession,
private TransportManager $transportManager,
Expand All @@ -58,7 +54,7 @@ public function initialize(Server $server): void {
}

public function propFind(PropFind $propFind, INode $node) {
if (count(array_intersect([self::PROPERTY_PUSH_TRANSPORTS, self::PROPERTY_PUSH_TOPIC], $propFind->getRequestedProperties())) == 0) {
if (count(array_intersect([PushSpec::PROPERTY_TRANSPORTS, PushSpec::PROPERTY_TOPIC], $propFind->getRequestedProperties())) == 0) {
return;
}

Expand All @@ -67,7 +63,7 @@ public function propFind(PropFind $propFind, INode $node) {
}

$propFind->handle(
self::PROPERTY_PUSH_TRANSPORTS,
PushSpec::PROPERTY_TRANSPORTS,
function () use ($node) {
//$user = $this->userSession->getUser();
//if (!($user instanceof IUser)) {
Expand All @@ -80,9 +76,7 @@ function () use ($node) {

foreach($transports as $transport) {
$result[] = [
(self::PUSH_PREFIX . "transport") => [
(self::PUSH_PREFIX . $transport->getId()) => $transport->getAdditionalInformation(),
]
(PushSpec::PUSH_PREFIX . $transport->getId()) => $transport->getAdditionalInformation(),
];
}

Expand All @@ -93,7 +87,7 @@ function () use ($node) {
);

$propFind->handle(
self::PROPERTY_PUSH_TOPIC,
PushSpec::PROPERTY_TOPIC,
//function () use ($node) {
//$user = $this->userSession->getUser();
//if (!($user instanceof IUser)) {
Expand All @@ -105,4 +99,4 @@ function () use ($node) {
$node->getName()
);
}
}
}
5 changes: 3 additions & 2 deletions lib/Dav/SubscriptionManagementPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

namespace OCA\DavPush\Dav;

use OCA\DavPush\Dav\PushSpec;
use OCA\DavPush\Transport\TransportManager;
use OCA\DavPush\Db\Subscription;
use OCA\DavPush\Service\SubscriptionService;
Expand Down Expand Up @@ -103,7 +104,7 @@ private function parsePushRegisterElement(array $subElements) {
if(sizeof($errors) === 0) {
return [
"errors" => [],
"subscriptionType" => $parsedSubscriptionElement["type"],
"subscriptionType" => str_replace(PushSpec::PUSH_PREFIX, "", $parsedSubscriptionElement["type"]),
"subscriptionOptions" => $parsedSubscriptionElement["options"],
"requestedSubscriptionExpiration" => $parsedExpiresElement,
];
Expand All @@ -122,7 +123,7 @@ private function parseSubscriptionElement(array $subElements): array {
if(sizeof($subElements) == 1) {
// parse child element
$type = $subElements[0]["name"];
$type = preg_replace('/^\{DAV:Push\}/', '', $type);
$type = str_replace('{' . PushSpec::PUSH_PREFIX . '}', '', $type);
$type = preg_replace('/-subscription$/', '', $type);

$options = $subElements[0]["value"];
Expand Down
11 changes: 8 additions & 3 deletions lib/PushTransports/WebPushTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OCA\DavPush\PushTransports;

use OCA\DavPush\Dav\PushSpec;
use OCA\DavPush\Transport\Transport;
use OCA\DavPush\Service\WebPushSubscriptionService;
use OCA\DavPush\Errors\WebPushSubscriptionNotFound;
Expand All @@ -43,7 +44,7 @@ private function parseOptions(array $options): array {
$result = [];

foreach($options as $option) {
if ($option["name"] == "{DAV:Push}push-resource") {
if ($option["name"] == PushSpec::PROPERTY_PUSH_RESOURCE) {
$result["pushResource"] = $option["value"];
}
}
Expand Down Expand Up @@ -85,8 +86,12 @@ public function notify(string $userId, string $collectionName, int $subscription

$pushResource = $this->webPushSubscriptionService->findBySubscriptionId($subscriptionId)->getPushResource();

$content = $xmlService->write('{DAV:Push}push-message', [
'{DAV:Push}topic' => $collectionName,
$content = $xmlService->write(PushSpec::PUSH_MESSAGE, [
'{DAV:}propstat' => [
'{DAV:}prop' => [
PushSpec::PROPERTY_TOPIC => $collectionName
]
],
]);

$options = [
Expand Down
5 changes: 3 additions & 2 deletions lib/PushTransports/WebhookTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OCA\DavPush\PushTransports;

use OCA\DavPush\Dav\PushSpec;
use OCA\DavPush\Transport\Transport;

class WebhookTransport extends Transport {
Expand All @@ -35,7 +36,7 @@ public function registerSubscription($options) {
$url = False;

foreach($options as $option) {
if($option["name"] == "{DAV:Push}endpoint") {
if($option["name"] == "{" . PushSpec::PUSH_PREFIX . "}endpoint") {
$url = $option["value"];
}
}
Expand Down Expand Up @@ -65,4 +66,4 @@ public function notify(string $userId, string $collectionName, $data) {
$context = stream_context_create($options);
$result = file_get_contents($data["url"], false, $context);
}
}
}

0 comments on commit 9e880fd

Please sign in to comment.