fix(files): Use proper DI for TagService class

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2026-05-11 12:19:36 +02:00
parent 865cf5f2a7
commit c853d8f96e
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A
3 changed files with 23 additions and 25 deletions

View file

@ -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
*/

View file

@ -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);
}
/**

View file

@ -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));