Skip to content

Commit f99b820

Browse files
authored
Merge pull request #44049 from nextcloud/feat/show-name-of-user-in-versions
feat: show the id of last author in versions metadata
2 parents b32c0f7 + bfacc6d commit f99b820

20 files changed

Lines changed: 274 additions & 12 deletions

apps/files_versions/composer/composer/autoload_classmap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
'OCA\\Files_Versions\\Listener\\FileEventsListener' => $baseDir . '/../lib/Listener/FileEventsListener.php',
2222
'OCA\\Files_Versions\\Listener\\LoadAdditionalListener' => $baseDir . '/../lib/Listener/LoadAdditionalListener.php',
2323
'OCA\\Files_Versions\\Listener\\LoadSidebarListener' => $baseDir . '/../lib/Listener/LoadSidebarListener.php',
24+
'OCA\\Files_Versions\\Listener\\MetadataFileEvents' => $baseDir . '/../lib/Listener/MetadataFileEvents.php',
2425
'OCA\\Files_Versions\\Migration\\Version1020Date20221114144058' => $baseDir . '/../lib/Migration/Version1020Date20221114144058.php',
2526
'OCA\\Files_Versions\\Sabre\\Plugin' => $baseDir . '/../lib/Sabre/Plugin.php',
2627
'OCA\\Files_Versions\\Sabre\\RestoreFolder' => $baseDir . '/../lib/Sabre/RestoreFolder.php',
@@ -32,6 +33,8 @@
3233
'OCA\\Files_Versions\\Storage' => $baseDir . '/../lib/Storage.php',
3334
'OCA\\Files_Versions\\Versions\\BackendNotFoundException' => $baseDir . '/../lib/Versions/BackendNotFoundException.php',
3435
'OCA\\Files_Versions\\Versions\\IDeletableVersionBackend' => $baseDir . '/../lib/Versions/IDeletableVersionBackend.php',
36+
'OCA\\Files_Versions\\Versions\\IMetadataVersion' => $baseDir . '/../lib/Versions/IMetadataVersion.php',
37+
'OCA\\Files_Versions\\Versions\\IMetadataVersionBackend' => $baseDir . '/../lib/Versions/IMetadataVersionBackend.php',
3538
'OCA\\Files_Versions\\Versions\\INameableVersion' => $baseDir . '/../lib/Versions/INameableVersion.php',
3639
'OCA\\Files_Versions\\Versions\\INameableVersionBackend' => $baseDir . '/../lib/Versions/INameableVersionBackend.php',
3740
'OCA\\Files_Versions\\Versions\\INeedSyncVersionBackend' => $baseDir . '/../lib/Versions/INeedSyncVersionBackend.php',

apps/files_versions/composer/composer/autoload_static.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ComposerStaticInitFiles_Versions
3636
'OCA\\Files_Versions\\Listener\\FileEventsListener' => __DIR__ . '/..' . '/../lib/Listener/FileEventsListener.php',
3737
'OCA\\Files_Versions\\Listener\\LoadAdditionalListener' => __DIR__ . '/..' . '/../lib/Listener/LoadAdditionalListener.php',
3838
'OCA\\Files_Versions\\Listener\\LoadSidebarListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarListener.php',
39+
'OCA\\Files_Versions\\Listener\\MetadataFileEvents' => __DIR__ . '/..' . '/../lib/Listener/MetadataFileEvents.php',
3940
'OCA\\Files_Versions\\Migration\\Version1020Date20221114144058' => __DIR__ . '/..' . '/../lib/Migration/Version1020Date20221114144058.php',
4041
'OCA\\Files_Versions\\Sabre\\Plugin' => __DIR__ . '/..' . '/../lib/Sabre/Plugin.php',
4142
'OCA\\Files_Versions\\Sabre\\RestoreFolder' => __DIR__ . '/..' . '/../lib/Sabre/RestoreFolder.php',
@@ -47,6 +48,8 @@ class ComposerStaticInitFiles_Versions
4748
'OCA\\Files_Versions\\Storage' => __DIR__ . '/..' . '/../lib/Storage.php',
4849
'OCA\\Files_Versions\\Versions\\BackendNotFoundException' => __DIR__ . '/..' . '/../lib/Versions/BackendNotFoundException.php',
4950
'OCA\\Files_Versions\\Versions\\IDeletableVersionBackend' => __DIR__ . '/..' . '/../lib/Versions/IDeletableVersionBackend.php',
51+
'OCA\\Files_Versions\\Versions\\IMetadataVersion' => __DIR__ . '/..' . '/../lib/Versions/IMetadataVersion.php',
52+
'OCA\\Files_Versions\\Versions\\IMetadataVersionBackend' => __DIR__ . '/..' . '/../lib/Versions/IMetadataVersionBackend.php',
5053
'OCA\\Files_Versions\\Versions\\INameableVersion' => __DIR__ . '/..' . '/../lib/Versions/INameableVersion.php',
5154
'OCA\\Files_Versions\\Versions\\INameableVersionBackend' => __DIR__ . '/..' . '/../lib/Versions/INameableVersionBackend.php',
5255
'OCA\\Files_Versions\\Versions\\INeedSyncVersionBackend' => __DIR__ . '/..' . '/../lib/Versions/INeedSyncVersionBackend.php',

apps/files_versions/composer/composer/installed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'name' => '__root__',
44
'pretty_version' => 'dev-master',
55
'version' => 'dev-master',
6-
'reference' => 'a820e3d036741ad1194361eca11bc1cbcdda0a47',
6+
'reference' => '84930a207a8d5f0ef32320796fe188892b63fa19',
77
'type' => 'library',
88
'install_path' => __DIR__ . '/../',
99
'aliases' => array(),
@@ -13,7 +13,7 @@
1313
'__root__' => array(
1414
'pretty_version' => 'dev-master',
1515
'version' => 'dev-master',
16-
'reference' => 'a820e3d036741ad1194361eca11bc1cbcdda0a47',
16+
'reference' => '84930a207a8d5f0ef32320796fe188892b63fa19',
1717
'type' => 'library',
1818
'install_path' => __DIR__ . '/../',
1919
'aliases' => array(),

apps/files_versions/lib/AppInfo/Application.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCA\Files_Versions\Listener\FileEventsListener;
3737
use OCA\Files_Versions\Listener\LoadAdditionalListener;
3838
use OCA\Files_Versions\Listener\LoadSidebarListener;
39+
use OCA\Files_Versions\Listener\MetadataFileEvents;
3940
use OCA\Files_Versions\Versions\IVersionManager;
4041
use OCA\Files_Versions\Versions\VersionManager;
4142
use OCP\Accounts\IAccountManager;
@@ -119,6 +120,8 @@ public function register(IRegistrationContext $context): void {
119120
$context->registerEventListener(NodeCopiedEvent::class, FileEventsListener::class);
120121
$context->registerEventListener(BeforeNodeRenamedEvent::class, FileEventsListener::class);
121122
$context->registerEventListener(BeforeNodeCopiedEvent::class, FileEventsListener::class);
123+
124+
$context->registerEventListener(NodeWrittenEvent::class, MetadataFileEvents::class);
122125
}
123126

124127
public function boot(IBootContext $context): void {

apps/files_versions/lib/Db/VersionEntity.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,23 @@ public function setLabel(string $label): void {
7878
$this->metadata['label'] = $label;
7979
$this->markFieldUpdated('metadata');
8080
}
81+
82+
/**
83+
* @abstract given a key, return the value associated with the key in the metadata column
84+
* if nothing is found, we return an empty string
85+
* @param string $key key associated with the value
86+
*/
87+
public function getMetadataValue(string $key): ?string {
88+
return $this->metadata[$key] ?? null;
89+
}
90+
91+
/**
92+
* @abstract sets a key value pair in the metadata column
93+
* @param string $key key associated with the value
94+
* @param string $value value associated with the key
95+
*/
96+
public function setMetadataValue(string $key, string $value): void {
97+
$this->metadata[$key] = $value;
98+
$this->markFieldUpdated('metadata');
99+
}
81100
}

apps/files_versions/lib/Db/VersionsMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function deleteAllVersionsForFileId(int $fileId): int {
8585
->executeStatement();
8686
}
8787

88-
public function deleteAllVersionsForUser(int $storageId, string $path = null): void {
88+
public function deleteAllVersionsForUser(int $storageId, ?string $path = null): void {
8989
$fileIdsGenerator = $this->getFileIdsGenerator($storageId, $path);
9090

9191
$versionEntitiesDeleteQuery = $this->db->getQueryBuilder();
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

apps/files_versions/lib/Sabre/Plugin.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Plugin extends ServerPlugin {
4444

4545
public const VERSION_LABEL = '{http://nextcloud.org/ns}version-label';
4646

47+
public const VERSION_AUTHOR = '{http://nextcloud.org/ns}version-author'; // dav property for author
48+
4749
public function __construct(
4850
private IRequest $request,
4951
private IPreview $previewManager,
@@ -93,6 +95,7 @@ public function afterGet(RequestInterface $request, ResponseInterface $response)
9395
public function propFind(PropFind $propFind, INode $node): void {
9496
if ($node instanceof VersionFile) {
9597
$propFind->handle(self::VERSION_LABEL, fn () => $node->getLabel());
98+
$propFind->handle(self::VERSION_AUTHOR, fn () => $node->getMetadataValue("author"));
9699
$propFind->handle(FilesPlugin::HAS_PREVIEW_PROPERTYNAME, fn () => $this->previewManager->isMimeSupported($node->getContentType()));
97100
}
98101
}

apps/files_versions/lib/Sabre/VersionFile.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
namespace OCA\Files_Versions\Sabre;
2828

2929
use OCA\Files_Versions\Versions\IDeletableVersionBackend;
30+
use OCA\Files_Versions\Versions\IMetadataVersion;
3031
use OCA\Files_Versions\Versions\INameableVersion;
3132
use OCA\Files_Versions\Versions\INameableVersionBackend;
3233
use OCA\Files_Versions\Versions\IVersion;
@@ -109,6 +110,13 @@ public function setLabel($label): bool {
109110
}
110111
}
111112

113+
public function getMetadataValue(string $key): ?string {
114+
if ($this->version instanceof IMetadataVersion) {
115+
return $this->version->getMetadataValue($key);
116+
}
117+
return null;
118+
}
119+
112120
public function getLastModified(): int {
113121
return $this->version->getTimestamp();
114122
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2024 Eduardo Morales <emoral435@gmail.com>
7+
*
8+
* @license GNU AGPL-3.0-or-later
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
namespace OCA\Files_Versions\Versions;
25+
26+
/**
27+
* This interface allows for just direct accessing of the metadata column JSON
28+
* @since 29.0.0
29+
*/
30+
interface IMetadataVersion {
31+
/**
32+
* retrieves the metadata value from our $key param
33+
*
34+
* @param string $key the key for the json value of the metadata column
35+
* @since 29.0.0
36+
*/
37+
public function getMetadataValue(string $key): ?string;
38+
}

0 commit comments

Comments
 (0)