Skip to content

Commit

Permalink
fix: resolve file version conflict during restore
Browse files Browse the repository at this point in the history
Signed-off-by: ailkiv <[email protected]>
  • Loading branch information
AIlkiv committed Feb 27, 2025
1 parent 0bea5f4 commit 940ebc3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Text\Event\LoadEditor;
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
use OCA\Text\Listeners\AddMissingIndicesListener;
use OCA\Text\Listeners\BeforeAssistantNotificationListener;
use OCA\Text\Listeners\BeforeNodeDeletedListener;
Expand All @@ -25,6 +26,7 @@
use OCA\Text\Listeners\RegisterTemplateCreatorListener;
use OCA\Text\Middleware\SessionMiddleware;
use OCA\Text\Notification\Notifier;
use OCA\Text\Service\DocumentService;
use OCA\TpAssistant\Event\BeforeAssistantNotificationEvent;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\App;
Expand All @@ -37,7 +39,11 @@
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\File;
use OCP\Files\NotFoundException;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\Server;
use OCP\Util;

class Application extends App implements IBootstrap {
public const APP_NAME = 'text';
Expand All @@ -63,8 +69,28 @@ public function register(IRegistrationContext $context): void {

$context->registerNotifierService(Notifier::class);
$context->registerMiddleware(SessionMiddleware::class);

/** @psalm-suppress DeprecatedMethod */
Util::connectHook('\OCP\Versions', 'rollback', $this, 'resetSessionsAfterRestoreFile');
}

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

public function resetSessionsAfterRestoreFile(array $params): void {
$node = $params['node'];
if (!$node instanceof File) {
return;
}

$documentService = Server::get(DocumentService::class);
// Reset document session to avoid manual conflict resolution if there's no unsaved steps
try {
$documentService->resetDocument($node->getId());
} catch (DocumentHasUnsavedChangesException|NotFoundException $e) {
// Do not throw during event handling in this is expected to happen
// DocumentHasUnsavedChangesException: A document editing session is likely ongoing, someone can resolve the conflict
// NotFoundException: The event was called oin a file that was just created so a NonExistingFile object is used that has no id yet
}
}
}

0 comments on commit 940ebc3

Please sign in to comment.