From 778ae6a4875cc0789f76dbf67f934ff3d3987f89 Mon Sep 17 00:00:00 2001 From: schaarsc Date: Sun, 26 Jan 2025 20:13:16 +0100 Subject: [PATCH] feat(systemtags): add commands to manage tags on files Resolve nextcloud#32735 Signed-off-by: schaarsc --- apps/systemtags/appinfo/info.xml | 5 ++ .../composer/composer/autoload_classmap.php | 3 + .../composer/composer/autoload_static.php | 3 + apps/systemtags/lib/Command/Files/Add.php | 90 +++++++++++++++++++ apps/systemtags/lib/Command/Files/Delete.php | 88 ++++++++++++++++++ .../lib/Command/Files/DeleteAll.php | 49 ++++++++++ 6 files changed, 238 insertions(+) create mode 100644 apps/systemtags/lib/Command/Files/Add.php create mode 100644 apps/systemtags/lib/Command/Files/Delete.php create mode 100644 apps/systemtags/lib/Command/Files/DeleteAll.php diff --git a/apps/systemtags/appinfo/info.xml b/apps/systemtags/appinfo/info.xml index e2e84cce1c8f9..56b2974d8e6f5 100644 --- a/apps/systemtags/appinfo/info.xml +++ b/apps/systemtags/appinfo/info.xml @@ -25,6 +25,11 @@ + + OCA\SystemTags\Command\Files\Add + OCA\SystemTags\Command\Files\Delete + OCA\SystemTags\Command\Files\DeleteAll + OCA\SystemTags\Settings\Admin diff --git a/apps/systemtags/composer/composer/autoload_classmap.php b/apps/systemtags/composer/composer/autoload_classmap.php index e6c60728ff6ea..450a387cd7264 100644 --- a/apps/systemtags/composer/composer/autoload_classmap.php +++ b/apps/systemtags/composer/composer/autoload_classmap.php @@ -12,6 +12,9 @@ 'OCA\\SystemTags\\Activity\\Setting' => $baseDir . '/../lib/Activity/Setting.php', 'OCA\\SystemTags\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php', 'OCA\\SystemTags\\Capabilities' => $baseDir . '/../lib/Capabilities.php', + 'OCA\\SystemTags\\Command\\Files\\Add' => $baseDir . '/../lib/Command/Files/Add.php', + 'OCA\\SystemTags\\Command\\Files\\Delete' => $baseDir . '/../lib/Command/Files/Delete.php', + 'OCA\\SystemTags\\Command\\Files\\DeleteAll' => $baseDir . '/../lib/Command/Files/DeleteAll.php', 'OCA\\SystemTags\\Controller\\LastUsedController' => $baseDir . '/../lib/Controller/LastUsedController.php', 'OCA\\SystemTags\\Listeners\\BeforeSabrePubliclyLoadedListener' => $baseDir . '/../lib/Listeners/BeforeSabrePubliclyLoadedListener.php', 'OCA\\SystemTags\\Listeners\\BeforeTemplateRenderedListener' => $baseDir . '/../lib/Listeners/BeforeTemplateRenderedListener.php', diff --git a/apps/systemtags/composer/composer/autoload_static.php b/apps/systemtags/composer/composer/autoload_static.php index 3d61ea1a1c17d..a86e63edc5367 100644 --- a/apps/systemtags/composer/composer/autoload_static.php +++ b/apps/systemtags/composer/composer/autoload_static.php @@ -27,6 +27,9 @@ class ComposerStaticInitSystemTags 'OCA\\SystemTags\\Activity\\Setting' => __DIR__ . '/..' . '/../lib/Activity/Setting.php', 'OCA\\SystemTags\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php', 'OCA\\SystemTags\\Capabilities' => __DIR__ . '/..' . '/../lib/Capabilities.php', + 'OCA\\SystemTags\\Command\\Files\\Add' => __DIR__ . '/..' . '/../lib/Command/Files/Add.php', + 'OCA\\SystemTags\\Command\\Files\\Delete' => __DIR__ . '/..' . '/../lib/Command/Files/Delete.php', + 'OCA\\SystemTags\\Command\\Files\\DeleteAll' => __DIR__ . '/..' . '/../lib/Command/Files/DeleteAll.php', 'OCA\\SystemTags\\Controller\\LastUsedController' => __DIR__ . '/..' . '/../lib/Controller/LastUsedController.php', 'OCA\\SystemTags\\Listeners\\BeforeSabrePubliclyLoadedListener' => __DIR__ . '/..' . '/../lib/Listeners/BeforeSabrePubliclyLoadedListener.php', 'OCA\\SystemTags\\Listeners\\BeforeTemplateRenderedListener' => __DIR__ . '/..' . '/../lib/Listeners/BeforeTemplateRenderedListener.php', diff --git a/apps/systemtags/lib/Command/Files/Add.php b/apps/systemtags/lib/Command/Files/Add.php new file mode 100644 index 0000000000000..237feb748a478 --- /dev/null +++ b/apps/systemtags/lib/Command/Files/Add.php @@ -0,0 +1,90 @@ +setName('tag:files:add') + ->setDescription('Add a system-tag to a file or folder') + ->addArgument('target', InputArgument::REQUIRED, 'file id or path') + ->addArgument('tags', InputArgument::REQUIRED, 'Name of the tag(s) to add, comma separated') + ->addArgument('access', InputArgument::REQUIRED, 'access level of the tag (public, restricted or invisible)'); + } + + public function execute(InputInterface $input, OutputInterface $output): int { + $targetInput = $input->getArgument('target'); + $tagsInput = $input->getArgument('tags'); + + if ($tagsInput === '') { + $output->writeln('`tags` can\'t be empty'); + return 3; + } + + $tagNameArray = explode(',', $tagsInput); + + $access = $input->getArgument('access'); + switch ($access) { + case 'public': + $userVisible = true; + $userAssignable = true; + break; + case 'restricted': + $userVisible = true; + $userAssignable = false; + break; + case 'invisible': + $userVisible = false; + $userAssignable = false; + break; + default: + $output->writeln('`access` property is invalid'); + return 1; + } + + $targetNode = $this->fileUtils->getNode($targetInput); + + if (! $targetNode) { + $output->writeln("file $targetInput not found"); + return 1; + } + + foreach ($tagNameArray as $tagName) { + try { + $tag = $this->systemTagManager->createTag($tagName, $userVisible, $userAssignable); + $output->writeln("$access tag named $tagName created."); + } catch (TagAlreadyExistsException $e) { + $tag = $this->systemTagManager->getTag($tagName, $userVisible, $userAssignable); + } + + $this->systemTagObjectMapper->assignTags((string)$targetNode->getId(), 'files', $tag->getId()); + $output->writeln("$access tag named $tagName added."); + } + + return 0; + } +} diff --git a/apps/systemtags/lib/Command/Files/Delete.php b/apps/systemtags/lib/Command/Files/Delete.php new file mode 100644 index 0000000000000..b24135664d794 --- /dev/null +++ b/apps/systemtags/lib/Command/Files/Delete.php @@ -0,0 +1,88 @@ +setName('tag:files:delete') + ->setDescription('Delete a system-tag from a file or folder') + ->addArgument('target', InputArgument::REQUIRED, 'file id or path') + ->addArgument('tags', InputArgument::REQUIRED, 'Name of the tag(s) to delete, comma separated') + ->addArgument('access', InputArgument::REQUIRED, 'access level of the tag (public, restricted or invisible)'); + } + + public function execute(InputInterface $input, OutputInterface $output): int { + $targetInput = $input->getArgument('target'); + $tagsInput = $input->getArgument('tags'); + + if ($tagsInput === '') { + $output->writeln('`tags` can\'t be empty'); + return 3; + } + + $tagNameArray = explode(',', $tagsInput); + + $access = $input->getArgument('access'); + switch ($access) { + case 'public': + $userVisible = true; + $userAssignable = true; + break; + case 'restricted': + $userVisible = true; + $userAssignable = false; + break; + case 'invisible': + $userVisible = false; + $userAssignable = false; + break; + default: + $output->writeln('`access` property is invalid'); + return 1; + } + + $targetNode = $this->fileUtils->getNode($targetInput); + + if (! $targetNode) { + $output->writeln("file $targetInput not found"); + return 1; + } + + foreach ($tagNameArray as $tagName) { + try { + $tag = $this->systemTagManager->getTag($tagName, $userVisible, $userAssignable); + $this->systemTagObjectMapper->unassignTags((string)$targetNode->getId(), 'files', $tag->getId()); + $output->writeln("$access tag named $tagName removed."); + } catch (TagNotFoundException $e) { + $output->writeln("$access tag named $tagName does not exist!"); + } + } + + return 0; + } +} diff --git a/apps/systemtags/lib/Command/Files/DeleteAll.php b/apps/systemtags/lib/Command/Files/DeleteAll.php new file mode 100644 index 0000000000000..59ebb741b8820 --- /dev/null +++ b/apps/systemtags/lib/Command/Files/DeleteAll.php @@ -0,0 +1,49 @@ +setName('tag:files:delete-all') + ->setDescription('Delete all system-tags from a file or folder') + ->addArgument('target', InputArgument::REQUIRED, 'file id or path'); + } + + public function execute(InputInterface $input, OutputInterface $output): int { + $targetInput = $input->getArgument('target'); + $targetNode = $this->fileUtils->getNode($targetInput); + + if (! $targetNode) { + $output->writeln("file $targetInput not found"); + return 1; + } + + $tags = $this->systemTagObjectMapper->getTagIdsForObjects([$targetNode->getId()], 'files'); + $this->systemTagObjectMapper->unassignTags((string)$targetNode->getId(), 'files', $tags[$targetNode->getId()]); + $output->writeln('all tags removed.'); + + return 0; + } +}