diff options
author | Eduardo Morales <emoral435@gmail.com> | 2024-03-09 10:55:22 -0600 |
---|---|---|
committer | Eduardo Morales <emoral435@gmail.com> | 2024-03-11 15:57:12 -0500 |
commit | 88b40bb392a2a35d84ef30b2fc7a893aac83fc7c (patch) | |
tree | 15ba708a49b43f7a69652f3d7384aad4db0f2217 /apps/files_versions | |
parent | b32c0f7da6e0992768f848dd4404e6fe169a51e1 (diff) | |
download | nextcloud-server-88b40bb392a2a35d84ef30b2fc7a893aac83fc7c.tar.gz nextcloud-server-88b40bb392a2a35d84ef30b2fc7a893aac83fc7c.zip |
feat: added metadata event listener
Signed-off-by: Eduardo Morales <emoral435@gmail.com>
Diffstat (limited to 'apps/files_versions')
-rw-r--r-- | apps/files_versions/lib/AppInfo/Application.php | 2 | ||||
-rw-r--r-- | apps/files_versions/lib/Listener/MetadataFileEvents.php | 67 |
2 files changed, 69 insertions, 0 deletions
diff --git a/apps/files_versions/lib/AppInfo/Application.php b/apps/files_versions/lib/AppInfo/Application.php index 2556b8e4150..36d7ec8e2a0 100644 --- a/apps/files_versions/lib/AppInfo/Application.php +++ b/apps/files_versions/lib/AppInfo/Application.php @@ -36,6 +36,7 @@ use OCA\Files_Versions\Capabilities; use OCA\Files_Versions\Listener\FileEventsListener; use OCA\Files_Versions\Listener\LoadAdditionalListener; use OCA\Files_Versions\Listener\LoadSidebarListener; +use OCA\Files_Versions\Listener\MetadataFileEvents; use OCA\Files_Versions\Versions\IVersionManager; use OCA\Files_Versions\Versions\VersionManager; use OCP\Accounts\IAccountManager; @@ -113,6 +114,7 @@ class Application extends App implements IBootstrap { $context->registerEventListener(NodeTouchedEvent::class, FileEventsListener::class); $context->registerEventListener(BeforeNodeWrittenEvent::class, FileEventsListener::class); $context->registerEventListener(NodeWrittenEvent::class, FileEventsListener::class); + $context->registerEventListener(NodeWrittenEvent::class, MetadataFileEvents::class); $context->registerEventListener(BeforeNodeDeletedEvent::class, FileEventsListener::class); $context->registerEventListener(NodeDeletedEvent::class, FileEventsListener::class); $context->registerEventListener(NodeRenamedEvent::class, FileEventsListener::class); diff --git a/apps/files_versions/lib/Listener/MetadataFileEvents.php b/apps/files_versions/lib/Listener/MetadataFileEvents.php new file mode 100644 index 00000000000..b58b6b4394a --- /dev/null +++ b/apps/files_versions/lib/Listener/MetadataFileEvents.php @@ -0,0 +1,67 @@ +<?php + +declare(strict_types=1); + +/** + * @author Eduardo Morales emoral435@gmail.com> + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ +namespace OCA\Files_Versions\Listener; + +use OC\Files\Node\Folder; +use OCA\Files_Versions\Versions\IMetadataVersionBackend; +use OCA\Files_Versions\Versions\IVersionManager; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Files\Events\Node\NodeWrittenEvent; +use OCP\Files\Node; +use OCP\IUserSession; + +/** @template-implements IEventListener<NodeWrittenEvent> */ +class MetadataFileEvents implements IEventListener { + public function __construct( + private IVersionManager $versionManager, + private IUserSession $userSession, + ) { + } + + /** + * @abstract handles events from a nodes version being changed + * @param Event $event the event that triggered this listener to activate + */ + public function handle(Event $event): void { + if ($event instanceof NodeWrittenEvent) { + $this->post_write_hook($event->getNode()); + } + } + + /** + * @abstract handles the NodeWrittenEvent, and sets the metadata for the associated node + * @param Node $node the node that is currently being written + */ + public function post_write_hook(Node $node): void { + // Do not handle folders or users that we cannot get metadata from + if ($node instanceof Folder || is_null($this->userSession->getUser())) { + return; + } + // check if our version manager supports setting the metadata + if ($this->versionManager instanceof IMetadataVersionBackend) { + $author = $this->userSession->getUser()->getDisplayName() ?? ''; + $this->versionManager->setMetadataValue($node, "author", $author); + } + } +} |