Plugin.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Thomas Citharel <nextcloud@tcit.fr>
  5. *
  6. * @author Thomas Citharel <nextcloud@tcit.fr>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV\PushSync;
  25. use Closure;
  26. use OCA\DAV\CalDAV\Calendar;
  27. use OCA\DAV\CalDAV\CalendarHome;
  28. use OCA\DAV\CalDAV\PushSync\Xml\PushTransports;
  29. use OCA\DAV\Db\PushKeyMapper;
  30. use OCA\DAV\Push\IPushTransportProvider;
  31. use OCA\DAV\Push\PushTransportManager;
  32. use Sabre\DAV\INode;
  33. use Sabre\DAV\PropFind;
  34. use Sabre\DAV\Server;
  35. use Sabre\DAV\ServerPlugin;
  36. class Plugin extends ServerPlugin {
  37. public const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
  38. public const PROPERTY_PUSH_TRANSPORTS = '{' . self::NS_CALENDARSERVER . '}push-transports';
  39. public const PROPERTY_PUSHKEY = '{' . self::NS_CALENDARSERVER . '}pushkey';
  40. public function __construct(private PushKeyMapper $pushKeyMapper, private PushTransportManager $pushTransportManager) { }
  41. public function initialize(Server $server): void {
  42. $server->on('propFind', Closure::fromCallable([$this, 'propFind']));
  43. }
  44. private function propFind(
  45. PropFind $propFind,
  46. INode $node): void {
  47. if ($node instanceof CalendarHome) {
  48. $pushTransportProviders = $this->pushTransportManager->getPushTransportProviders();
  49. $propFind->handle(self::PROPERTY_PUSH_TRANSPORTS, function () use ($node, $pushTransportProviders) {
  50. return new PushTransports(array_map(function (IPushTransportProvider $pushTransportProvider) {
  51. return $pushTransportProvider->getPushTransport();
  52. }, $pushTransportProviders));
  53. });
  54. $propFind->handle(self::PROPERTY_PUSHKEY, function () use ($node) {
  55. return $this->pushKeyMapper->getForPrincipal($node->getOwner());
  56. });
  57. }
  58. if ($node instanceof Calendar) {
  59. $propFind->handle(self::PROPERTY_PUSHKEY, function () use ($node) {
  60. return $this->pushKeyMapper->getForUri($node->getOwner(), $node->getName());
  61. });
  62. }
  63. }
  64. public function getFeatures(): array {
  65. return ['nc-calendar-push-sync'];
  66. }
  67. public function getPluginName(): string {
  68. return 'nc-calendar-push-sync';
  69. }
  70. }