Skip to content

Commit 1e2f5e4

Browse files
committed
chore: use VersionRestoredEvent instead of deprecated rollback hook
1 parent 8f7b2fe commit 1e2f5e4

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

lib/AppInfo/Application.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OCA\Files\Event\LoadAdditionalScriptsEvent;
1212
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
13+
use OCA\Files_Versions\Events\VersionRestoredEvent;
1314
use OCA\Text\Event\LoadEditor;
1415
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
1516
use OCA\Text\Listeners\AddMissingIndicesListener;
@@ -24,6 +25,7 @@
2425
use OCA\Text\Listeners\NodeCopiedListener;
2526
use OCA\Text\Listeners\RegisterDirectEditorEventListener;
2627
use OCA\Text\Listeners\RegisterTemplateCreatorListener;
28+
use OCA\Text\Listeners\VersionRestoredListener;
2729
use OCA\Text\Middleware\SessionMiddleware;
2830
use OCA\Text\Notification\Notifier;
2931
use OCA\Text\Service\DocumentService;
@@ -67,30 +69,12 @@ public function register(IRegistrationContext $context): void {
6769
$context->registerEventListener(BeforeAssistantNotificationEvent::class, BeforeAssistantNotificationListener::class);
6870
$context->registerEventListener(RegisterTemplateCreatorEvent::class, RegisterTemplateCreatorListener::class);
6971

72+
$context->registerEventListener(VersionRestoredEvent::class, VersionRestoredListener::class);
73+
7074
$context->registerNotifierService(Notifier::class);
7175
$context->registerMiddleware(SessionMiddleware::class);
72-
73-
/** @psalm-suppress DeprecatedMethod */
74-
Util::connectHook('\OCP\Versions', 'rollback', $this, 'resetSessionsAfterRestoreFile');
7576
}
7677

7778
public function boot(IBootContext $context): void {
7879
}
79-
80-
public function resetSessionsAfterRestoreFile(array $params): void {
81-
$node = $params['node'];
82-
if (!$node instanceof File) {
83-
return;
84-
}
85-
86-
$documentService = Server::get(DocumentService::class);
87-
// Reset document session to avoid manual conflict resolution if there's no unsaved steps
88-
try {
89-
$documentService->resetDocument($node->getId());
90-
} catch (DocumentHasUnsavedChangesException|NotFoundException $e) {
91-
// Do not throw during event handling in this is expected to happen
92-
// DocumentHasUnsavedChangesException: A document editing session is likely ongoing, someone can resolve the conflict
93-
// NotFoundException: The event was called oin a file that was just created so a NonExistingFile object is used that has no id yet
94-
}
95-
}
9680
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Text\Listeners;
10+
11+
use OCA\Files_Versions\Events\VersionRestoredEvent;
12+
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
13+
use OCA\Text\Service\DocumentService;
14+
use OCP\EventDispatcher\Event;
15+
use OCP\EventDispatcher\IEventListener;
16+
use OCP\Files\File;
17+
use OCP\Files\NotFoundException;
18+
19+
class VersionRestoredListener implements IEventListener {
20+
public function __construct(
21+
private DocumentService $documentService,
22+
) {
23+
}
24+
25+
public function handle(Event $event): void {
26+
if (!($event instanceof VersionRestoredEvent)) {
27+
return;
28+
}
29+
30+
$sourceFile = $event->getVersion()->getSourceFile();
31+
if (!$sourceFile instanceof File) {
32+
return;
33+
}
34+
35+
// Reset document session to avoid manual conflict resolution if there's no unsaved steps
36+
try {
37+
$this->documentService->resetDocument($sourceFile->getId());
38+
} catch (DocumentHasUnsavedChangesException|NotFoundException $e) {
39+
// Do not throw during event handling in this is expected to happen
40+
// DocumentHasUnsavedChangesException: A document editing session is likely ongoing, someone can resolve the conflict
41+
// NotFoundException: The event was called on a file that was just created so a NonExistingFile object is used that has no id yet
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)