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
juliushaertl committed Jun 25, 2024
1 parent ffa8b00 commit f7563df
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 21 deletions.
2 changes: 1 addition & 1 deletion apps/comments/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion apps/comments/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
15 changes: 9 additions & 6 deletions apps/comments/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
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 {
/** @template-implements IEventListener<CommentsEvent|Event> */
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;
Expand Down
6 changes: 3 additions & 3 deletions apps/comments/tests/Unit/EventHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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() {
Expand Down
3 changes: 3 additions & 0 deletions lib/private/Comments/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +50,7 @@ public function __construct(
protected IEmojiHelper $emojiHelper,
protected IInitialStateService $initialStateService,
protected IRootFolder $rootFolder,
protected IEventDispatcher $eventDispatcher,
) {
}

Expand Down Expand Up @@ -1529,6 +1531,7 @@ private function sendEvent($eventType, IComment $comment) {
foreach ($entities as $entity) {
$entity->handle($event);
}
$this->eventDispatcher->dispatchTyped($event);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions lib/public/Comments/ICommentsEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
* Interface ICommentsEventHandler
*
* @since 11.0.0
* @deprecated 30.0.0 Register a listener for the CommentsEvent through the IEventDispatcher
*/
interface ICommentsEventHandler {
/**
* @param CommentsEvent $event
* @since 11.0.0
* @deprecated 30.0.0 Register a listener for the CommentsEvent through the IEventDispatcher
*/
public function handle(CommentsEvent $event);
}
4 changes: 4 additions & 0 deletions tests/lib/Comments/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCP\Comments\ICommentsEventHandler;
use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\IConfig;
Expand Down Expand Up @@ -91,6 +92,7 @@ protected function getManager() {
new EmojiHelper($this->connection),
$this->createMock(IInitialStateService::class),
$this->rootFolder,
$this->createMock(IEventDispatcher::class),
);
}

Expand Down Expand Up @@ -758,6 +760,7 @@ public function testDeleteCommentsExpiredAtObjectTypeAndId(): void {
new EmojiHelper($this->connection),
$this->createMock(IInitialStateService::class),
$this->rootFolder,
$this->createMock(IEventDispatcher::class)
);

// just to make sure they are really set, with correct actor data
Expand Down Expand Up @@ -804,6 +807,7 @@ public function testDeleteCommentsExpiredAtObjectType(): void {
new EmojiHelper($this->connection),
$this->createMock(IInitialStateService::class),
$this->rootFolder,
$this->createMock(IEventDispatcher::class)
);

$deleted = $manager->deleteCommentsExpiredAtObject('files');
Expand Down

0 comments on commit f7563df

Please sign in to comment.