|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @author Eduardo Morales emoral435@gmail.com> |
| 7 | + * |
| 8 | + * @license GNU AGPL-3.0-or-later |
| 9 | + * |
| 10 | + * This code is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 12 | + * as published by the Free Software Foundation. |
| 13 | + * |
| 14 | + * This program is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU Affero General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 20 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 21 | + * |
| 22 | + */ |
| 23 | +namespace OCA\Files_Versions\Listener; |
| 24 | + |
| 25 | +use OC\Files\Node\Folder; |
| 26 | +use OCA\Files_Versions\Versions\IMetadataVersionBackend; |
| 27 | +use OCA\Files_Versions\Versions\IVersionManager; |
| 28 | +use OCP\EventDispatcher\Event; |
| 29 | +use OCP\EventDispatcher\IEventListener; |
| 30 | +use OCP\Files\Events\Node\NodeWrittenEvent; |
| 31 | +use OCP\Files\Node; |
| 32 | +use OCP\IUserSession; |
| 33 | + |
| 34 | +/** @template-implements IEventListener<NodeWrittenEvent> */ |
| 35 | +class MetadataFileEvents implements IEventListener { |
| 36 | + public function __construct( |
| 37 | + private IVersionManager $versionManager, |
| 38 | + private IUserSession $userSession, |
| 39 | + ) { |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @abstract handles events from a nodes version being changed |
| 44 | + * @param Event $event the event that triggered this listener to activate |
| 45 | + */ |
| 46 | + public function handle(Event $event): void { |
| 47 | + if ($event instanceof NodeWrittenEvent) { |
| 48 | + $this->post_write_hook($event->getNode()); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @abstract handles the NodeWrittenEvent, and sets the metadata for the associated node |
| 54 | + * @param Node $node the node that is currently being written |
| 55 | + */ |
| 56 | + public function post_write_hook(Node $node): void { |
| 57 | + $user = $this->userSession->getUser(); |
| 58 | + // Do not handle folders or users that we cannot get metadata from |
| 59 | + if ($node instanceof Folder || is_null($user)) { |
| 60 | + return; |
| 61 | + } |
| 62 | + // check if our version manager supports setting the metadata |
| 63 | + if ($this->versionManager instanceof IMetadataVersionBackend) { |
| 64 | + $author = $user->getUID(); |
| 65 | + $this->versionManager->setMetadataValue($node, 'author', $author); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments