Skip to content

Commit

Permalink
refactor: move version rollback hook to event
Browse files Browse the repository at this point in the history
Signed-off-by: ailkiv <a.ilkiv.ye@gmail.com>
  • Loading branch information
AIlkiv committed Feb 24, 2025
1 parent c1204c0 commit e935ddd
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 12 deletions.
2 changes: 2 additions & 0 deletions apps/files_versions/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
'OCA\\Files_Versions\\Db\\VersionEntity' => $baseDir . '/../lib/Db/VersionEntity.php',
'OCA\\Files_Versions\\Db\\VersionsMapper' => $baseDir . '/../lib/Db/VersionsMapper.php',
'OCA\\Files_Versions\\Events\\CreateVersionEvent' => $baseDir . '/../lib/Events/CreateVersionEvent.php',
'OCA\\Files_Versions\\Events\\VersionRestoredEvent' => $baseDir . '/../lib/Events/VersionRestoredEvent.php',
'OCA\\Files_Versions\\Expiration' => $baseDir . '/../lib/Expiration.php',
'OCA\\Files_Versions\\Listener\\FileEventsListener' => $baseDir . '/../lib/Listener/FileEventsListener.php',
'OCA\\Files_Versions\\Listener\\LoadAdditionalListener' => $baseDir . '/../lib/Listener/LoadAdditionalListener.php',
'OCA\\Files_Versions\\Listener\\LoadSidebarListener' => $baseDir . '/../lib/Listener/LoadSidebarListener.php',
'OCA\\Files_Versions\\Listener\\VersionAuthorListener' => $baseDir . '/../lib/Listener/VersionAuthorListener.php',
'OCA\\Files_Versions\\Listener\\VersionStorageMoveListener' => $baseDir . '/../lib/Listener/VersionStorageMoveListener.php',
'OCA\\Files_Versions\\Listener\\LegacyRollbackListener' => $baseDir . '/../lib/Listener/LegacyRollbackListener.php',
'OCA\\Files_Versions\\Migration\\Version1020Date20221114144058' => $baseDir . '/../lib/Migration/Version1020Date20221114144058.php',
'OCA\\Files_Versions\\Sabre\\Plugin' => $baseDir . '/../lib/Sabre/Plugin.php',
'OCA\\Files_Versions\\Sabre\\RestoreFolder' => $baseDir . '/../lib/Sabre/RestoreFolder.php',
Expand Down
2 changes: 2 additions & 0 deletions apps/files_versions/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ class ComposerStaticInitFiles_Versions
'OCA\\Files_Versions\\Db\\VersionEntity' => __DIR__ . '/..' . '/../lib/Db/VersionEntity.php',
'OCA\\Files_Versions\\Db\\VersionsMapper' => __DIR__ . '/..' . '/../lib/Db/VersionsMapper.php',
'OCA\\Files_Versions\\Events\\CreateVersionEvent' => __DIR__ . '/..' . '/../lib/Events/CreateVersionEvent.php',
'OCA\\Files_Versions\\Events\\VersionRestoredEvent' => __DIR__ . '/..' . '/../lib/Events/VersionRestoredEvent.php',
'OCA\\Files_Versions\\Expiration' => __DIR__ . '/..' . '/../lib/Expiration.php',
'OCA\\Files_Versions\\Listener\\FileEventsListener' => __DIR__ . '/..' . '/../lib/Listener/FileEventsListener.php',
'OCA\\Files_Versions\\Listener\\LoadAdditionalListener' => __DIR__ . '/..' . '/../lib/Listener/LoadAdditionalListener.php',
'OCA\\Files_Versions\\Listener\\LoadSidebarListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarListener.php',
'OCA\\Files_Versions\\Listener\\VersionAuthorListener' => __DIR__ . '/..' . '/../lib/Listener/VersionAuthorListener.php',
'OCA\\Files_Versions\\Listener\\VersionStorageMoveListener' => __DIR__ . '/..' . '/../lib/Listener/VersionStorageMoveListener.php',
'OCA\\Files_Versions\\Listener\\LegacyRollbackListener' => __DIR__ . '/..' . '/../lib/Listener/LegacyRollbackListener.php',
'OCA\\Files_Versions\\Migration\\Version1020Date20221114144058' => __DIR__ . '/..' . '/../lib/Migration/Version1020Date20221114144058.php',
'OCA\\Files_Versions\\Sabre\\Plugin' => __DIR__ . '/..' . '/../lib/Sabre/Plugin.php',
'OCA\\Files_Versions\\Sabre\\RestoreFolder' => __DIR__ . '/..' . '/../lib/Sabre/RestoreFolder.php',
Expand Down
8 changes: 5 additions & 3 deletions apps/files_versions/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files\Event\LoadSidebar;
use OCA\Files_Versions\Capabilities;
use OCA\Files_Versions\Events\VersionRestoredEvent;
use OCA\Files_Versions\Listener\FileEventsListener;
use OCA\Files_Versions\Listener\LegacyRollbackListener;
use OCA\Files_Versions\Listener\LoadAdditionalListener;
use OCA\Files_Versions\Listener\LoadSidebarListener;
use OCA\Files_Versions\Listener\VersionAuthorListener;
Expand Down Expand Up @@ -80,9 +82,7 @@ public function register(IRegistrationContext $context): void {
);
});

$context->registerService(IVersionManager::class, function () {
return new VersionManager();
});
$context->registerServiceAlias(IVersionManager::class, VersionManager::class);

/**
* Register Events
Expand All @@ -108,6 +108,8 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(BeforeNodeCopiedEvent::class, FileEventsListener::class);

$context->registerEventListener(NodeWrittenEvent::class, VersionAuthorListener::class);

$context->registerEventListener(VersionRestoredEvent::class, LegacyRollbackListener::class);
}

public function boot(IBootContext $context): void {
Expand Down
31 changes: 31 additions & 0 deletions apps/files_versions/lib/Events/VersionRestoredEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_Versions\Events;

use OCP\EventDispatcher\Event;
use OCA\Files_Versions\Versions\IVersion;

/**
* Class VersionRestoredEvent
*
* Event that is called after a successful restore of a previous version
*
* @package OCA\Files_Versions
*/
class VersionRestoredEvent extends Event {
public function __construct(private IVersion $version) {
}

/**
* Version that was restored
*/
public function getVersion(): IVersion {
return $this->version;
}
}
35 changes: 35 additions & 0 deletions apps/files_versions/lib/Listener/LegacyRollbackListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Files_Versions\Listener;

use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCA\Files_Versions\Events\VersionRestoredEvent;

/**
* This listener is designed to be compatible with third-party code
* that can still use a hook. This listener will be removed in
* the next version and the rollback hook will stop working.
*
* @template-implements IEventListener<VersionRestoredEvent>
*/
class LegacyRollbackListener implements IEventListener {
public function handle(Event $event): void {
if (!($event instanceof VersionRestoredEvent)) {
return;
}

$version = $event->getVersion();
\OC_Hook::emit('\OCP\Versions', 'rollback', [
'path' => $version->getVersionPath(),
'revision' => $version->getRevisionId(),
'node' => $version->getSourceFile(),
]);
}
}
14 changes: 9 additions & 5 deletions apps/files_versions/lib/Versions/VersionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace OCA\Files_Versions\Versions;

use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
Expand All @@ -16,14 +17,21 @@
use OCP\Files\Lock\LockContext;
use OCP\Files\Node;
use OCP\Files\Storage\IStorage;
use OCA\Files_Versions\Events\VersionRestoredEvent;
use OCP\IUser;
use OCP\Lock\ManuallyLockedException;
use OCP\Server;

class VersionManager implements IVersionManager, IDeletableVersionBackend, INeedSyncVersionBackend, IMetadataVersionBackend {

/** @var (IVersionBackend[])[] */
private $backends = [];

public function __construct(
private IEventDispatcher $dispatcher
) {
}

public function registerBackend(string $storageType, IVersionBackend $backend) {
if (!isset($this->backends[$storageType])) {
$this->backends[$storageType] = [];
Expand Down Expand Up @@ -87,11 +95,7 @@ public function rollback(IVersion $version) {
$result = self::handleAppLocks(fn (): ?bool => $backend->rollback($version));
// rollback doesn't have a return type yet and some implementations don't return anything
if ($result === null || $result === true) {
\OC_Hook::emit('\OCP\Versions', 'rollback', [
'path' => $version->getVersionPath(),
'revision' => $version->getRevisionId(),
'node' => $version->getSourceFile(),
]);
$this->dispatcher->dispatchTyped(new VersionRestoredEvent($version));
}
return $result;
}
Expand Down
10 changes: 6 additions & 4 deletions apps/files_versions/tests/Versions/VersionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use OC\Files\Storage\Local;
use OCA\Files_Versions\Versions\IVersionBackend;
use OCA\Files_Versions\Versions\VersionManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Storage\IStorage;
use OCP\Server;
use Test\TestCase;

class VersionManagerTest extends TestCase {
Expand All @@ -30,15 +32,15 @@ private function getStorage(string $class): IStorage {
}

public function testGetBackendSingle(): void {
$manager = new VersionManager();
$manager = new VersionManager(Server::get(IEventDispatcher::class));
$backend = $this->getBackend();
$manager->registerBackend(IStorage::class, $backend);

$this->assertEquals($backend, $manager->getBackendForStorage($this->getStorage(Local::class)));
}

public function testGetBackendMoreSpecific(): void {
$manager = new VersionManager();
$manager = new VersionManager(Server::get(IEventDispatcher::class));
$backend1 = $this->getBackend();
$backend2 = $this->getBackend();
$manager->registerBackend(IStorage::class, $backend1);
Expand All @@ -48,7 +50,7 @@ public function testGetBackendMoreSpecific(): void {
}

public function testGetBackendNoUse(): void {
$manager = new VersionManager();
$manager = new VersionManager(Server::get(IEventDispatcher::class));
$backend1 = $this->getBackend();
$backend2 = $this->getBackend(false);
$manager->registerBackend(IStorage::class, $backend1);
Expand All @@ -58,7 +60,7 @@ public function testGetBackendNoUse(): void {
}

public function testGetBackendMultiple(): void {
$manager = new VersionManager();
$manager = new VersionManager(Server::get(IEventDispatcher::class));
$backend1 = $this->getBackend();
$backend2 = $this->getBackend(false);
$backend3 = $this->getBackend();
Expand Down

0 comments on commit e935ddd

Please sign in to comment.