Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make sure that rollback hook is triggered on all version backends #36690

Merged
merged 4 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions apps/files_versions/lib/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,6 @@ public static function rollback(string $file, int $revision, IUser $user) {

$node = $userFolder->get($file);

// TODO: move away from those legacy hooks!
\OC_Hook::emit('\OCP\Versions', 'rollback', [
'path' => $filename,
'revision' => $revision,
'node' => $node,
]);
return true;
} elseif ($versionCreated) {
self::deleteVersion($users_view, $version);
Expand Down
30 changes: 17 additions & 13 deletions apps/files_versions/lib/Versions/LegacyVersionsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,46 +69,50 @@ public function getVersionsForFile(IUser $user, FileInfo $file): array {
if ($storage->instanceOfStorage(SharedStorage::class)) {
$owner = $storage->getOwner('');
$user = $this->userManager->get($owner);
}

$userFolder = $this->rootFolder->getUserFolder($user->getUID());
$nodes = $userFolder->getById($file->getId());
$file2 = array_pop($nodes);
$userFolder = $this->rootFolder->getUserFolder($user->getUID());

Check notice

Code scanning / Psalm

PossiblyNullReference

Cannot call method getUID on possibly null value
$nodes = $userFolder->getById($file->getId());
Fixed Show fixed Hide fixed

Check notice

Code scanning / Psalm

PossiblyNullArgument

Argument 1 of OCP\Files\Folder::getById cannot be null, possibly null value provided
$file = array_pop($nodes);

if (!$file) {
throw new NotFoundException("version file not found for share owner");
}
}

$versions = $this->getVersionsForFileFromDB($file2, $user);
$versions = $this->getVersionsForFileFromDB($file, $user);

if (count($versions) > 0) {
return $versions;
}

// Insert the entry in the DB for the current version.
$versionEntity = new VersionEntity();
$versionEntity->setFileId($file2->getId());
$versionEntity->setTimestamp($file2->getMTime());
$versionEntity->setSize($file2->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file2->getMimetype()));
$versionEntity->setFileId($file->getId());

Check notice

Code scanning / Psalm

PossiblyNullArgument

Argument 1 of setFileId cannot be null, possibly null value provided
$versionEntity->setTimestamp($file->getMTime());
$versionEntity->setSize($file->getSize());
$versionEntity->setMimetype($this->mimeTypeLoader->getId($file->getMimetype()));
$versionEntity->setMetadata([]);
$this->versionsMapper->insert($versionEntity);

// Insert entries in the DB for existing versions.
$versionsOnFS = Storage::getVersions($user->getUID(), $userFolder->getRelativePath($file2->getPath()));
$versionsOnFS = Storage::getVersions($user->getUID(), $userFolder->getRelativePath($file->getPath()));

Check notice

Code scanning / Psalm

PossiblyUndefinedVariable

Possibly undefined variable $userFolder, first seen on line 73
foreach ($versionsOnFS as $version) {
$versionEntity = new VersionEntity();
$versionEntity->setFileId($file2->getId());
$versionEntity->setFileId($file->getId());

Check notice

Code scanning / Psalm

PossiblyNullArgument

Argument 1 of setFileId cannot be null, possibly null value provided
$versionEntity->setTimestamp((int)$version['version']);
$versionEntity->setSize((int)$version['size']);
$versionEntity->setMimetype($this->mimeTypeLoader->getId($version['mimetype']));
$versionEntity->setMetadata([]);
$this->versionsMapper->insert($versionEntity);
}

return $this->getVersionsForFileFromDB($file2, $user);
return $this->getVersionsForFileFromDB($file, $user);
}

/**
* @return IVersion[]
*/
private function getVersionsForFileFromDB(Node $file, IUser $user): array {
private function getVersionsForFileFromDB(FileInfo $file, IUser $user): array {
$userFolder = $this->rootFolder->getUserFolder($user->getUID());

return array_map(
Expand Down
11 changes: 10 additions & 1 deletion apps/files_versions/lib/Versions/VersionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ public function createVersion(IUser $user, FileInfo $file) {

public function rollback(IVersion $version) {
$backend = $version->getBackend();
return $backend->rollback($version);
$result = $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(),
]);
}
return $result;
}

public function read(IVersion $version) {
Expand Down
8 changes: 7 additions & 1 deletion apps/files_versions/tests/VersioningTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OC\Files\Storage\Temporary;
use OCA\Files_Versions\Db\VersionEntity;
use OCA\Files_Versions\Db\VersionsMapper;
use OCA\Files_Versions\Versions\IVersionManager;
use OCP\Files\IMimeTypeLoader;
use OCP\IConfig;
use OCP\IUser;
Expand Down Expand Up @@ -822,7 +823,12 @@ private function doTestRestore() {
$params = [];
$this->connectMockHooks('rollback', $params);

$this->assertTrue(\OCA\Files_Versions\Storage::rollback('sub/test.txt', $t2, $this->user1));
$versionManager = \OCP\Server::get(IVersionManager::class);
$versions = $versionManager->getVersionsForFile($this->user1, $info1);
$version = array_filter($versions, function ($version) use ($t2) {
return $version->getRevisionId() === $t2;
});
$this->assertTrue($versionManager->rollback(current($version)));
$expectedParams = [
'path' => '/sub/test.txt',
];
Expand Down