From d512a5629ec01fecd87944836eb9b02eb11257b8 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Fri, 7 May 2021 10:26:49 +0200 Subject: Remove the \OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject event Signed-off-by: Christoph Wurst --- apps/dav/composer/composer/autoload_classmap.php | 4 +- apps/dav/composer/composer/autoload_static.php | 4 +- apps/dav/lib/AppInfo/Application.php | 14 ++-- apps/dav/lib/CalDAV/CalDavBackend.php | 9 --- apps/dav/lib/Listener/ActivityUpdaterListener.php | 88 +++++++++++++++++++++ .../CalendarDeletionActivityUpdaterListener.php | 75 ------------------ .../CalendarDeletionReminderUpdaterListener.php | 74 ------------------ .../CalendarObjectReminderUpdaterListener.php | 91 ++++++++++++++++++++++ apps/dav/tests/unit/CalDAV/CalDavBackendTest.php | 32 +++++--- 9 files changed, 210 insertions(+), 181 deletions(-) create mode 100644 apps/dav/lib/Listener/ActivityUpdaterListener.php delete mode 100644 apps/dav/lib/Listener/CalendarDeletionActivityUpdaterListener.php delete mode 100644 apps/dav/lib/Listener/CalendarDeletionReminderUpdaterListener.php create mode 100644 apps/dav/lib/Listener/CalendarObjectReminderUpdaterListener.php (limited to 'apps/dav') diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index fe6c6ab3253..5da3526c820 100644 --- a/apps/dav/composer/composer/autoload_classmap.php +++ b/apps/dav/composer/composer/autoload_classmap.php @@ -205,10 +205,10 @@ return array( 'OCA\\DAV\\Files\\Sharing\\FilesDropPlugin' => $baseDir . '/../lib/Files/Sharing/FilesDropPlugin.php', 'OCA\\DAV\\Files\\Sharing\\PublicLinkCheckPlugin' => $baseDir . '/../lib/Files/Sharing/PublicLinkCheckPlugin.php', 'OCA\\DAV\\HookManager' => $baseDir . '/../lib/HookManager.php', + 'OCA\\DAV\\Listener\\ActivityUpdaterListener' => $baseDir . '/../lib/Listener/ActivityUpdaterListener.php', 'OCA\\DAV\\Listener\\CalendarContactInteractionListener' => $baseDir . '/../lib/Listener/CalendarContactInteractionListener.php', - 'OCA\\DAV\\Listener\\CalendarDeletionActivityUpdaterListener' => $baseDir . '/../lib/Listener/CalendarDeletionActivityUpdaterListener.php', 'OCA\\DAV\\Listener\\CalendarDeletionDefaultUpdaterListener' => $baseDir . '/../lib/Listener/CalendarDeletionDefaultUpdaterListener.php', - 'OCA\\DAV\\Listener\\CalendarDeletionReminderUpdaterListener' => $baseDir . '/../lib/Listener/CalendarDeletionReminderUpdaterListener.php', + 'OCA\\DAV\\Listener\\CalendarObjectReminderUpdaterListener' => $baseDir . '/../lib/Listener/CalendarObjectReminderUpdaterListener.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndex.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php', 'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => $baseDir . '/../lib/Migration/BuildSocialSearchIndex.php', diff --git a/apps/dav/composer/composer/autoload_static.php b/apps/dav/composer/composer/autoload_static.php index 48e453b72f7..60162ba5554 100644 --- a/apps/dav/composer/composer/autoload_static.php +++ b/apps/dav/composer/composer/autoload_static.php @@ -220,10 +220,10 @@ class ComposerStaticInitDAV 'OCA\\DAV\\Files\\Sharing\\FilesDropPlugin' => __DIR__ . '/..' . '/../lib/Files/Sharing/FilesDropPlugin.php', 'OCA\\DAV\\Files\\Sharing\\PublicLinkCheckPlugin' => __DIR__ . '/..' . '/../lib/Files/Sharing/PublicLinkCheckPlugin.php', 'OCA\\DAV\\HookManager' => __DIR__ . '/..' . '/../lib/HookManager.php', + 'OCA\\DAV\\Listener\\ActivityUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/ActivityUpdaterListener.php', 'OCA\\DAV\\Listener\\CalendarContactInteractionListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarContactInteractionListener.php', - 'OCA\\DAV\\Listener\\CalendarDeletionActivityUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarDeletionActivityUpdaterListener.php', 'OCA\\DAV\\Listener\\CalendarDeletionDefaultUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarDeletionDefaultUpdaterListener.php', - 'OCA\\DAV\\Listener\\CalendarDeletionReminderUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarDeletionReminderUpdaterListener.php', + 'OCA\\DAV\\Listener\\CalendarObjectReminderUpdaterListener' => __DIR__ . '/..' . '/../lib/Listener/CalendarObjectReminderUpdaterListener.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndex.php', 'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php', 'OCA\\DAV\\Migration\\BuildSocialSearchIndex' => __DIR__ . '/..' . '/../lib/Migration/BuildSocialSearchIndex.php', diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index 8298a9dd6f7..8bedb287148 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -54,13 +54,14 @@ use OCA\DAV\CardDAV\PhotoCache; use OCA\DAV\CardDAV\SyncService; use OCA\DAV\Events\CalendarDeletedEvent; use OCA\DAV\Events\CalendarObjectCreatedEvent; +use OCA\DAV\Events\CalendarObjectDeletedEvent; use OCA\DAV\Events\CalendarObjectUpdatedEvent; use OCA\DAV\Events\CalendarShareUpdatedEvent; use OCA\DAV\HookManager; +use OCA\DAV\Listener\ActivityUpdaterListener; use OCA\DAV\Listener\CalendarContactInteractionListener; -use OCA\DAV\Listener\CalendarDeletionActivityUpdaterListener; use OCA\DAV\Listener\CalendarDeletionDefaultUpdaterListener; -use OCA\DAV\Listener\CalendarDeletionReminderUpdaterListener; +use OCA\DAV\Listener\CalendarObjectReminderUpdaterListener; use OCA\DAV\Search\ContactsSearchProvider; use OCA\DAV\Search\EventsSearchProvider; use OCA\DAV\Search\TasksSearchProvider; @@ -114,11 +115,13 @@ class Application extends App implements IBootstrap { /** * Register event listeners */ - $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionActivityUpdaterListener::class); - $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionReminderUpdaterListener::class); + $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class); + $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class); $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class); $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class); $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class); + $context->registerEventListener(CalendarObjectDeletedEvent::class, ActivityUpdaterListener::class); + $context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarObjectReminderUpdaterListener::class); $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class); $context->registerNotifierService(Notifier::class); @@ -237,8 +240,6 @@ class Application extends App implements IBootstrap { $subject = Event::SUBJECT_OBJECT_ADD; if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject') { $subject = Event::SUBJECT_OBJECT_UPDATE; - } elseif ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject') { - $subject = Event::SUBJECT_OBJECT_DELETE; } $backend->onTouchCalendarObject( $subject, @@ -257,7 +258,6 @@ class Application extends App implements IBootstrap { }; $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $listener); $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', $listener); - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', $listener); $dispatcher->addListener('OCP\Federation\TrustedServerEvent::remove', function (GenericEvent $event) { diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index fb99040dd66..b4782ca6840 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -1278,15 +1278,6 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription $shares = $this->getShares($calendarId); $this->dispatcher->dispatchTyped(new CalendarObjectDeletedEvent((int)$calendarId, $calendarRow, $shares, $data)); - $this->legacyDispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', new GenericEvent( - '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', - [ - 'calendarId' => $calendarId, - 'calendarData' => $calendarRow, - 'shares' => $shares, - 'objectData' => $data, - ] - )); } else { $subscriptionRow = $this->getSubscriptionById($calendarId); diff --git a/apps/dav/lib/Listener/ActivityUpdaterListener.php b/apps/dav/lib/Listener/ActivityUpdaterListener.php new file mode 100644 index 00000000000..9734a4fead8 --- /dev/null +++ b/apps/dav/lib/Listener/ActivityUpdaterListener.php @@ -0,0 +1,88 @@ + + * + * @author 2021 Christoph Wurst + * + * @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\DAV\Listener; + +use OCA\DAV\CalDAV\Activity\Backend as ActivityBackend; +use OCA\DAV\Events\CalendarDeletedEvent; +use OCA\DAV\Events\CalendarObjectDeletedEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use Psr\Log\LoggerInterface; +use Throwable; +use function sprintf; + +class ActivityUpdaterListener implements IEventListener { + + /** @var ActivityBackend */ + private $activityBackend; + + /** @var LoggerInterface */ + private $logger; + + public function __construct(ActivityBackend $activityBackend, + LoggerInterface $logger) { + $this->activityBackend = $activityBackend; + $this->logger = $logger; + } + + public function handle(Event $event): void { + if ($event instanceof CalendarDeletedEvent) { + try { + $this->activityBackend->onCalendarDelete( + $event->getCalendarData(), + $event->getShares() + ); + + $this->logger->debug( + sprintf('Activity generated for deleted calendar %d', $event->getCalendarId()) + ); + } catch (Throwable $e) { + // Any error with activities shouldn't abort the calendar deletion, so we just log it + $this->logger->error('Error generating activities for a deleted calendar: ' . $e->getMessage(), [ + 'exception' => $e, + ]); + } + } elseif ($event instanceof CalendarObjectDeletedEvent) { + try { + $this->activityBackend->onTouchCalendarObject( + \OCA\DAV\CalDAV\Activity\Provider\Event::SUBJECT_OBJECT_DELETE, + $event->getCalendarData(), + $event->getShares(), + $event->getObjectData() + ); + + $this->logger->debug( + sprintf('Activity generated for deleted calendar object %d', $event->getCalendarId()) + ); + } catch (Throwable $e) { + // Any error with activities shouldn't abort the calendar deletion, so we just log it + $this->logger->error('Error generating activity for a deleted calendar object: ' . $e->getMessage(), [ + 'exception' => $e, + ]); + } + } + } +} diff --git a/apps/dav/lib/Listener/CalendarDeletionActivityUpdaterListener.php b/apps/dav/lib/Listener/CalendarDeletionActivityUpdaterListener.php deleted file mode 100644 index 4139aba226e..00000000000 --- a/apps/dav/lib/Listener/CalendarDeletionActivityUpdaterListener.php +++ /dev/null @@ -1,75 +0,0 @@ - - * - * @author 2021 Christoph Wurst - * - * @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\DAV\Listener; - -use OCA\DAV\CalDAV\Activity\Backend as ActivityBackend; -use OCA\DAV\Events\CalendarDeletedEvent; -use OCP\EventDispatcher\Event; -use OCP\EventDispatcher\IEventListener; -use Psr\Log\LoggerInterface; -use Throwable; -use function sprintf; - -/** - * @template-implements IEventListener<\OCA\DAV\Events\CalendarDeletedEvent> - */ -class CalendarDeletionActivityUpdaterListener implements IEventListener { - - /** @var ActivityBackend */ - private $activityBackend; - - /** @var LoggerInterface */ - private $logger; - - public function __construct(ActivityBackend $activityBackend, - LoggerInterface $logger) { - $this->activityBackend = $activityBackend; - $this->logger = $logger; - } - - public function handle(Event $event): void { - if (!($event instanceof CalendarDeletedEvent)) { - // Not what we subscribed to - return; - } - - try { - $this->activityBackend->onCalendarDelete( - $event->getCalendarData(), - $event->getShares() - ); - - $this->logger->debug( - sprintf('Activity generated for deleted calendar %d', $event->getCalendarId()) - ); - } catch (Throwable $e) { - // Any error with activities shouldn't abort the calendar deletion, so we just log it - $this->logger->error('Error generating activities for a deleted calendar: ' . $e->getMessage(), [ - 'exception' => $e, - ]); - } - } -} diff --git a/apps/dav/lib/Listener/CalendarDeletionReminderUpdaterListener.php b/apps/dav/lib/Listener/CalendarDeletionReminderUpdaterListener.php deleted file mode 100644 index 3beecc8ff05..00000000000 --- a/apps/dav/lib/Listener/CalendarDeletionReminderUpdaterListener.php +++ /dev/null @@ -1,74 +0,0 @@ - - * - * @author 2021 Christoph Wurst - * - * @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\DAV\Listener; - -use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend; -use OCA\DAV\Events\CalendarDeletedEvent; -use OCP\EventDispatcher\Event; -use OCP\EventDispatcher\IEventListener; -use Psr\Log\LoggerInterface; -use Throwable; -use function sprintf; - -/** - * @template-implements IEventListener<\OCA\DAV\Events\CalendarDeletedEvent> - */ -class CalendarDeletionReminderUpdaterListener implements IEventListener { - - /** @var ReminderBackend */ - private $reminderBackend; - - /** @var LoggerInterface */ - private $logger; - - public function __construct(ReminderBackend $reminderBackend, - LoggerInterface $logger) { - $this->reminderBackend = $reminderBackend; - $this->logger = $logger; - } - - public function handle(Event $event): void { - if (!($event instanceof CalendarDeletedEvent)) { - // Not what we subscribed to - return; - } - - try { - $this->reminderBackend->cleanRemindersForCalendar( - $event->getCalendarId() - ); - - $this->logger->debug( - sprintf('Reminders of calendar %d cleaned up', $event->getCalendarId()) - ); - } catch (Throwable $e) { - // Any error with activities shouldn't abort the calendar deletion, so we just log it - $this->logger->error('Error cleaning up reminders of a deleted calendar: ' . $e->getMessage(), [ - 'exception' => $e, - ]); - } - } -} diff --git a/apps/dav/lib/Listener/CalendarObjectReminderUpdaterListener.php b/apps/dav/lib/Listener/CalendarObjectReminderUpdaterListener.php new file mode 100644 index 00000000000..63fe786cfa2 --- /dev/null +++ b/apps/dav/lib/Listener/CalendarObjectReminderUpdaterListener.php @@ -0,0 +1,91 @@ + + * + * @author 2021 Christoph Wurst + * + * @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\DAV\Listener; + +use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend; +use OCA\DAV\CalDAV\Reminder\ReminderService; +use OCA\DAV\Events\CalendarDeletedEvent; +use OCA\DAV\Events\CalendarObjectDeletedEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use Psr\Log\LoggerInterface; +use Throwable; +use function sprintf; + +class CalendarObjectReminderUpdaterListener implements IEventListener { + + /** @var ReminderBackend */ + private $reminderBackend; + + /** @var ReminderService */ + private $reminderService; + + /** @var LoggerInterface */ + private $logger; + + public function __construct(ReminderBackend $reminderBackend, + ReminderService $reminderService, + LoggerInterface $logger) { + $this->reminderBackend = $reminderBackend; + $this->reminderService = $reminderService; + $this->logger = $logger; + } + + public function handle(Event $event): void { + if ($event instanceof CalendarDeletedEvent) { + try { + $this->reminderBackend->cleanRemindersForCalendar( + $event->getCalendarId() + ); + + $this->logger->debug( + sprintf('Reminders of calendar %d cleaned up', $event->getCalendarId()) + ); + } catch (Throwable $e) { + // Any error with activities shouldn't abort the calendar deletion, so we just log it + $this->logger->error('Error cleaning up reminders of a deleted calendar: ' . $e->getMessage(), [ + 'exception' => $e, + ]); + } + } elseif ($event instanceof CalendarObjectDeletedEvent) { + try { + $this->reminderService->onTouchCalendarObject( + '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', + $event->getObjectData() + ); + + $this->logger->debug( + sprintf('Reminders of calendar object of calendar %d cleaned up', $event->getCalendarId()) + ); + } catch (Throwable $e) { + // Any error with activities shouldn't abort the calendar object deletion, so we just log it + $this->logger->error('Error cleaning up reminders of a deleted calendar object: ' . $e->getMessage(), [ + 'exception' => $e, + ]); + } + } + } +} diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index 25966aa5c95..98e1b728f9f 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -278,9 +278,11 @@ EOD; $this->assertEquals($calData, $calendarObject['calendardata']); // delete the card - $this->legacyDispatcher->expects($this->at(0)) - ->method('dispatch') - ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject'); + $this->dispatcher->expects(self::once()) + ->method('dispatchTyped') + ->with(self::callback(function ($event) { + return $event instanceof CalendarDeletedEvent; + })); $this->backend->deleteCalendarObject($calendarId, $uri); $calendarObjects = $this->backend->getCalendarObjects($calendarId); $this->assertCount(0, $calendarObjects); @@ -412,17 +414,23 @@ EOD; $this->assertEquals($calData[2], $calendarObjects[1]['calendardata']); // delete the card - $this->legacyDispatcher->expects($this->at(0)) - ->method('dispatch') - ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject'); + $this->dispatcher->expects(self::once()) + ->method('dispatchTyped') + ->with(self::callback(function ($event) { + return $event instanceof CalendarDeletedEvent; + })); $this->backend->deleteCalendarObject($calendarId, $uri0); - $this->legacyDispatcher->expects($this->at(0)) - ->method('dispatch') - ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject'); + $this->dispatcher->expects(self::once()) + ->method('dispatchTyped') + ->with(self::callback(function ($event) { + return $event instanceof CalendarDeletedEvent; + })); $this->backend->deleteCalendarObject($calendarId, $uri1); - $this->legacyDispatcher->expects($this->at(0)) - ->method('dispatch') - ->with('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject'); + $this->dispatcher->expects(self::once()) + ->method('dispatchTyped') + ->with(self::callback(function ($event) { + return $event instanceof CalendarDeletedEvent; + })); $this->backend->deleteCalendarObject($calendarId, $uri2); $calendarObjects = $this->backend->getCalendarObjects($calendarId); $this->assertCount(0, $calendarObjects); -- cgit v1.2.3