diff --git a/apps/dav/lib/Connector/Sabre/TagsPlugin.php b/apps/dav/lib/Connector/Sabre/TagsPlugin.php index f70706c1d477d..eb06fa5cef6c7 100644 --- a/apps/dav/lib/Connector/Sabre/TagsPlugin.php +++ b/apps/dav/lib/Connector/Sabre/TagsPlugin.php @@ -28,8 +28,6 @@ * */ use OCP\EventDispatcher\IEventDispatcher; -use OCP\Files\Events\NodeAddedToFavorite; -use OCP\Files\Events\NodeRemovedFromFavorite; use OCP\ITagManager; use OCP\ITags; use OCP\IUserSession; @@ -260,10 +258,8 @@ public function handleUpdateProperties($path, PropPatch $propPatch) { $propPatch->handle(self::FAVORITE_PROPERTYNAME, function ($favState) use ($node, $path) { if ((int)$favState === 1 || $favState === 'true') { $this->getTagger()->tagAs($node->getId(), self::TAG_FAVORITE); - $this->eventDispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $node->getId(), $path)); } else { $this->getTagger()->unTag($node->getId(), self::TAG_FAVORITE); - $this->eventDispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $node->getId(), $path)); } if (is_null($favState)) { diff --git a/apps/files/lib/AppInfo/Application.php b/apps/files/lib/AppInfo/Application.php index 04a1434adec02..84387983946aa 100644 --- a/apps/files/lib/AppInfo/Application.php +++ b/apps/files/lib/AppInfo/Application.php @@ -35,7 +35,6 @@ use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Collaboration\Reference\RenderReferenceEvent; use OCP\Collaboration\Resources\IProviderManager; -use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Cache\CacheEntryRemovedEvent; use OCP\Files\Events\Node\BeforeNodeCopiedEvent; use OCP\Files\Events\Node\BeforeNodeDeletedEvent; @@ -100,7 +99,6 @@ public function register(IRegistrationContext $context): void { $c->get(IActivityManager::class), $c->get(ITagManager::class)->load(self::APP_ID), $server->getUserFolder(), - $c->get(IEventDispatcher::class), ); }); diff --git a/apps/files/lib/Service/TagService.php b/apps/files/lib/Service/TagService.php index abae26f91a6c6..63c54d01fd08e 100644 --- a/apps/files/lib/Service/TagService.php +++ b/apps/files/lib/Service/TagService.php @@ -8,9 +8,6 @@ namespace OCA\Files\Service; use OCP\Activity\IManager; -use OCP\EventDispatcher\IEventDispatcher; -use OCP\Files\Events\NodeAddedToFavorite; -use OCP\Files\Events\NodeRemovedFromFavorite; use OCP\Files\Folder; use OCP\Files\NotFoundException; use OCP\ITags; @@ -26,7 +23,6 @@ public function __construct( private IManager $activityManager, private ?ITags $tagger, private ?Folder $homeFolder, - private IEventDispatcher $dispatcher, ) { } @@ -58,16 +54,10 @@ public function updateFileTags($path, $tags) { $newTags = array_diff($tags, $currentTags); foreach ($newTags as $tag) { - if ($tag === ITags::TAG_FAVORITE) { - $this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $fileId, $path)); - } $this->tagger->tagAs($fileId, $tag); } $deletedTags = array_diff($currentTags, $tags); foreach ($deletedTags as $tag) { - if ($tag === ITags::TAG_FAVORITE) { - $this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $fileId, $path)); - } $this->tagger->unTag($fileId, $tag); } diff --git a/apps/files/tests/Service/TagServiceTest.php b/apps/files/tests/Service/TagServiceTest.php index 68a9ec86cb46b..a05025e98e1ae 100644 --- a/apps/files/tests/Service/TagServiceTest.php +++ b/apps/files/tests/Service/TagServiceTest.php @@ -91,7 +91,6 @@ protected function getTagService(array $methods = []) { $this->activityManager, $this->tagger, $this->root, - $this->dispatcher, ]) ->setMethods($methods) ->getMock(); diff --git a/lib/private/TagManager.php b/lib/private/TagManager.php index f99653f2c052f..813992381fb52 100644 --- a/lib/private/TagManager.php +++ b/lib/private/TagManager.php @@ -11,6 +11,7 @@ use OCP\Db\Exception as DBException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventDispatcher; use OCP\EventDispatcher\IEventListener; use OCP\IDBConnection; use OCP\ITagManager; @@ -23,16 +24,14 @@ * @template-implements IEventListener */ class TagManager implements ITagManager, IEventListener { - private TagMapper $mapper; - private IUserSession $userSession; - private IDBConnection $connection; - private LoggerInterface $logger; - public function __construct(TagMapper $mapper, IUserSession $userSession, IDBConnection $connection, LoggerInterface $logger) { - $this->mapper = $mapper; - $this->userSession = $userSession; - $this->connection = $connection; - $this->logger = $logger; + public function __construct( + private TagMapper $mapper, + private IUserSession $userSession, + private IDBConnection $connection, + private LoggerInterface $logger, + private IEventDispatcher $dispatcher, + ) { } /** @@ -57,7 +56,7 @@ public function load($type, $defaultTags = [], $includeShared = false, $userId = } $userId = $this->userSession->getUser()->getUId(); } - return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $defaultTags); + return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $defaultTags, $this->dispatcher, $this->userSession); } /** diff --git a/lib/private/Tags.php b/lib/private/Tags.php index d59c1bd692853..cae42bd51d77e 100644 --- a/lib/private/Tags.php +++ b/lib/private/Tags.php @@ -11,8 +11,12 @@ use OC\Tagging\TagMapper; use OCP\DB\Exception; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Events\NodeAddedToFavorite; +use OCP\Files\Events\NodeRemovedFromFavorite; use OCP\IDBConnection; use OCP\ITags; +use OCP\IUserSession; use OCP\Share_Backend; use Psr\Log\LoggerInterface; @@ -21,10 +25,6 @@ class Tags implements ITags { * Used for storing objectid/categoryname pairs while rescanning. */ private static array $relations = []; - private string $type; - private string $user; - private IDBConnection $db; - private LoggerInterface $logger; private array $tags = []; /** @@ -38,11 +38,6 @@ class Tags implements ITags { */ private array $owners = []; - /** - * The Mapper we are using to communicate our Tag objects to the database. - */ - private TagMapper $mapper; - /** * The sharing backend for objects of $this->type. Required if * $this->includeShared === true to determine ownership of items. @@ -62,14 +57,18 @@ class Tags implements ITags { * * since 20.0.0 $includeShared isn't used anymore */ - public function __construct(TagMapper $mapper, string $user, string $type, LoggerInterface $logger, IDBConnection $connection, array $defaultTags = []) { - $this->mapper = $mapper; - $this->user = $user; - $this->type = $type; + public function __construct( + private TagMapper $mapper, + private string $user, + private string $type, + private LoggerInterface $logger, + private IDBConnection $db, + array $defaultTags = [], + private IEventDispatcher $dispatcher, + private IUserSession $userSession, + ) { $this->owners = [$this->user]; $this->tags = $this->mapper->loadTags($this->owners, $this->type); - $this->db = $connection; - $this->logger = $logger; if (count($defaultTags) > 0 && count($this->tags) === 0) { $this->addMultiple($defaultTags, true); @@ -502,7 +501,7 @@ public function removeFromFavorites($objid) { * @param string $tag The id or name of the tag * @return boolean Returns false on error. */ - public function tagAs($objid, $tag) { + public function tagAs($objid, $tag, string $path = '') { if (is_string($tag) && !is_numeric($tag)) { $tag = trim($tag); if ($tag === '') { @@ -532,6 +531,9 @@ public function tagAs($objid, $tag) { ]); return false; } + if ($tag === ITags::TAG_FAVORITE) { + $this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $objid, $path)); + } return true; } @@ -542,7 +544,7 @@ public function tagAs($objid, $tag) { * @param string $tag The id or name of the tag * @return boolean */ - public function unTag($objid, $tag) { + public function unTag($objid, $tag, string $path = '') { if (is_string($tag) && !is_numeric($tag)) { $tag = trim($tag); if ($tag === '') { @@ -569,6 +571,9 @@ public function unTag($objid, $tag) { ]); return false; } + if ($tag === ITags::TAG_FAVORITE) { + $this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $objid, $path)); + } return true; } diff --git a/tests/lib/TagsTest.php b/tests/lib/TagsTest.php index 1876897095486..7483ed15e6b14 100644 --- a/tests/lib/TagsTest.php +++ b/tests/lib/TagsTest.php @@ -7,6 +7,7 @@ namespace Test; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IDBConnection; use OCP\IUser; use OCP\IUserSession; @@ -48,7 +49,7 @@ protected function setUp(): void { $this->objectType = $this->getUniqueID('type_'); $this->tagMapper = new \OC\Tagging\TagMapper(\OC::$server->get(IDBConnection::class)); - $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class)); + $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->get(IDBConnection::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(IEventDispatcher::class)); } protected function tearDown(): void { @@ -65,7 +66,7 @@ public function testTagManagerWithoutUserReturnsNull(): void { ->expects($this->any()) ->method('getUser') ->willReturn(null); - $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection(), \OC::$server->get(LoggerInterface::class)); + $this->tagMgr = new \OC\TagManager($this->tagMapper, $this->userSession, \OC::$server->getDatabaseConnection(), \OC::$server->get(LoggerInterface::class), \OC::$server->get(IEventDispatcher::class)); $this->assertNull($this->tagMgr->load($this->objectType)); }