Merge pull request #53746 from nextcloud/add-VersionCreatedEvent

This commit is contained in:
John Molakvoæ 2025-07-29 17:43:23 +02:00 committed by GitHub
commit 26c3c1feff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 63 additions and 5 deletions

View file

@ -17,6 +17,7 @@ return array(
'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\\VersionCreatedEvent' => $baseDir . '/../lib/Events/VersionCreatedEvent.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',

View file

@ -32,6 +32,7 @@ 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\\VersionCreatedEvent' => __DIR__ . '/..' . '/../lib/Events/VersionCreatedEvent.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',

View file

@ -0,0 +1,39 @@
<?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 OCA\Files_Versions\Versions\IVersion;
use OCP\EventDispatcher\Event;
use OCP\Files\Node;
/**
* Event dispatched after a successful creation of a version
*/
class VersionCreatedEvent extends Event {
public function __construct(
private Node $node,
private IVersion $version,
) {
parent::__construct();
}
/**
* Node of the file that has been versioned
*/
public function getNode(): Node {
return $this->node;
}
/**
* Version of the file that was created
*/
public function getVersion(): IVersion {
return $this->version;
}
}

View file

@ -8,13 +8,18 @@ declare(strict_types=1);
*/
namespace OCA\Files_Versions\Versions;
use OCA\Files_Versions\Db\VersionEntity;
use OCP\Files\File;
/**
* @since 28.0.0
*/
interface INeedSyncVersionBackend {
public function createVersionEntity(File $file): void;
/**
* TODO: Convert return type to strong type once all implementations are fixed.
* @return null|VersionEntity
*/
public function createVersionEntity(File $file);
public function updateVersionEntity(File $sourceFile, int $revision, array $properties): void;
public function deleteVersionsEntity(File $file): void;
}

View file

@ -229,7 +229,7 @@ class LegacyVersionsBackend implements IVersionBackend, IDeletableVersionBackend
$this->versionsMapper->delete($versionEntity);
}
public function createVersionEntity(File $file): void {
public function createVersionEntity(File $file): ?VersionEntity {
$versionEntity = new VersionEntity();
$versionEntity->setFileId($file->getId());
$versionEntity->setTimestamp($file->getMTime());
@ -241,8 +241,7 @@ class LegacyVersionsBackend implements IVersionBackend, IDeletableVersionBackend
while ($tries < 5) {
try {
$this->versionsMapper->insert($versionEntity);
/* No errors, get out of the method */
return;
return $versionEntity;
} catch (\OCP\DB\Exception $e) {
if (!in_array($e->getReason(), [
\OCP\DB\Exception::REASON_CONSTRAINT_VIOLATION,
@ -257,6 +256,8 @@ class LegacyVersionsBackend implements IVersionBackend, IDeletableVersionBackend
$this->logger->warning('Constraint violation while inserting version, retrying with increased timestamp', ['exception' => $e]);
}
}
return null;
}
public function updateVersionEntity(File $sourceFile, int $revision, array $properties): void {

View file

@ -8,6 +8,8 @@ declare(strict_types=1);
*/
namespace OCA\Files_Versions\Versions;
use OCA\Files_Versions\Db\VersionEntity;
use OCA\Files_Versions\Events\VersionCreatedEvent;
use OCA\Files_Versions\Events\VersionRestoredEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\File;
@ -129,7 +131,16 @@ class VersionManager implements IVersionManager, IDeletableVersionBackend, INeed
public function createVersionEntity(File $file): void {
$backend = $this->getBackendForStorage($file->getStorage());
if ($backend instanceof INeedSyncVersionBackend) {
$backend->createVersionEntity($file);
$versionEntity = $backend->createVersionEntity($file);
if ($versionEntity instanceof VersionEntity) {
foreach ($backend->getVersionsForFile($file->getOwner(), $file) as $version) {
if ($version->getRevisionId() === $versionEntity->getTimestamp()) {
$this->dispatcher->dispatchTyped(new VersionCreatedEvent($file, $version));
break;
}
}
}
}
}