Skip to content
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

DAV Push Sync #40464

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 3rdparty
2 changes: 1 addition & 1 deletion apps/dav/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<name>WebDAV</name>
<summary>WebDAV endpoint</summary>
<description>WebDAV endpoint</description>
<version>1.28.0</version>
<version>1.28.1</version>
<licence>agpl</licence>
<author>owncloud.org</author>
<namespace>DAV</namespace>
Expand Down
8 changes: 8 additions & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
'OCA\\DAV\\CalDAV\\PublicCalendarRoot' => $baseDir . '/../lib/CalDAV/PublicCalendarRoot.php',
'OCA\\DAV\\CalDAV\\Publishing\\PublishPlugin' => $baseDir . '/../lib/CalDAV/Publishing/PublishPlugin.php',
'OCA\\DAV\\CalDAV\\Publishing\\Xml\\Publisher' => $baseDir . '/../lib/CalDAV/Publishing/Xml/Publisher.php',
'OCA\\DAV\\CalDAV\\PushSync\\Plugin' => $baseDir . '/../lib/CalDAV/PushSync/Plugin.php',
'OCA\\DAV\\CalDAV\\PushSync\\Xml\\PushTransports' => $baseDir . '/../lib/CalDAV/PushSync/Xml/PushTransports.php',
'OCA\\DAV\\CalDAV\\Reminder\\Backend' => $baseDir . '/../lib/CalDAV/Reminder/Backend.php',
'OCA\\DAV\\CalDAV\\Reminder\\INotificationProvider' => $baseDir . '/../lib/CalDAV/Reminder/INotificationProvider.php',
'OCA\\DAV\\CalDAV\\Reminder\\NotificationProviderManager' => $baseDir . '/../lib/CalDAV/Reminder/NotificationProviderManager.php',
Expand Down Expand Up @@ -203,6 +205,8 @@
'OCA\\DAV\\DAV\\ViewOnlyPlugin' => $baseDir . '/../lib/DAV/ViewOnlyPlugin.php',
'OCA\\DAV\\Db\\Direct' => $baseDir . '/../lib/Db/Direct.php',
'OCA\\DAV\\Db\\DirectMapper' => $baseDir . '/../lib/Db/DirectMapper.php',
'OCA\\DAV\\Db\\PushKey' => $baseDir . '/../lib/Db/PushKey.php',
'OCA\\DAV\\Db\\PushKeyMapper' => $baseDir . '/../lib/Db/PushKeyMapper.php',
'OCA\\DAV\\Direct\\DirectFile' => $baseDir . '/../lib/Direct/DirectFile.php',
'OCA\\DAV\\Direct\\DirectHome' => $baseDir . '/../lib/Direct/DirectHome.php',
'OCA\\DAV\\Direct\\Server' => $baseDir . '/../lib/Direct/Server.php',
Expand Down Expand Up @@ -297,9 +301,13 @@
'OCA\\DAV\\Migration\\Version1018Date20210312100735' => $baseDir . '/../lib/Migration/Version1018Date20210312100735.php',
'OCA\\DAV\\Migration\\Version1024Date20211221144219' => $baseDir . '/../lib/Migration/Version1024Date20211221144219.php',
'OCA\\DAV\\Migration\\Version1027Date20230504122946' => $baseDir . '/../lib/Migration/Version1027Date20230504122946.php',
'OCA\\DAV\\Migration\\Version1028Date20230521143154' => $baseDir . '/../lib/Migration/Version1028Date20230521143154.php',
'OCA\\DAV\\Profiler\\ProfilerPlugin' => $baseDir . '/../lib/Profiler/ProfilerPlugin.php',
'OCA\\DAV\\Provisioning\\Apple\\AppleProvisioningNode' => $baseDir . '/../lib/Provisioning/Apple/AppleProvisioningNode.php',
'OCA\\DAV\\Provisioning\\Apple\\AppleProvisioningPlugin' => $baseDir . '/../lib/Provisioning/Apple/AppleProvisioningPlugin.php',
'OCA\\DAV\\Push\\IPushTransport' => $baseDir . '/../lib/Push/IPushTransport.php',
'OCA\\DAV\\Push\\IPushTransportProvider' => $baseDir . '/../lib/Push/IPushTransportProvider.php',
'OCA\\DAV\\Push\\PushTransportManager' => $baseDir . '/../lib/Push/PushTransportManager.php',
'OCA\\DAV\\RootCollection' => $baseDir . '/../lib/RootCollection.php',
'OCA\\DAV\\Search\\ACalendarSearchProvider' => $baseDir . '/../lib/Search/ACalendarSearchProvider.php',
'OCA\\DAV\\Search\\ContactsSearchProvider' => $baseDir . '/../lib/Search/ContactsSearchProvider.php',
Expand Down
8 changes: 8 additions & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ class ComposerStaticInitDAV
'OCA\\DAV\\CalDAV\\PublicCalendarRoot' => __DIR__ . '/..' . '/../lib/CalDAV/PublicCalendarRoot.php',
'OCA\\DAV\\CalDAV\\Publishing\\PublishPlugin' => __DIR__ . '/..' . '/../lib/CalDAV/Publishing/PublishPlugin.php',
'OCA\\DAV\\CalDAV\\Publishing\\Xml\\Publisher' => __DIR__ . '/..' . '/../lib/CalDAV/Publishing/Xml/Publisher.php',
'OCA\\DAV\\CalDAV\\PushSync\\Plugin' => __DIR__ . '/..' . '/../lib/CalDAV/PushSync/Plugin.php',
'OCA\\DAV\\CalDAV\\PushSync\\Xml\\PushTransports' => __DIR__ . '/..' . '/../lib/CalDAV/PushSync/Xml/PushTransports.php',
'OCA\\DAV\\CalDAV\\Reminder\\Backend' => __DIR__ . '/..' . '/../lib/CalDAV/Reminder/Backend.php',
'OCA\\DAV\\CalDAV\\Reminder\\INotificationProvider' => __DIR__ . '/..' . '/../lib/CalDAV/Reminder/INotificationProvider.php',
'OCA\\DAV\\CalDAV\\Reminder\\NotificationProviderManager' => __DIR__ . '/..' . '/../lib/CalDAV/Reminder/NotificationProviderManager.php',
Expand Down Expand Up @@ -218,6 +220,8 @@ class ComposerStaticInitDAV
'OCA\\DAV\\DAV\\ViewOnlyPlugin' => __DIR__ . '/..' . '/../lib/DAV/ViewOnlyPlugin.php',
'OCA\\DAV\\Db\\Direct' => __DIR__ . '/..' . '/../lib/Db/Direct.php',
'OCA\\DAV\\Db\\DirectMapper' => __DIR__ . '/..' . '/../lib/Db/DirectMapper.php',
'OCA\\DAV\\Db\\PushKey' => __DIR__ . '/..' . '/../lib/Db/PushKey.php',
'OCA\\DAV\\Db\\PushKeyMapper' => __DIR__ . '/..' . '/../lib/Db/PushKeyMapper.php',
'OCA\\DAV\\Direct\\DirectFile' => __DIR__ . '/..' . '/../lib/Direct/DirectFile.php',
'OCA\\DAV\\Direct\\DirectHome' => __DIR__ . '/..' . '/../lib/Direct/DirectHome.php',
'OCA\\DAV\\Direct\\Server' => __DIR__ . '/..' . '/../lib/Direct/Server.php',
Expand Down Expand Up @@ -312,9 +316,13 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Migration\\Version1018Date20210312100735' => __DIR__ . '/..' . '/../lib/Migration/Version1018Date20210312100735.php',
'OCA\\DAV\\Migration\\Version1024Date20211221144219' => __DIR__ . '/..' . '/../lib/Migration/Version1024Date20211221144219.php',
'OCA\\DAV\\Migration\\Version1027Date20230504122946' => __DIR__ . '/..' . '/../lib/Migration/Version1027Date20230504122946.php',
'OCA\\DAV\\Migration\\Version1028Date20230521143154' => __DIR__ . '/..' . '/../lib/Migration/Version1028Date20230521143154.php',
'OCA\\DAV\\Profiler\\ProfilerPlugin' => __DIR__ . '/..' . '/../lib/Profiler/ProfilerPlugin.php',
'OCA\\DAV\\Provisioning\\Apple\\AppleProvisioningNode' => __DIR__ . '/..' . '/../lib/Provisioning/Apple/AppleProvisioningNode.php',
'OCA\\DAV\\Provisioning\\Apple\\AppleProvisioningPlugin' => __DIR__ . '/..' . '/../lib/Provisioning/Apple/AppleProvisioningPlugin.php',
'OCA\\DAV\\Push\\IPushTransport' => __DIR__ . '/..' . '/../lib/Push/IPushTransport.php',
'OCA\\DAV\\Push\\IPushTransportProvider' => __DIR__ . '/..' . '/../lib/Push/IPushTransportProvider.php',
'OCA\\DAV\\Push\\PushTransportManager' => __DIR__ . '/..' . '/../lib/Push/PushTransportManager.php',
'OCA\\DAV\\RootCollection' => __DIR__ . '/..' . '/../lib/RootCollection.php',
'OCA\\DAV\\Search\\ACalendarSearchProvider' => __DIR__ . '/..' . '/../lib/Search/ACalendarSearchProvider.php',
'OCA\\DAV\\Search\\ContactsSearchProvider' => __DIR__ . '/..' . '/../lib/Search/ContactsSearchProvider.php',
Expand Down
79 changes: 79 additions & 0 deletions apps/dav/lib/CalDAV/PushSync/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Thomas Citharel <[email protected]>
*
* @author Thomas Citharel <[email protected]>
*
* @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\DAV\CalDAV\PushSync;

use Closure;
use OCA\DAV\CalDAV\Calendar;
use OCA\DAV\CalDAV\CalendarHome;
use OCA\DAV\CalDAV\PushSync\Xml\PushTransports;
use OCA\DAV\Db\PushKeyMapper;
use OCA\DAV\Push\IPushTransportProvider;
use OCA\DAV\Push\PushTransportManager;
use Sabre\DAV\INode;
use Sabre\DAV\PropFind;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;

class Plugin extends ServerPlugin {
public const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
public const PROPERTY_PUSH_TRANSPORTS = '{' . self::NS_CALENDARSERVER . '}push-transports';
public const PROPERTY_PUSHKEY = '{' . self::NS_CALENDARSERVER . '}pushkey';

public function __construct(private PushKeyMapper $pushKeyMapper, private PushTransportManager $pushTransportManager) { }

public function initialize(Server $server): void {
$server->on('propFind', Closure::fromCallable([$this, 'propFind']));
}

private function propFind(
PropFind $propFind,
INode $node): void {
if ($node instanceof CalendarHome) {
$pushTransportProviders = $this->pushTransportManager->getPushTransportProviders();
$propFind->handle(self::PROPERTY_PUSH_TRANSPORTS, function () use ($node, $pushTransportProviders) {
return new PushTransports(array_map(function (IPushTransportProvider $pushTransportProvider) {
return $pushTransportProvider->getPushTransport();
}, $pushTransportProviders));
});
$propFind->handle(self::PROPERTY_PUSHKEY, function () use ($node) {
return $this->pushKeyMapper->getForPrincipal($node->getOwner());
});
}
if ($node instanceof Calendar) {
$propFind->handle(self::PROPERTY_PUSHKEY, function () use ($node) {
return $this->pushKeyMapper->getForUri($node->getOwner(), $node->getName());
});
}
}

public function getFeatures(): array {
return ['nc-calendar-push-sync'];
}

public function getPluginName(): string {
return 'nc-calendar-push-sync';
}
}
90 changes: 90 additions & 0 deletions apps/dav/lib/CalDAV/PushSync/Xml/PushTransports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Thomas Citharel <[email protected]>
*
* @author Thomas Citharel <[email protected]>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\DAV\CalDAV\PushSync\Xml;

use OCA\DAV\CalDAV\PushSync\Plugin;
use OCA\DAV\Push\IPushTransport;
use Sabre\Xml\Writer;
use Sabre\Xml\XmlSerializable;


class PushTransports implements XmlSerializable {

/** @var IPushTransport[] */
private array $pushTransports;

public function __construct(array $pushTransports) {
$this->pushTransports = $pushTransports;
}

public function getValue(): array {
return $this->pushTransports;
}

/**
* The xmlSerialize method is called during xml writing.
*
* Use the $writer argument to write its own xml serialization.
*
* An important note: do _not_ create a parent element. Any element
* implementing XmlSerializble should only ever write what's considered
* its 'inner xml'.
*
* The parent of the current element is responsible for writing a
* containing element.
*
* This allows serializers to be re-used for different element names.
*
* If you are opening new elements, you must also close them again.
*
* @param Writer $writer
* @return void
*/
public function xmlSerialize(Writer $writer): void {
// $cs = '{' . Plugin::NS_CALENDARSERVER . '}';

if (count($this->pushTransports) <= 0) return;

//$writer->startElement($cs . 'push-transports');

foreach ($this->pushTransports as $pushTransport) {
$pushTransport->xmlSerialize($writer);

// $writer->startElement($cs . 'transport');
// $writer->writeAttribute('type', 'whatever');
//
// $writer->startElement($cs . 'subscription-url');
// $writer->writeElement('{DAV:}href', $pushTransport->getSubscriptionUrl());
// $writer->endElement();
//
// $writer->writeElement('apsbundleid', 'whatever');
// $writer->writeElement('env', 'whatever');
// $writer->writeElement('refresh-interval', $pushTransport->getRefreshInterval());
//
// $writer->endElement(); // transport
}
// $writer->endElement();
}
}
48 changes: 48 additions & 0 deletions apps/dav/lib/Db/PushKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Thomas Citharel <[email protected]>
*
* @author Thomas Citharel <[email protected]>
*
* @license AGPL-3.0
*
* 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\DAV\Db;

use OCP\AppFramework\Db\Entity;

/**
* @method string getPrincipalUri()
* @method void setPrincipalUri(string $principalUri)
* @method string getUri()
* @method void setUri(string $principalUri)
* @method string getPushKey()
* @method void setPushKey(string $pushKey)
*/
class PushKey extends Entity {
protected ?string $principalUri = null;
protected ?string $uri = null;
protected ?string $pushKey = null;

public function __construct() {
$this->addType('principalUri', 'string');
$this->addType('uri', 'string');
$this->addType('pushKey', 'string');
}
}
75 changes: 75 additions & 0 deletions apps/dav/lib/Db/PushKeyMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/**
* @copyright 2023 Thomas Citharel <[email protected]>
*
* @author Thomas Citharel <[email protected]>
*
* @license AGPL-3.0
*
* 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\DAV\Db;

use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\QBMapper;
use OCP\IDBConnection;

/**
* @template-extends QBMapper<PushKey>
*/
class PushKeyMapper extends QBMapper {
public function __construct(IDBConnection $db) {
parent::__construct($db, 'push_transports', PushKey::class);
}

/**
* @return PushKey[]
* @throws DoesNotExistException
*/
public function getForPrincipal(string $principalUri): array {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('principalUri', $qb->createNamedParameter($principalUri))
)
->andWhere($qb->expr()->eq('uri', $qb->createNamedParameter(null)))
;

return parent::findEntities($qb);
}

/**
* @return PushKey[]
* @throws DoesNotExistException
*/
public function getForUri(string $principalUri, string $uri): array {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
->from($this->getTableName())
->where(
$qb->expr()->eq('principalUri', $qb->createNamedParameter($principalUri))
)
->andWhere($qb->expr()->eq('uri', $qb->createNamedParameter($uri)))
;

return parent::findEntities($qb);
}
}
Loading