Skip to content

Commit

Permalink
chore: Move comments event handler to use proper event dispatcher
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Jun 18, 2024
1 parent af8dbc7 commit e73d3b1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
12 changes: 3 additions & 9 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Deck\Activity;
namespace OCA\Deck\Listeners;

use OCA\Deck\Activity\ActivityManager;
use OCA\Deck\Db\CardMapper;
use OCA\Deck\Db\ChangeHelper;
use OCA\Deck\Notification\NotificationHelper;
use OCP\Comments\CommentsEvent;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsEventHandler;

class CommentEventHandler implements ICommentsEventHandler {

/** @var ActivityManager */
private $activityManager;

/** @var NotificationHelper */
private $notificationHelper;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

/** @var CardMapper */
private $cardMapper;
/** @template-implements IEventListener<CommentsEvent|Event> */
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;
}
Expand All @@ -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());
}
}
4 changes: 2 additions & 2 deletions tests/unit/Activity/CommentEventHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

class CommentEventHandlerTest extends TestCase {

/** @var CommentEventHandler */
/** @var CommentEventListener */
private $commentEventHandler;
/** @var ActivityManager */
private $activityManager;
Expand All @@ -49,7 +49,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,
Expand Down

0 comments on commit e73d3b1

Please sign in to comment.