diff options
author | Joas Schilling <coding@schilljs.com> | 2020-08-28 16:02:19 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-08-28 16:02:19 +0300 |
commit | bbbf4a496e7cbc08b0642a3b44dcf10fec270416 (patch) | |
tree | 553de60eda6550b22f0da3a9f163d6ca8a55657f | |
parent | f920cba83d9088d3ee515a21c91e813005d23ced (diff) | |
parent | 311870e657630d34690484c9863280c86b00e846 (diff) |
Merge branch 'bugfix/noid/nextcloud-20' into master
* bugfix/noid/nextcloud-20:
Only send notifications once to the owner
Compatibility with Nextcloud 20
-rw-r--r-- | appinfo/app.php | 23 | ||||
-rw-r--r-- | appinfo/info.xml | 4 | ||||
-rw-r--r-- | lib/AppInfo/Application.php | 47 | ||||
-rw-r--r-- | lib/EventListener.php (renamed from lib/Backend.php) | 40 |
4 files changed, 51 insertions, 63 deletions
diff --git a/appinfo/app.php b/appinfo/app.php deleted file mode 100644 index a36905b..0000000 --- a/appinfo/app.php +++ /dev/null @@ -1,23 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2018, Joas Schilling <coding@schilljs.com> - * - * @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/>. - * - */ - -$application = \OC::$server->query(\OCA\EventUpdateNotification\AppInfo\Application::class); -$application->register(); diff --git a/appinfo/info.xml b/appinfo/info.xml index 1bf7126..d1165f2 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -6,7 +6,7 @@ <summary>Receive a notification when an event in a shared calendar was added, modified or deleted.</summary> <description><![CDATA[Receive a notification when an event in a shared calendar was added, modified or deleted.]]></description> - <version>1.0.2</version> + <version>1.1.0</version> <licence>agpl</licence> <author>Joas Schilling</author> @@ -27,6 +27,6 @@ <screenshot>https://github.com/nickv-nextcloud/event_update_notification/raw/master/docs/demo.png</screenshot> <dependencies> - <nextcloud min-version="17" max-version="19" /> + <nextcloud min-version="20" max-version="20" /> </dependencies> </info> diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index f277521..ab8c010 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -23,46 +23,29 @@ namespace OCA\EventUpdateNotification\AppInfo; -use OCA\EventUpdateNotification\Backend; +use OCA\DAV\Events\CalendarObjectCreatedEvent; +use OCA\DAV\Events\CalendarObjectDeletedEvent; +use OCA\DAV\Events\CalendarObjectUpdatedEvent; +use OCA\EventUpdateNotification\EventListener; use OCA\EventUpdateNotification\Notifier; use OCP\AppFramework\App; -use Symfony\Component\EventDispatcher\GenericEvent; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Notification\IManager; -class Application extends App { +class Application extends App implements IBootstrap { public function __construct() { parent::__construct('event_update_notification'); } - public function register() { - $this->registerEventListener(); - $this->registerNotifier(); + public function register(IRegistrationContext $context): void { + $context->registerEventListener(CalendarObjectCreatedEvent::class, EventListener::class); + $context->registerEventListener(CalendarObjectUpdatedEvent::class, EventListener::class); + $context->registerEventListener(CalendarObjectDeletedEvent::class, EventListener::class); } - public function registerEventListener() { - $dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); - $listener = function(GenericEvent $event, $eventName) { - /** @var Backend $backend */ - $backend = $this->getContainer()->query(Backend::class); - - $subject = Notifier::SUBJECT_OBJECT_ADD; - if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject') { - $subject = Notifier::SUBJECT_OBJECT_UPDATE; - } else if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject') { - $subject = Notifier::SUBJECT_OBJECT_DELETE; - } - $backend->onTouchCalendarObject( - $subject, - $event->getArgument('calendarData'), - $event->getArgument('shares'), - $event->getArgument('objectData') - ); - }; - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $listener); - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', $listener); - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', $listener); - } - - protected function registerNotifier() { - $this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class); + public function boot(IBootContext $context): void { + $context->getServerContainer()->get(IManager::class)->registerNotifierService(Notifier::class); } } diff --git a/lib/Backend.php b/lib/EventListener.php index 77528ae..d78c427 100644 --- a/lib/Backend.php +++ b/lib/EventListener.php @@ -25,6 +25,11 @@ declare(strict_types=1); namespace OCA\EventUpdateNotification; use OCA\DAV\CalDAV\CalDavBackend; +use OCA\DAV\Events\CalendarObjectCreatedEvent; +use OCA\DAV\Events\CalendarObjectDeletedEvent; +use OCA\DAV\Events\CalendarObjectUpdatedEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; use OCP\Notification\IManager as INotificationManager; use OCP\IGroup; use OCP\IGroupManager; @@ -34,7 +39,7 @@ use Sabre\VObject\Component\VEvent; use Sabre\VObject\Reader; use Sabre\VObject\Recur\EventIterator; -class Backend { +class EventListener implements IEventListener { /** @var INotificationManager */ protected $notificationManager; @@ -52,6 +57,28 @@ class Backend { } /** + * @param Event $event + * @throws \Sabre\VObject\Recur\MaxInstancesExceededException + * @throws \Sabre\VObject\Recur\NoInstancesException + */ + public function handle(Event $event): void { + if (!($event instanceof CalendarObjectCreatedEvent) + && !($event instanceof CalendarObjectUpdatedEvent) + && !($event instanceof CalendarObjectDeletedEvent)) { + return; + } + + $subject = Notifier::SUBJECT_OBJECT_ADD; + if ($event instanceof CalendarObjectUpdatedEvent) { + $subject = Notifier::SUBJECT_OBJECT_UPDATE; + } else if ($event instanceof CalendarObjectDeletedEvent) { + $subject = Notifier::SUBJECT_OBJECT_DELETE; + } + + $this->onTouchCalendarObject($subject, $event->getCalendarData(), $event->getShares(), $event->getObjectData()); + } + + /** * Creates activities when a calendar object was created/updated/deleted * * @param string $action @@ -61,7 +88,7 @@ class Backend { * @throws \Sabre\VObject\Recur\MaxInstancesExceededException * @throws \Sabre\VObject\Recur\NoInstancesException */ - public function onTouchCalendarObject(string $action, array $calendarData, array $shares, array $objectData) { + public function onTouchCalendarObject(string $action, array $calendarData, array $shares, array $objectData): void { if (!isset($calendarData['principaluri'])) { return; } @@ -102,8 +129,7 @@ class Backend { 'hasTime' => $hasTime, ]); - $users = $this->getUsersForShares($shares); - $users[] = $owner; + $users = $this->getUsersForShares($shares, $owner); foreach ($users as $user) { if ($user === $currentUser) { @@ -166,10 +192,12 @@ class Backend { * Get all users that have access to a given calendar * * @param array $shares + * @param string $owner * @return string[] */ - protected function getUsersForShares(array $shares): array { - $users = $groups = []; + protected function getUsersForShares(array $shares, string $owner): array { + $users = [$owner]; + $groups = []; foreach ($shares as $share) { $prinical = explode('/', $share['{http://owncloud.org/ns}principal']); if ($prinical[1] === 'users') { |