Skip to content

Commit

Permalink
Merge pull request #39304 from nextcloud/bugfix/noid/typed-event-comm…
Browse files Browse the repository at this point in the history
…ents-entity

fix(comments): Emit CommentsEntityEvent as typed event
  • Loading branch information
nickvergessen authored Jul 19, 2023
2 parents 8b3b5ff + c3bc575 commit 489b480
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion apps/comments/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function register(IRegistrationContext $context): void {
LoadSidebarScripts::class
);
$context->registerEventListener(
CommentsEntityEvent::EVENT_ENTITY,
CommentsEntityEvent::class,
CommentsEntityEventListener::class
);
$context->registerSearchProvider(CommentsSearchProvider::class);
Expand Down
9 changes: 5 additions & 4 deletions apps/dav/lib/Comments/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

use OCP\Comments\CommentsEntityEvent;
use OCP\Comments\ICommentsManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUserManager;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotAuthenticated;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\ICollection;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class RootCollection implements ICollection {
/** @var EntityTypeCollection[]|null */
Expand All @@ -43,13 +43,13 @@ class RootCollection implements ICollection {
protected LoggerInterface $logger;
protected IUserManager $userManager;
protected IUserSession $userSession;
protected EventDispatcherInterface $dispatcher;
protected IEventDispatcher $dispatcher;

public function __construct(
ICommentsManager $commentsManager,
IUserManager $userManager,
IUserSession $userSession,
EventDispatcherInterface $dispatcher,
IEventDispatcher $dispatcher,
LoggerInterface $logger) {
$this->commentsManager = $commentsManager;
$this->logger = $logger;
Expand All @@ -74,7 +74,8 @@ protected function initCollections() {
throw new NotAuthenticated();
}

$event = new CommentsEntityEvent(CommentsEntityEvent::EVENT_ENTITY);
$event = new CommentsEntityEvent();
$this->dispatcher->dispatchTyped($event);
$this->dispatcher->dispatch(CommentsEntityEvent::EVENT_ENTITY, $event);

$this->entityTypeCollections = [];
Expand Down
2 changes: 1 addition & 1 deletion apps/dav/lib/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function __construct() {
\OC::$server->getCommentsManager(),
$userManager,
\OC::$server->getUserSession(),
\OC::$server->getEventDispatcher(),
$dispatcher,
$logger
);

Expand Down
16 changes: 6 additions & 10 deletions apps/dav/tests/unit/Comments/RootCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
namespace OCA\DAV\Tests\unit\Comments;

use OC\EventDispatcher\EventDispatcher;
use OC\EventDispatcher\SymfonyAdapter;
use OCA\DAV\Comments\EntityTypeCollection as EntityTypeCollectionImplementation;
use OCP\Comments\CommentsEntityEvent;
use OCP\Comments\ICommentsManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class RootCollectionTest extends \Test\TestCase {

Expand All @@ -48,7 +47,7 @@ class RootCollectionTest extends \Test\TestCase {
protected $collection;
/** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject */
protected $userSession;
/** @var EventDispatcherInterface */
/** @var IEventDispatcher */
protected $dispatcher;
/** @var \OCP\IUser|\PHPUnit\Framework\MockObject\MockObject */
protected $user;
Expand All @@ -72,12 +71,9 @@ protected function setUp(): void {
$this->logger = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->dispatcher = new SymfonyAdapter(
new EventDispatcher(
new \Symfony\Component\EventDispatcher\EventDispatcher(),
\OC::$server,
$this->logger
),
$this->dispatcher = new EventDispatcher(
new \Symfony\Component\EventDispatcher\EventDispatcher(),
\OC::$server,
$this->logger
);

Expand All @@ -99,7 +95,7 @@ protected function prepareForInitCollections() {
->method('getUser')
->willReturn($this->user);

$this->dispatcher->addListener(CommentsEntityEvent::EVENT_ENTITY, function (CommentsEntityEvent $event): void {
$this->dispatcher->addListener(CommentsEntityEvent::class, function (CommentsEntityEvent $event): void {
$event->addEntityCollection('files', function () {
return true;
});
Expand Down
10 changes: 4 additions & 6 deletions lib/public/Comments/CommentsEntityEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,24 @@
* Class CommentsEntityEvent
*
* @since 9.1.0
* @since 28.0.0 Dispatched as a typed event
*/
class CommentsEntityEvent extends Event {
/**
* @deprecated 22.0.0
* @deprecated 22.0.0 - Listen to the typed event instead.
*/
public const EVENT_ENTITY = 'OCP\Comments\ICommentsManager::registerEntity';

/** @var string */
protected $event;
/** @var \Closure[] */
protected $collections;

/**
* DispatcherEvent constructor.
*
* @param string $event
* @since 9.1.0
*/
public function __construct($event) {
$this->event = $event;
public function __construct() {
parent::__construct();
$this->collections = [];
}

Expand Down

0 comments on commit 489b480

Please sign in to comment.