feat!: Migrate TagService events to typed events

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2023-07-27 19:44:42 +02:00
parent 53392861ff
commit 9bf812ac6c
No known key found for this signature in database
GPG key ID: C400AAF20C1BB6FC
7 changed files with 152 additions and 14 deletions

View file

@ -57,6 +57,7 @@ use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Collaboration\Reference\RenderReferenceEvent;
use OCP\Collaboration\Resources\IProviderManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IPreview;
@ -110,7 +111,7 @@ class Application extends App implements IBootstrap {
$c->get(IActivityManager::class),
$c->get(ITagManager::class)->load(self::APP_ID),
$server->getUserFolder(),
$server->getEventDispatcher()
$c->get(IEventDispatcher::class),
);
});

View file

@ -26,12 +26,13 @@ namespace OCA\Files\Service;
use OCA\Files\Activity\FavoriteProvider;
use OCP\Activity\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Events\NodeAddedToFavorite;
use OCP\Files\Events\NodeRemovedFromFavorite;
use OCP\Files\Folder;
use OCP\ITags;
use OCP\IUser;
use OCP\IUserSession;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
/**
* Service class to manage tags on files.
@ -46,7 +47,7 @@ class TagService {
private $tagger;
/** @var Folder|null */
private $homeFolder;
/** @var EventDispatcherInterface */
/** @var IEventDispatcher */
private $dispatcher;
public function __construct(
@ -54,7 +55,7 @@ class TagService {
IManager $activityManager,
?ITags $tagger,
?Folder $homeFolder,
EventDispatcherInterface $dispatcher
IEventDispatcher $dispatcher,
) {
$this->userSession = $userSession;
$this->activityManager = $activityManager;
@ -120,12 +121,12 @@ class TagService {
return;
}
$eventName = $addToFavorite ? 'addFavorite' : 'removeFavorite';
$this->dispatcher->dispatch(self::class . '::' . $eventName, new GenericEvent(null, [
'userId' => $user->getUID(),
'fileId' => $fileId,
'path' => $path,
]));
if ($addToFavorite) {
$event = new NodeAddedToFavorite($user, $fileId, $path);
} else {
$event = new NodeRemovedFromFavorite($user, $fileId, $path);
}
$this->dispatcher->dispatchTyped($event);
$event = $this->activityManager->generateEvent();
try {

View file

@ -29,10 +29,10 @@ namespace OCA\Files\Tests\Service;
use OCA\Files\Service\TagService;
use OCP\Activity\IManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ITags;
use OCP\IUser;
use OCP\IUserSession;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Class TagServiceTest
@ -59,7 +59,7 @@ class TagServiceTest extends \Test\TestCase {
*/
private $root;
/** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */
/** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */
private $dispatcher;
/**
@ -90,7 +90,7 @@ class TagServiceTest extends \Test\TestCase {
->willReturn($user);
$this->root = \OC::$server->getUserFolder();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->dispatcher = $this->createMock(IEventDispatcher::class);
$this->tagger = \OC::$server->getTagManager()->load('files');
$this->tagService = $this->getTagService(['addActivity']);

View file

@ -309,7 +309,9 @@ return array(
'OCP\\Files\\Events\\FolderScannedEvent' => $baseDir . '/lib/public/Files/Events/FolderScannedEvent.php',
'OCP\\Files\\Events\\InvalidateMountCacheEvent' => $baseDir . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
'OCP\\Files\\Events\\NodeAddedToCache' => $baseDir . '/lib/public/Files/Events/NodeAddedToCache.php',
'OCP\\Files\\Events\\NodeAddedToFavorite' => $baseDir . '/lib/public/Files/Events/NodeAddedToFavorite.php',
'OCP\\Files\\Events\\NodeRemovedFromCache' => $baseDir . '/lib/public/Files/Events/NodeRemovedFromCache.php',
'OCP\\Files\\Events\\NodeRemovedFromFavorite' => $baseDir . '/lib/public/Files/Events/NodeRemovedFromFavorite.php',
'OCP\\Files\\Events\\Node\\AbstractNodeEvent' => $baseDir . '/lib/public/Files/Events/Node/AbstractNodeEvent.php',
'OCP\\Files\\Events\\Node\\AbstractNodesEvent' => $baseDir . '/lib/public/Files/Events/Node/AbstractNodesEvent.php',
'OCP\\Files\\Events\\Node\\BeforeNodeCopiedEvent' => $baseDir . '/lib/public/Files/Events/Node/BeforeNodeCopiedEvent.php',

View file

@ -342,7 +342,9 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Files\\Events\\FolderScannedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/FolderScannedEvent.php',
'OCP\\Files\\Events\\InvalidateMountCacheEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/InvalidateMountCacheEvent.php',
'OCP\\Files\\Events\\NodeAddedToCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToCache.php',
'OCP\\Files\\Events\\NodeAddedToFavorite' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeAddedToFavorite.php',
'OCP\\Files\\Events\\NodeRemovedFromCache' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeRemovedFromCache.php',
'OCP\\Files\\Events\\NodeRemovedFromFavorite' => __DIR__ . '/../../..' . '/lib/public/Files/Events/NodeRemovedFromFavorite.php',
'OCP\\Files\\Events\\Node\\AbstractNodeEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/AbstractNodeEvent.php',
'OCP\\Files\\Events\\Node\\AbstractNodesEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/AbstractNodesEvent.php',
'OCP\\Files\\Events\\Node\\BeforeNodeCopiedEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Events/Node/BeforeNodeCopiedEvent.php',

View file

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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 OCP\Files\Events;
use OCP\EventDispatcher\Event;
use OCP\IUser;
/**
* @since 28.0.0
*/
class NodeAddedToFavorite extends Event {
/**
* @since 28.0.0
*/
public function __construct(
protected IUser $user,
protected int $fileId,
protected string $path,
) {
parent::__construct();
}
/**
* @since 28.0.0
*/
public function getUser(): IUser {
return $this->user;
}
/**
* @since 28.0.0
*/
public function getFileId(): int {
return $this->fileId;
}
/**
* @since 28.0.0
*/
public function getPath(): string {
return $this->path;
}
}

View file

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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 OCP\Files\Events;
use OCP\EventDispatcher\Event;
use OCP\IUser;
/**
* @since 28.0.0
*/
class NodeRemovedFromFavorite extends Event {
/**
* @since 28.0.0
*/
public function __construct(
protected IUser $user,
protected int $fileId,
protected string $path,
) {
parent::__construct();
}
/**
* @since 28.0.0
*/
public function getUser(): IUser {
return $this->user;
}
/**
* @since 28.0.0
*/
public function getFileId(): int {
return $this->fileId;
}
/**
* @since 28.0.0
*/
public function getPath(): string {
return $this->path;
}
}