From 6a164616d7ef74d574ef8a6cabe8872701c82586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 18 Jun 2024 14:11:32 +0200 Subject: [PATCH] chore: Move comments event handler to use proper event dispatcher MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../composer/composer/autoload_classmap.php | 2 +- .../composer/composer/autoload_static.php | 2 +- apps/comments/lib/AppInfo/Application.php | 15 +++++---- .../CommentsEventListener.php} | 33 +++++++++++++++---- apps/comments/tests/Unit/EventHandlerTest.php | 6 ++-- lib/private/Comments/Manager.php | 3 ++ lib/public/Comments/ICommentsEventHandler.php | 2 ++ 7 files changed, 46 insertions(+), 17 deletions(-) rename apps/comments/lib/{EventHandler.php => Listener/CommentsEventListener.php} (50%) diff --git a/apps/comments/composer/composer/autoload_classmap.php b/apps/comments/composer/composer/autoload_classmap.php index 61def2ecc46fd..22f95a934d760 100644 --- a/apps/comments/composer/composer/autoload_classmap.php +++ b/apps/comments/composer/composer/autoload_classmap.php @@ -15,8 +15,8 @@ 'OCA\\Comments\\Capabilities' => $baseDir . '/../lib/Capabilities.php', 'OCA\\Comments\\Collaboration\\CommentersSorter' => $baseDir . '/../lib/Collaboration/CommentersSorter.php', 'OCA\\Comments\\Controller\\NotificationsController' => $baseDir . '/../lib/Controller/NotificationsController.php', - 'OCA\\Comments\\EventHandler' => $baseDir . '/../lib/EventHandler.php', 'OCA\\Comments\\Listener\\CommentsEntityEventListener' => $baseDir . '/../lib/Listener/CommentsEntityEventListener.php', + 'OCA\\Comments\\Listener\\CommentsEventListener' => $baseDir . '/../lib/Listener/CommentsEventListener.php', 'OCA\\Comments\\Listener\\LoadAdditionalScripts' => $baseDir . '/../lib/Listener/LoadAdditionalScripts.php', 'OCA\\Comments\\Listener\\LoadSidebarScripts' => $baseDir . '/../lib/Listener/LoadSidebarScripts.php', 'OCA\\Comments\\MaxAutoCompleteResultsInitialState' => $baseDir . '/../lib/MaxAutoCompleteResultsInitialState.php', diff --git a/apps/comments/composer/composer/autoload_static.php b/apps/comments/composer/composer/autoload_static.php index 091dfce5931ea..7e553f8e2e2f3 100644 --- a/apps/comments/composer/composer/autoload_static.php +++ b/apps/comments/composer/composer/autoload_static.php @@ -30,8 +30,8 @@ class ComposerStaticInitComments 'OCA\\Comments\\Capabilities' => __DIR__ . '/..' . '/../lib/Capabilities.php', 'OCA\\Comments\\Collaboration\\CommentersSorter' => __DIR__ . '/..' . '/../lib/Collaboration/CommentersSorter.php', 'OCA\\Comments\\Controller\\NotificationsController' => __DIR__ . '/..' . '/../lib/Controller/NotificationsController.php', - 'OCA\\Comments\\EventHandler' => __DIR__ . '/..' . '/../lib/EventHandler.php', 'OCA\\Comments\\Listener\\CommentsEntityEventListener' => __DIR__ . '/..' . '/../lib/Listener/CommentsEntityEventListener.php', + 'OCA\\Comments\\Listener\\CommentsEventListener' => __DIR__ . '/..' . '/../lib/Listener/CommentsEventListener.php', 'OCA\\Comments\\Listener\\LoadAdditionalScripts' => __DIR__ . '/..' . '/../lib/Listener/LoadAdditionalScripts.php', 'OCA\\Comments\\Listener\\LoadSidebarScripts' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarScripts.php', 'OCA\\Comments\\MaxAutoCompleteResultsInitialState' => __DIR__ . '/..' . '/../lib/MaxAutoCompleteResultsInitialState.php', diff --git a/apps/comments/lib/AppInfo/Application.php b/apps/comments/lib/AppInfo/Application.php index 8951387b362b7..7d9210eec78a3 100644 --- a/apps/comments/lib/AppInfo/Application.php +++ b/apps/comments/lib/AppInfo/Application.php @@ -5,10 +5,9 @@ */ namespace OCA\Comments\AppInfo; -use Closure; use OCA\Comments\Capabilities; -use OCA\Comments\EventHandler; use OCA\Comments\Listener\CommentsEntityEventListener; +use OCA\Comments\Listener\CommentsEventListener; use OCA\Comments\Listener\LoadAdditionalScripts; use OCA\Comments\Listener\LoadSidebarScripts; use OCA\Comments\MaxAutoCompleteResultsInitialState; @@ -22,6 +21,7 @@ use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Comments\CommentsEntityEvent; +use OCP\Comments\CommentsEvent; use OCP\Comments\ICommentsManager; use OCP\ISearch; use OCP\IServerContainer; @@ -48,6 +48,11 @@ public function register(IRegistrationContext $context): void { CommentsEntityEvent::class, CommentsEntityEventListener::class ); + $context->registerEventListener( + CommentsEvent::class, + CommentsEventListener::class, + ); + $context->registerSearchProvider(CommentsSearchProvider::class); $context->registerInitialStateProvider(MaxAutoCompleteResultsInitialState::class); @@ -56,14 +61,12 @@ public function register(IRegistrationContext $context): void { } public function boot(IBootContext $context): void { - $context->injectFn(Closure::fromCallable([$this, 'registerCommentsEventHandler'])); - $context->getServerContainer()->get(ISearch::class)->registerProvider(LegacyProvider::class, ['apps' => ['files']]); } protected function registerCommentsEventHandler(IServerContainer $container): void { - $container->get(ICommentsManager::class)->registerEventHandler(function (): EventHandler { - return $this->getContainer()->get(EventHandler::class); + $container->get(ICommentsManager::class)->registerEventHandler(function (): CommentsEventListener { + return $this->getContainer()->get(CommentsEventListener::class); }); } } diff --git a/apps/comments/lib/EventHandler.php b/apps/comments/lib/Listener/CommentsEventListener.php similarity index 50% rename from apps/comments/lib/EventHandler.php rename to apps/comments/lib/Listener/CommentsEventListener.php index 3ad76941444c2..bc22064e5f0a8 100644 --- a/apps/comments/lib/EventHandler.php +++ b/apps/comments/lib/Listener/CommentsEventListener.php @@ -1,28 +1,49 @@ + * + * @author Julius Härtl + * + * @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\Comments; +namespace OCA\Comments\Listener; use OCA\Comments\Activity\Listener as ActivityListener; use OCA\Comments\Notification\Listener as NotificationListener; use OCP\Comments\CommentsEvent; -use OCP\Comments\ICommentsEventHandler; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; /** * Class EventHandler * * @package OCA\Comments */ -class EventHandler implements ICommentsEventHandler { +class CommentsEventListener implements IEventListener { public function __construct( private ActivityListener $activityListener, private NotificationListener $notificationListener, ) { } - public function handle(CommentsEvent $event): void { + public function handle(Event $event): void { + if (!$event instanceof CommentsEvent) { + return; + } + if ($event->getComment()->getObjectType() !== 'files') { // this is a 'files'-specific Handler return; diff --git a/apps/comments/tests/Unit/EventHandlerTest.php b/apps/comments/tests/Unit/EventHandlerTest.php index 6c3277a60b2d2..fc7890440f338 100644 --- a/apps/comments/tests/Unit/EventHandlerTest.php +++ b/apps/comments/tests/Unit/EventHandlerTest.php @@ -6,14 +6,14 @@ namespace OCA\Comments\Tests\Unit\Notification; use OCA\Comments\Activity\Listener as ActivityListener; -use OCA\Comments\EventHandler; +use OCA\Comments\Listener\CommentsEventListener; use OCA\Comments\Notification\Listener as NotificationListener; use OCP\Comments\CommentsEvent; use OCP\Comments\IComment; use Test\TestCase; class EventHandlerTest extends TestCase { - /** @var EventHandler */ + /** @var CommentsEventListener */ protected $eventHandler; /** @var ActivityListener|\PHPUnit\Framework\MockObject\MockObject */ @@ -33,7 +33,7 @@ protected function setUp(): void { ->disableOriginalConstructor() ->getMock(); - $this->eventHandler = new EventHandler($this->activityListener, $this->notificationListener); + $this->eventHandler = new CommentsEventListener($this->activityListener, $this->notificationListener); } public function testNotFiles() { diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index e3d40f2d96510..21ff5322f231b 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -16,6 +16,7 @@ use OCP\Comments\ICommentsManager; use OCP\Comments\NotFoundException; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\FileInfo; use OCP\Files\Folder; use OCP\Files\IRootFolder; @@ -49,6 +50,7 @@ public function __construct( protected IEmojiHelper $emojiHelper, protected IInitialStateService $initialStateService, protected IRootFolder $rootFolder, + protected IEventDispatcher $eventDispatcher, ) { } @@ -1529,6 +1531,7 @@ private function sendEvent($eventType, IComment $comment) { foreach ($entities as $entity) { $entity->handle($event); } + $this->eventDispatcher->dispatchTyped($event); } /** diff --git a/lib/public/Comments/ICommentsEventHandler.php b/lib/public/Comments/ICommentsEventHandler.php index af36e051b5f6e..54d941793894f 100644 --- a/lib/public/Comments/ICommentsEventHandler.php +++ b/lib/public/Comments/ICommentsEventHandler.php @@ -9,11 +9,13 @@ * Interface ICommentsEventHandler * * @since 11.0.0 + * @depreacted 30.0.0 Register a listener for the CommentsEvent through the IEventDispatcher */ interface ICommentsEventHandler { /** * @param CommentsEvent $event * @since 11.0.0 + * @depreacted 30.0.0 Register a listener for the CommentsEvent through the IEventDispatcher */ public function handle(CommentsEvent $event); }