From a6d4368058effe8617ce884bb40c1cc6eb2505cc Mon Sep 17 00:00:00 2001 From: Jonathan Treffler Date: Wed, 12 Jun 2024 03:05:59 +0200 Subject: [PATCH] added calendar listener --- lib/AppInfo/Application.php | 11 ++++- lib/Db/SubscriptionMapper.php | 17 ++++++++ lib/Listener/CalendarListener.php | 71 +++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 lib/Listener/CalendarListener.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 69e6572..70c57d8 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -5,14 +5,18 @@ namespace OCA\DavPush\AppInfo; +use OCA\DavPush\Listener\SabrePluginAddListener; +use OCA\DavPush\Listener\CalendarListener; + use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Bootstrap\IBootContext; use OCA\DAV\Events\SabrePluginAddEvent; - -use OCA\DavPush\Listener\SabrePluginAddListener; +use OCA\DAV\Events\CalendarObjectCreatedEvent; +use OCA\DAV\Events\CalendarObjectDeletedEvent; +use OCA\DAV\Events\CalendarObjectUpdatedEvent; class Application extends App implements IBootstrap { public const APP_ID = 'dav_push'; @@ -23,6 +27,9 @@ class Application extends App implements IBootstrap { public function register(IRegistrationContext $context): void { $context->registerEventListener(SabrePluginAddEvent::class, SabrePluginAddListener::class); + $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarListener::class); + $context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarListener::class); + $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarListener::class); } public function boot(IBootContext $context): void { diff --git a/lib/Db/SubscriptionMapper.php b/lib/Db/SubscriptionMapper.php index 00e10dd..9cddbdc 100644 --- a/lib/Db/SubscriptionMapper.php +++ b/lib/Db/SubscriptionMapper.php @@ -32,4 +32,21 @@ class SubscriptionMapper extends QBMapper { return $this->findEntity($qb); } + + /** + * @param string $userId + * @param string $collectionName + * @return Subscription[] + */ + public function findAll(string $userId, string $collectionName): array { + /* @var $qb IQueryBuilder */ + $qb = $this->db->getQueryBuilder(); + + $qb->select('*') + ->from(self::TABLENAME) + ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) + ->andWhere($qb->expr()->eq('collection_name', $qb->createNamedParameter($collectionName))); + + return $this->findEntities($qb); + } } \ No newline at end of file diff --git a/lib/Listener/CalendarListener.php b/lib/Listener/CalendarListener.php new file mode 100644 index 0000000..ed36026 --- /dev/null +++ b/lib/Listener/CalendarListener.php @@ -0,0 +1,71 @@ + + * + * @author bitfire web engineering GmbH + * @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\Listener; + +use OCP\IConfig; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +use OCA\DAV\Events\CalendarObjectCreatedEvent; +use OCA\DAV\Events\CalendarObjectDeletedEvent; +use OCA\DAV\Events\CalendarObjectUpdatedEvent; +use OCA\DAV\Events\CardCreatedEvent; +use OCA\DAV\Events\CardDeletedEvent; +use OCA\DAV\Events\CardUpdatedEvent; + +use Psr\Log\LoggerInterface; + +use OCA\DavPush\Db\SubscriptionMapper; +use OCA\DavPush\Transport\TransportManager; + +class CalendarListener implements IEventListener { + + public function __construct( + private LoggerInterface $logger, + private SubscriptionMapper $subscriptionMapper, + private TransportManager $transportManager, + private $userId, + ) {} + + public function handle(Event $event): void { + if (!($event instanceOf CalendarObjectCreatedEvent) && !($event instanceOf CalendarObjectDeletedEvent) && + !($event instanceOf CalendarObjectUpdatedEvent)) { + return; + } + + $collectionName = $event->getCalendarData()['uri']; + $subscriptions = $this->subscriptionMapper->findAll($this->userId, $collectionName); + + foreach($subscriptions as $subscription) { + $transport = $this->transportManager->getTransport($subscription->getTransport()); + try { + $transport->notify($this->userId, $collectionName, json_decode($subscription->getData(), True)); + } catch (Error $e) { + $this->logger->error("transport " . $subscription->getTransport() . " failed to deliver notification to subscription " . $subscription->getId()); + } + } + } +}