Skip to content

Commit 92ec240

Browse files
committed
chore: Move comments event handler to use proper event dispatcher
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 8ceed06 commit 92ec240

3 files changed

Lines changed: 26 additions & 45 deletions

File tree

lib/AppInfo/Application.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Closure;
1010
use Exception;
1111
use OCA\Circles\Events\CircleDestroyedEvent;
12-
use OCA\Deck\Activity\CommentEventHandler;
1312
use OCA\Deck\Capabilities;
1413
use OCA\Deck\Collaboration\Resources\ResourceProvider;
1514
use OCA\Deck\Collaboration\Resources\ResourceProviderCard;
@@ -28,6 +27,7 @@
2827
use OCA\Deck\Event\SessionClosedEvent;
2928
use OCA\Deck\Event\SessionCreatedEvent;
3029
use OCA\Deck\Listeners\BeforeTemplateRenderedListener;
30+
use OCA\Deck\Listeners\CommentEventListener;
3131
use OCA\Deck\Listeners\FullTextSearchEventListener;
3232
use OCA\Deck\Listeners\LiveUpdateListener;
3333
use OCA\Deck\Listeners\ParticipantCleanupListener;
@@ -56,7 +56,7 @@
5656
use OCP\Collaboration\Resources\IProviderManager;
5757
use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent;
5858
use OCP\Comments\CommentsEntityEvent;
59-
use OCP\Comments\ICommentsManager;
59+
use OCP\Comments\CommentsEvent;
6060
use OCP\EventDispatcher\IEventDispatcher;
6161
use OCP\Group\Events\GroupDeletedEvent;
6262
use OCP\IConfig;
@@ -91,7 +91,6 @@ public function __construct(array $urlParams = []) {
9191

9292
public function boot(IBootContext $context): void {
9393
$context->injectFn(Closure::fromCallable([$this, 'registerCommentsEntity']));
94-
$context->injectFn(Closure::fromCallable([$this, 'registerCommentsEventHandler']));
9594
$context->injectFn(Closure::fromCallable([$this, 'registerCollaborationResources']));
9695

9796
$context->injectFn(function (IManager $shareManager) {
@@ -141,6 +140,7 @@ public function register(IRegistrationContext $context): void {
141140
$context->registerEventListener(AclCreatedEvent::class, FullTextSearchEventListener::class);
142141
$context->registerEventListener(AclUpdatedEvent::class, FullTextSearchEventListener::class);
143142
$context->registerEventListener(AclDeletedEvent::class, FullTextSearchEventListener::class);
143+
$context->registerEventListener(CommentsEvent::class, CommentEventListener::class);
144144

145145
// Handling cache invalidation for collections
146146
$context->registerEventListener(AclCreatedEvent::class, ResourceListener::class);
@@ -184,12 +184,6 @@ public function registerCommentsEntity(IEventDispatcher $eventDispatcher): void
184184
});
185185
}
186186

187-
protected function registerCommentsEventHandler(ICommentsManager $commentsManager): void {
188-
$commentsManager->registerEventHandler(function () {
189-
return $this->getContainer()->query(CommentEventHandler::class);
190-
});
191-
}
192-
193187
protected function registerCollaborationResources(IProviderManager $resourceManager): void {
194188
$resourceManager->registerResourceProvider(ResourceProvider::class);
195189
$resourceManager->registerResourceProvider(ResourceProviderCard::class);
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,37 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/**
35
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
46
* SPDX-License-Identifier: AGPL-3.0-or-later
57
*/
68

7-
namespace OCA\Deck\Activity;
9+
namespace OCA\Deck\Listeners;
810

11+
use OCA\Deck\Activity\ActivityManager;
912
use OCA\Deck\Db\CardMapper;
1013
use OCA\Deck\Db\ChangeHelper;
1114
use OCA\Deck\Notification\NotificationHelper;
1215
use OCP\Comments\CommentsEvent;
13-
use OCP\Comments\IComment;
14-
use OCP\Comments\ICommentsEventHandler;
15-
16-
class CommentEventHandler implements ICommentsEventHandler {
17-
18-
/** @var ActivityManager */
19-
private $activityManager;
20-
21-
/** @var NotificationHelper */
22-
private $notificationHelper;
16+
use OCP\EventDispatcher\Event;
17+
use OCP\EventDispatcher\IEventListener;
2318

24-
/** @var CardMapper */
25-
private $cardMapper;
19+
/** @template-implements IEventListener<CommentsEvent|Event> */
20+
class CommentEventListener implements IEventListener {
2621

27-
/** @var ChangeHelper */
28-
private $changeHelper;
29-
30-
public function __construct(ActivityManager $activityManager, NotificationHelper $notificationHelper, CardMapper $cardMapper, ChangeHelper $changeHelper) {
31-
$this->notificationHelper = $notificationHelper;
32-
$this->activityManager = $activityManager;
33-
$this->cardMapper = $cardMapper;
34-
$this->changeHelper = $changeHelper;
22+
public function __construct(
23+
private ActivityManager $activityManager,
24+
private NotificationHelper $notificationHelper,
25+
private CardMapper $cardMapper,
26+
private ChangeHelper $changeHelper,
27+
) {
3528
}
3629

37-
/**
38-
* @param CommentsEvent $event
39-
*/
40-
public function handle(CommentsEvent $event) {
30+
public function handle(Event $event): void {
31+
if (!$event instanceof CommentsEvent) {
32+
return;
33+
}
34+
4135
if ($event->getComment()->getObjectType() !== 'deckCard') {
4236
return;
4337
}
@@ -61,20 +55,13 @@ public function handle(CommentsEvent $event) {
6155
}
6256
}
6357

64-
/**
65-
* @param CommentsEvent $event
66-
*/
67-
private function activityHandler(CommentsEvent $event) {
68-
/** @var IComment $comment */
58+
private function activityHandler(CommentsEvent $event): void {
6959
$comment = $event->getComment();
7060
$card = $this->cardMapper->find($comment->getObjectId());
7161
$this->activityManager->triggerEvent(ActivityManager::DECK_OBJECT_CARD, $card, ActivityManager::SUBJECT_CARD_COMMENT_CREATE, ['comment' => $comment]);
7262
}
7363

74-
/**
75-
* @param CommentsEvent $event
76-
*/
77-
private function notificationHandler(CommentsEvent $event) {
64+
private function notificationHandler(CommentsEvent $event): void {
7865
$this->notificationHelper->sendMention($event->getComment());
7966
}
8067
}

tests/unit/Activity/CommentEventHandlerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
class CommentEventHandlerTest extends TestCase {
3535

36-
/** @var CommentEventHandler */
36+
/** @var CommentEventListener */
3737
private $commentEventHandler;
3838
/** @var ActivityManager */
3939
private $activityManager;
@@ -49,7 +49,7 @@ public function setUp(): void {
4949
$this->notificationHelper = $this->createMock(NotificationHelper::class);
5050
$this->cardMapper = $this->createMock(CardMapper::class);
5151
$this->changeHelper = $this->createMock(ChangeHelper::class);
52-
$this->commentEventHandler = new CommentEventHandler(
52+
$this->commentEventHandler = new CommentEventListener(
5353
$this->activityManager,
5454
$this->notificationHelper,
5555
$this->cardMapper,

0 commit comments

Comments
 (0)