From c853d8f96ec2ceebd1b4c1068771d0e0c15a4d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 11 May 2026 12:19:36 +0200 Subject: [PATCH] fix(files): Use proper DI for TagService class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/files/lib/AppInfo/Application.php | 21 --------------------- apps/files/lib/Service/TagService.php | 14 ++++++++++++-- apps/files/tests/Service/TagServiceTest.php | 13 +++++++++++-- 3 files changed, 23 insertions(+), 25 deletions(-) diff --git a/apps/files/lib/AppInfo/Application.php b/apps/files/lib/AppInfo/Application.php index ae58e9b73f1..54e6b2c678a 100644 --- a/apps/files/lib/AppInfo/Application.php +++ b/apps/files/lib/AppInfo/Application.php @@ -27,8 +27,6 @@ use OCA\Files\Listener\SyncLivePhotosListener; use OCA\Files\Listener\UserFirstTimeLoggedInListener; use OCA\Files\Notification\Notifier; use OCA\Files\Search\FilesSearchProvider; -use OCA\Files\Service\TagService; -use OCP\Activity\IManager as IActivityManager; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; @@ -42,12 +40,8 @@ use OCP\Files\Events\Node\BeforeNodeRenamedEvent; use OCP\Files\Events\Node\NodeCopiedEvent; use OCP\Files\Events\NodeAddedToFavorite; use OCP\Files\Events\NodeRemovedFromFavorite; -use OCP\IServerContainer; -use OCP\ITagManager; -use OCP\IUserSession; use OCP\User\Events\UserFirstTimeLoggedInEvent; use OCP\Util; -use Psr\Container\ContainerInterface; class Application extends App implements IBootstrap { public const APP_ID = 'files'; @@ -58,21 +52,6 @@ class Application extends App implements IBootstrap { #[\Override] public function register(IRegistrationContext $context): void { - /** - * Services - */ - $context->registerService(TagService::class, function (ContainerInterface $c) { - /** @var IServerContainer $server */ - $server = $c->get(IServerContainer::class); - - return new TagService( - $c->get(IUserSession::class), - $c->get(IActivityManager::class), - $c->get(ITagManager::class)->load(self::APP_ID), - $server->getUserFolder(), - ); - }); - /* * Register capabilities */ diff --git a/apps/files/lib/Service/TagService.php b/apps/files/lib/Service/TagService.php index 95176ce95fa..9e5661eef6f 100644 --- a/apps/files/lib/Service/TagService.php +++ b/apps/files/lib/Service/TagService.php @@ -7,9 +7,12 @@ */ namespace OCA\Files\Service; +use OCA\Files\AppInfo\Application; use OCP\Activity\IManager; use OCP\Files\Folder; +use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; +use OCP\ITagManager; use OCP\ITags; use OCP\IUserSession; @@ -17,13 +20,20 @@ use OCP\IUserSession; * Service class to manage tags on files. */ class TagService { + private ?Folder $homeFolder = null; + private ?ITags $tagger; public function __construct( private IUserSession $userSession, private IManager $activityManager, - private ?ITags $tagger, - private ?Folder $homeFolder, + ITagManager $tagManager, + IRootFolder $rootFolder, ) { + $user = $this->userSession->getUser(); + if ($user) { + $this->homeFolder = $rootFolder->getUserFolder($user->getUID()); + } + $this->tagger = $tagManager->load(Application::APP_ID); } /** diff --git a/apps/files/tests/Service/TagServiceTest.php b/apps/files/tests/Service/TagServiceTest.php index 1b3e0898b33..b914489053f 100644 --- a/apps/files/tests/Service/TagServiceTest.php +++ b/apps/files/tests/Service/TagServiceTest.php @@ -44,6 +44,9 @@ class TagServiceTest extends \Test\TestCase { \OC_User::setUserId($this->user); \OC_Util::setupFS($this->user); $user = $this->createMock(IUser::class); + $user->expects($this->any()) + ->method('getUID') + ->willReturn($this->user); $this->userSession = $this->createMock(IUserSession::class); $this->userSession->expects($this->any()) ->method('getUser') @@ -61,8 +64,8 @@ class TagServiceTest extends \Test\TestCase { ->setConstructorArgs([ $this->userSession, $this->activityManager, - $this->tagger, - $this->root, + Server::get(ITagManager::class), + Server::get(IRootFolder::class), ]) ->onlyMethods($methods) ->getMock(); @@ -91,16 +94,22 @@ class TagServiceTest extends \Test\TestCase { // set tags $this->tagService->updateFileTags('subdir/test.txt', [$tag1, $tag2]); + // Sync to reload tags + $this->tagger->addMultiple([], sync:true); $this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag1)); $this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag2)); // remove tag $this->tagService->updateFileTags('subdir/test.txt', [$tag2]); + // Sync to reload tags + $this->tagger->addMultiple([], sync:true); $this->assertEquals([], $this->tagger->getIdsForTag($tag1)); $this->assertEquals([$fileId], $this->tagger->getIdsForTag($tag2)); // clear tags $this->tagService->updateFileTags('subdir/test.txt', []); + // Sync to reload tags + $this->tagger->addMultiple([], sync:true); $this->assertEquals([], $this->tagger->getIdsForTag($tag1)); $this->assertEquals([], $this->tagger->getIdsForTag($tag2));