From 884e34b12a90f8958ac46e0b8c135d61c3895cd5 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Fri, 7 May 2021 16:58:33 +0200 Subject: [PATCH 1/2] Remove the untyped calendar create event Signed-off-by: Christoph Wurst --- apps/dav/lib/AppInfo/Application.php | 9 ++------- apps/dav/lib/CalDAV/CalDavBackend.php | 6 ------ .../lib/Listener/ActivityUpdaterListener.php | 18 +++++++++++++++++- .../unit/CalDAV/AbstractCalDavBackend.php | 9 ++++++--- .../tests/unit/CalDAV/CalDavBackendTest.php | 9 ++++++--- 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index 19c360e708a..4c410697d2e 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -51,6 +51,7 @@ use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\CardDAV\ContactsManager; use OCA\DAV\CardDAV\PhotoCache; use OCA\DAV\CardDAV\SyncService; +use OCA\DAV\Events\CalendarCreatedEvent; use OCA\DAV\Events\CalendarDeletedEvent; use OCA\DAV\Events\CalendarObjectCreatedEvent; use OCA\DAV\Events\CalendarObjectDeletedEvent; @@ -114,6 +115,7 @@ class Application extends App implements IBootstrap { /** * Register event listeners */ + $context->registerEventListener(CalendarCreatedEvent::class, ActivityUpdaterListener::class); $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class); $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class); $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class); @@ -198,13 +200,6 @@ class Application extends App implements IBootstrap { $syncService->updateUser($user); }); - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', function (GenericEvent $event) use ($container) { - /** @var Backend $backend */ - $backend = $container->query(Backend::class); - $backend->onCalendarAdd( - $event->getArgument('calendarData') - ); - }); $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', function (GenericEvent $event) use ($container) { /** @var Backend $backend */ $backend = $container->query(Backend::class); diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index e14c0ec6b45..69924e62154 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -810,12 +810,6 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription $calendarData = $this->getCalendarById($calendarId); $this->dispatcher->dispatchTyped(new CalendarCreatedEvent((int)$calendarId, $calendarData)); - $this->legacyDispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', new GenericEvent( - '\OCA\DAV\CalDAV\CalDavBackend::createCalendar', - [ - 'calendarId' => $calendarId, - 'calendarData' => $calendarData, - ])); return $calendarId; } diff --git a/apps/dav/lib/Listener/ActivityUpdaterListener.php b/apps/dav/lib/Listener/ActivityUpdaterListener.php index 8ff65170e3f..bd59108b4a1 100644 --- a/apps/dav/lib/Listener/ActivityUpdaterListener.php +++ b/apps/dav/lib/Listener/ActivityUpdaterListener.php @@ -26,6 +26,7 @@ declare(strict_types=1); namespace OCA\DAV\Listener; use OCA\DAV\CalDAV\Activity\Backend as ActivityBackend; +use OCA\DAV\Events\CalendarCreatedEvent; use OCA\DAV\Events\CalendarDeletedEvent; use OCA\DAV\Events\CalendarObjectCreatedEvent; use OCA\DAV\Events\CalendarObjectDeletedEvent; @@ -51,7 +52,22 @@ class ActivityUpdaterListener implements IEventListener { } public function handle(Event $event): void { - if ($event instanceof CalendarDeletedEvent) { + if ($event instanceof CalendarCreatedEvent) { + try { + $this->activityBackend->onCalendarAdd( + $event->getCalendarData() + ); + + $this->logger->debug( + sprintf('Activity generated for new calendar %d', $event->getCalendarId()) + ); + } catch (Throwable $e) { + // Any error with activities shouldn't abort the calendar creation, so we just log it + $this->logger->error('Error generating activities for a new calendar: ' . $e->getMessage(), [ + 'exception' => $e, + ]); + } + } else if ($event instanceof CalendarDeletedEvent) { try { $this->activityBackend->onCalendarDelete( $event->getCalendarData(), diff --git a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php index 17ac839b5f2..1264342e27c 100644 --- a/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php +++ b/apps/dav/tests/unit/CalDAV/AbstractCalDavBackend.php @@ -31,6 +31,7 @@ use OC\KnownUser\KnownUserService; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\Proxy\ProxyMapper; use OCA\DAV\Connector\Sabre\Principal; +use OCA\DAV\Events\CalendarCreatedEvent; use OCA\DAV\Events\CalendarDeletedEvent; use OCA\DAV\Events\CalendarObjectCreatedEvent; use OCP\App\IAppManager; @@ -150,9 +151,11 @@ abstract class AbstractCalDavBackend extends TestCase { } protected function createTestCalendar() { - $this->legacyDispatcher->expects($this->at(0)) - ->method('dispatch') - ->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendar'); + $this->dispatcher->expects(self::once()) + ->method('dispatchTyped') + ->with(self::callback(function ($event) { + return $event instanceof CalendarCreatedEvent; + })); $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', [ '{http://apple.com/ns/ical/}calendar-color' => '#1C4587FF' diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index 097fbd79fe3..ec77c1f7d8c 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -36,6 +36,7 @@ use DateTime; use DateTimeZone; use OCA\DAV\CalDAV\CalDavBackend; use OCA\DAV\CalDAV\Calendar; +use OCA\DAV\Events\CalendarCreatedEvent; use OCA\DAV\Events\CalendarDeletedEvent; use OCA\DAV\Events\CalendarObjectCreatedEvent; use OCA\DAV\Events\CalendarUpdatedEvent; @@ -527,9 +528,11 @@ EOD; } public function testPublications() { - $this->legacyDispatcher->expects($this->at(0)) - ->method('dispatch') - ->with('\OCA\DAV\CalDAV\CalDavBackend::createCalendar'); + $this->dispatcher->expects(self::once()) + ->method('dispatchTyped') + ->with(self::callback(function ($event) { + return $event instanceof CalendarCreatedEvent; + })); $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []); From cebe951b8ecd1586b5fae4e0e7f6307679e36f51 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Fri, 7 May 2021 17:09:37 +0200 Subject: [PATCH 2/2] Remove the untyped calendar update event Signed-off-by: Christoph Wurst --- apps/dav/lib/AppInfo/Application.php | 11 ++-------- apps/dav/lib/CalDAV/CalDavBackend.php | 8 -------- .../lib/Listener/ActivityUpdaterListener.php | 20 ++++++++++++++++++- .../tests/unit/CalDAV/CalDavBackendTest.php | 8 +++++--- 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/apps/dav/lib/AppInfo/Application.php b/apps/dav/lib/AppInfo/Application.php index 4c410697d2e..8c1e4f77a12 100644 --- a/apps/dav/lib/AppInfo/Application.php +++ b/apps/dav/lib/AppInfo/Application.php @@ -57,6 +57,7 @@ use OCA\DAV\Events\CalendarObjectCreatedEvent; use OCA\DAV\Events\CalendarObjectDeletedEvent; use OCA\DAV\Events\CalendarObjectUpdatedEvent; use OCA\DAV\Events\CalendarShareUpdatedEvent; +use OCA\DAV\Events\CalendarUpdatedEvent; use OCA\DAV\HookManager; use OCA\DAV\Listener\ActivityUpdaterListener; use OCA\DAV\Listener\CalendarContactInteractionListener; @@ -119,6 +120,7 @@ class Application extends App implements IBootstrap { $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class); $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class); $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class); + $context->registerEventListener(CalendarUpdatedEvent::class, ActivityUpdaterListener::class); $context->registerEventListener(CalendarObjectCreatedEvent::class, ActivityUpdaterListener::class); $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class); $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectReminderUpdaterListener::class); @@ -200,15 +202,6 @@ class Application extends App implements IBootstrap { $syncService->updateUser($user); }); - $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', function (GenericEvent $event) use ($container) { - /** @var Backend $backend */ - $backend = $container->query(Backend::class); - $backend->onCalendarUpdate( - $event->getArgument('calendarData'), - $event->getArgument('shares'), - $event->getArgument('propertyMutations') - ); - }); $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateShares', function (GenericEvent $event) use ($container) { /** @var Backend $backend */ $backend = $container->query(Backend::class); diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index 69924e62154..2daa03843de 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -861,14 +861,6 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription $calendarData = $this->getCalendarById($calendarId); $shares = $this->getShares($calendarId); $this->dispatcher->dispatchTyped(new CalendarUpdatedEvent((int)$calendarId, $calendarData, $shares, $mutations)); - $this->legacyDispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', new GenericEvent( - '\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', - [ - 'calendarId' => $calendarId, - 'calendarData' => $calendarData, - 'shares' => $shares, - 'propertyMutations' => $mutations, - ])); return true; }); diff --git a/apps/dav/lib/Listener/ActivityUpdaterListener.php b/apps/dav/lib/Listener/ActivityUpdaterListener.php index bd59108b4a1..30e0008b183 100644 --- a/apps/dav/lib/Listener/ActivityUpdaterListener.php +++ b/apps/dav/lib/Listener/ActivityUpdaterListener.php @@ -31,6 +31,7 @@ use OCA\DAV\Events\CalendarDeletedEvent; use OCA\DAV\Events\CalendarObjectCreatedEvent; use OCA\DAV\Events\CalendarObjectDeletedEvent; use OCA\DAV\Events\CalendarObjectUpdatedEvent; +use OCA\DAV\Events\CalendarUpdatedEvent; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use Psr\Log\LoggerInterface; @@ -67,7 +68,24 @@ class ActivityUpdaterListener implements IEventListener { 'exception' => $e, ]); } - } else if ($event instanceof CalendarDeletedEvent) { + } elseif ($event instanceof CalendarUpdatedEvent) { + try { + $this->activityBackend->onCalendarUpdate( + $event->getCalendarData(), + $event->getShares(), + $event->getMutations() + ); + + $this->logger->debug( + sprintf('Activity generated for changed calendar %d', $event->getCalendarId()) + ); + } catch (Throwable $e) { + // Any error with activities shouldn't abort the calendar update, so we just log it + $this->logger->error('Error generating activities for changed calendar: ' . $e->getMessage(), [ + 'exception' => $e, + ]); + } + } elseif ($event instanceof CalendarDeletedEvent) { try { $this->activityBackend->onCalendarDelete( $event->getCalendarData(), diff --git a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php index ec77c1f7d8c..2ac333b1526 100644 --- a/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php +++ b/apps/dav/tests/unit/CalDAV/CalDavBackendTest.php @@ -63,9 +63,11 @@ class CalDavBackendTest extends AbstractCalDavBackend { '{DAV:}displayname' => 'Unit test', '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'Calendar used for unit testing' ]); - $this->legacyDispatcher->expects($this->at(0)) - ->method('dispatch') - ->with('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar'); + $this->dispatcher->expects(self::once()) + ->method('dispatchTyped') + ->with(self::callback(function ($event) { + return $event instanceof CalendarUpdatedEvent; + })); $this->backend->updateCalendar($calendarId, $patch); $patch->commit(); $this->assertEquals(1, $this->backend->getCalendarsForUserCount(self::UNIT_TEST_USER));