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 18, 2024
1 parent 250bb12 commit 6a16461
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 17 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,49 @@
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
* @copyright Copyright (c) 2024 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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 {
class CommentsEventListener implements IEventListener {

Check failure

Code scanning / Psalm

MissingTemplateParam Error

OCA\Comments\Listener\CommentsEventListener has missing template params when extending OCP\EventDispatcher\IEventListener, expecting 1

Check failure on line 35 in apps/comments/lib/Listener/CommentsEventListener.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

MissingTemplateParam

apps/comments/lib/Listener/CommentsEventListener.php:35:40: MissingTemplateParam: OCA\Comments\Listener\CommentsEventListener has missing template params when extending OCP\EventDispatcher\IEventListener, expecting 1 (see https://psalm.dev/182)
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
* @depreacted 30.0.0 Register a listener for the CommentsEvent through the IEventDispatcher
*/
interface ICommentsEventHandler {

Check failure on line 14 in lib/public/Comments/ICommentsEventHandler.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/Comments/ICommentsEventHandler.php:14:1: InvalidDocblock: Typo in @deprecated for classes/interfaces in OCP. (see https://psalm.dev/008)
/**
* @param CommentsEvent $event
* @since 11.0.0
* @depreacted 30.0.0 Register a listener for the CommentsEvent through the IEventDispatcher
*/
public function handle(CommentsEvent $event);

Check failure on line 20 in lib/public/Comments/ICommentsEventHandler.php

View workflow job for this annotation

GitHub Actions / static-code-analysis-ocp

InvalidDocblock

lib/public/Comments/ICommentsEventHandler.php:20:2: InvalidDocblock: Typo in @deprecated for method in OCP. (see https://psalm.dev/008)
}

0 comments on commit 6a16461

Please sign in to comment.