From 261e6d0362d6440f01924386b9883c8e231b02f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 18 Jun 2024 14:23:51 +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 --- lib/AppInfo/Application.php | 12 +--- .../CommentEventListener.php} | 55 +++++++------------ .../CommentEventListenerTest.php} | 9 +-- 3 files changed, 29 insertions(+), 47 deletions(-) rename lib/{Activity/CommentEventHandler.php => Listeners/CommentEventListener.php} (51%) rename tests/unit/{Activity/CommentEventHandlerTest.php => Listeners/CommentEventListenerTest.php} (94%) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 469e407f7..b2c0b83eb 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -9,7 +9,6 @@ use Closure; use Exception; use OCA\Circles\Events\CircleDestroyedEvent; -use OCA\Deck\Activity\CommentEventHandler; use OCA\Deck\Capabilities; use OCA\Deck\Collaboration\Resources\ResourceProvider; use OCA\Deck\Collaboration\Resources\ResourceProviderCard; @@ -28,6 +27,7 @@ use OCA\Deck\Event\SessionClosedEvent; use OCA\Deck\Event\SessionCreatedEvent; use OCA\Deck\Listeners\BeforeTemplateRenderedListener; +use OCA\Deck\Listeners\CommentEventListener; use OCA\Deck\Listeners\FullTextSearchEventListener; use OCA\Deck\Listeners\LiveUpdateListener; use OCA\Deck\Listeners\ParticipantCleanupListener; @@ -56,7 +56,7 @@ use OCP\Collaboration\Resources\IProviderManager; use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent; use OCP\Comments\CommentsEntityEvent; -use OCP\Comments\ICommentsManager; +use OCP\Comments\CommentsEvent; use OCP\EventDispatcher\IEventDispatcher; use OCP\Group\Events\GroupDeletedEvent; use OCP\IConfig; @@ -91,7 +91,6 @@ public function __construct(array $urlParams = []) { public function boot(IBootContext $context): void { $context->injectFn(Closure::fromCallable([$this, 'registerCommentsEntity'])); - $context->injectFn(Closure::fromCallable([$this, 'registerCommentsEventHandler'])); $context->injectFn(Closure::fromCallable([$this, 'registerCollaborationResources'])); $context->injectFn(function (IManager $shareManager) { @@ -141,6 +140,7 @@ public function register(IRegistrationContext $context): void { $context->registerEventListener(AclCreatedEvent::class, FullTextSearchEventListener::class); $context->registerEventListener(AclUpdatedEvent::class, FullTextSearchEventListener::class); $context->registerEventListener(AclDeletedEvent::class, FullTextSearchEventListener::class); + $context->registerEventListener(CommentsEvent::class, CommentEventListener::class); // Handling cache invalidation for collections $context->registerEventListener(AclCreatedEvent::class, ResourceListener::class); @@ -184,12 +184,6 @@ public function registerCommentsEntity(IEventDispatcher $eventDispatcher): void }); } - protected function registerCommentsEventHandler(ICommentsManager $commentsManager): void { - $commentsManager->registerEventHandler(function () { - return $this->getContainer()->query(CommentEventHandler::class); - }); - } - protected function registerCollaborationResources(IProviderManager $resourceManager): void { $resourceManager->registerResourceProvider(ResourceProvider::class); $resourceManager->registerResourceProvider(ResourceProviderCard::class); diff --git a/lib/Activity/CommentEventHandler.php b/lib/Listeners/CommentEventListener.php similarity index 51% rename from lib/Activity/CommentEventHandler.php rename to lib/Listeners/CommentEventListener.php index 03e0d166e..626764aa2 100644 --- a/lib/Activity/CommentEventHandler.php +++ b/lib/Listeners/CommentEventListener.php @@ -1,43 +1,37 @@ */ +class CommentEventListener implements IEventListener { - /** @var ChangeHelper */ - private $changeHelper; - - public function __construct(ActivityManager $activityManager, NotificationHelper $notificationHelper, CardMapper $cardMapper, ChangeHelper $changeHelper) { - $this->notificationHelper = $notificationHelper; - $this->activityManager = $activityManager; - $this->cardMapper = $cardMapper; - $this->changeHelper = $changeHelper; + public function __construct( + private ActivityManager $activityManager, + private NotificationHelper $notificationHelper, + private CardMapper $cardMapper, + private ChangeHelper $changeHelper, + ) { } - /** - * @param CommentsEvent $event - */ - public function handle(CommentsEvent $event) { + public function handle(Event $event): void { + if (!$event instanceof CommentsEvent) { + return; + } + if ($event->getComment()->getObjectType() !== 'deckCard') { return; } @@ -61,20 +55,13 @@ public function handle(CommentsEvent $event) { } } - /** - * @param CommentsEvent $event - */ - private function activityHandler(CommentsEvent $event) { - /** @var IComment $comment */ + private function activityHandler(CommentsEvent $event): void { $comment = $event->getComment(); $card = $this->cardMapper->find($comment->getObjectId()); $this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $card, ActivityManager::SUBJECT_CARD_COMMENT_CREATE, ['comment' => $comment]); } - /** - * @param CommentsEvent $event - */ - private function notificationHandler(CommentsEvent $event) { + private function notificationHandler(CommentsEvent $event): void { $this->notificationHelper->sendMention($event->getComment()); } } diff --git a/tests/unit/Activity/CommentEventHandlerTest.php b/tests/unit/Listeners/CommentEventListenerTest.php similarity index 94% rename from tests/unit/Activity/CommentEventHandlerTest.php rename to tests/unit/Listeners/CommentEventListenerTest.php index 07a88e1da..90b298cdd 100644 --- a/tests/unit/Activity/CommentEventHandlerTest.php +++ b/tests/unit/Listeners/CommentEventListenerTest.php @@ -21,8 +21,9 @@ * */ -namespace OCA\Deck\Activity; +namespace OCA\Deck\Listeners; +use OCA\Deck\Activity\ActivityManager; use OCA\Deck\Db\Card; use OCA\Deck\Db\CardMapper; use OCA\Deck\Db\ChangeHelper; @@ -31,9 +32,9 @@ use OCP\Comments\IComment; use PHPUnit\Framework\TestCase; -class CommentEventHandlerTest extends TestCase { +class CommentEventListenerTest extends TestCase { - /** @var CommentEventHandler */ + /** @var CommentEventListener */ private $commentEventHandler; /** @var ActivityManager */ private $activityManager; @@ -49,7 +50,7 @@ public function setUp(): void { $this->notificationHelper = $this->createMock(NotificationHelper::class); $this->cardMapper = $this->createMock(CardMapper::class); $this->changeHelper = $this->createMock(ChangeHelper::class); - $this->commentEventHandler = new CommentEventHandler( + $this->commentEventHandler = new CommentEventListener( $this->activityManager, $this->notificationHelper, $this->cardMapper,