Browse Source

chore: Move comments event handler to use proper event dispatcher

Signed-off-by: Julius Härtl <jus@bitgrid.net>
pull/45951/head
Julius Härtl 1 year ago
parent
commit
9713dd3fa9
No known key found for this signature in database GPG Key ID: 4C614C6ED2CDE6DF
  1. 2
      apps/comments/composer/composer/autoload_classmap.php
  2. 2
      apps/comments/composer/composer/autoload_static.php
  3. 19
      apps/comments/lib/AppInfo/Application.php
  4. 24
      apps/comments/lib/Listener/CommentsEventListener.php
  5. 6
      apps/comments/tests/Unit/EventHandlerTest.php
  6. 3
      lib/private/Comments/Manager.php
  7. 2
      lib/public/Comments/ICommentsEventHandler.php
  8. 4
      tests/lib/Comments/ManagerTest.php

2
apps/comments/composer/composer/autoload_classmap.php

@ -15,8 +15,8 @@ return array(
'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',

2
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',

19
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,9 +21,8 @@ use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Comments\CommentsEntityEvent;
use OCP\Comments\ICommentsManager;
use OCP\Comments\CommentsEvent;
use OCP\ISearch;
use OCP\IServerContainer;
class Application extends App implements IBootstrap {
public const APP_ID = 'comments';
@ -48,6 +46,11 @@ class Application extends App implements IBootstrap {
CommentsEntityEvent::class,
CommentsEntityEventListener::class
);
$context->registerEventListener(
CommentsEvent::class,
CommentsEventListener::class,
);
$context->registerSearchProvider(CommentsSearchProvider::class);
$context->registerInitialStateProvider(MaxAutoCompleteResultsInitialState::class);
@ -56,14 +59,6 @@ class Application extends App implements IBootstrap {
}
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);
});
}
}

24
apps/comments/lib/EventHandler.php → apps/comments/lib/Listener/CommentsEventListener.php

@ -1,28 +1,34 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 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;

6
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 @@ class EventHandlerTest extends TestCase {
->disableOriginalConstructor()
->getMock();
$this->eventHandler = new EventHandler($this->activityListener, $this->notificationListener);
$this->eventHandler = new CommentsEventListener($this->activityListener, $this->notificationListener);
}
public function testNotFiles() {

3
lib/private/Comments/Manager.php

@ -16,6 +16,7 @@ use OCP\Comments\ICommentsEventHandler;
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 @@ class Manager implements ICommentsManager {
protected IEmojiHelper $emojiHelper,
protected IInitialStateService $initialStateService,
protected IRootFolder $rootFolder,
protected IEventDispatcher $eventDispatcher,
) {
}
@ -1529,6 +1531,7 @@ class Manager implements ICommentsManager {
foreach ($entities as $entity) {
$entity->handle($event);
}
$this->eventDispatcher->dispatchTyped($event);
}
/**

2
lib/public/Comments/ICommentsEventHandler.php

@ -9,11 +9,13 @@ namespace OCP\Comments;
* 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
tests/lib/Comments/ManagerTest.php

@ -14,6 +14,7 @@ use OCP\Comments\IComment;
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;
@ -91,6 +92,7 @@ class ManagerTest extends TestCase {
new EmojiHelper($this->connection),
$this->createMock(IInitialStateService::class),
$this->rootFolder,
$this->createMock(IEventDispatcher::class),
);
}
@ -758,6 +760,7 @@ class ManagerTest extends TestCase {
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
@ -804,6 +807,7 @@ class ManagerTest extends TestCase {
new EmojiHelper($this->connection),
$this->createMock(IInitialStateService::class),
$this->rootFolder,
$this->createMock(IEventDispatcher::class)
);
$deleted = $manager->deleteCommentsExpiredAtObject('files');

Loading…
Cancel
Save