Skip to content
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
1 change: 1 addition & 0 deletions composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'OCA\\Text\\Listeners\\NodeCopiedListener' => $baseDir . '/../lib/Listeners/NodeCopiedListener.php',
'OCA\\Text\\Listeners\\RegisterDirectEditorEventListener' => $baseDir . '/../lib/Listeners/RegisterDirectEditorEventListener.php',
'OCA\\Text\\Listeners\\RegisterTemplateCreatorListener' => $baseDir . '/../lib/Listeners/RegisterTemplateCreatorListener.php',
'OCA\\Text\\Listeners\\VersionRestoredListener' => $baseDir . '/../lib/Listeners/VersionRestoredListener.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentBaseVersionEtag' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentBaseVersionEtag.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSession' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentSession.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSessionOrUserOrShareToken' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentSessionOrUserOrShareToken.php',
Expand Down
1 change: 1 addition & 0 deletions composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ComposerStaticInitText
'OCA\\Text\\Listeners\\NodeCopiedListener' => __DIR__ . '/..' . '/../lib/Listeners/NodeCopiedListener.php',
'OCA\\Text\\Listeners\\RegisterDirectEditorEventListener' => __DIR__ . '/..' . '/../lib/Listeners/RegisterDirectEditorEventListener.php',
'OCA\\Text\\Listeners\\RegisterTemplateCreatorListener' => __DIR__ . '/..' . '/../lib/Listeners/RegisterTemplateCreatorListener.php',
'OCA\\Text\\Listeners\\VersionRestoredListener' => __DIR__ . '/..' . '/../lib/Listeners/VersionRestoredListener.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentBaseVersionEtag' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentBaseVersionEtag.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSession' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentSession.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSessionOrUserOrShareToken' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentSessionOrUserOrShareToken.php',
Expand Down
30 changes: 4 additions & 26 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Files_Versions\Events\VersionRestoredEvent;
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 @@ -24,9 +24,9 @@
use OCA\Text\Listeners\NodeCopiedListener;
use OCA\Text\Listeners\RegisterDirectEditorEventListener;
use OCA\Text\Listeners\RegisterTemplateCreatorListener;
use OCA\Text\Listeners\VersionRestoredListener;
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 @@ -39,11 +39,7 @@
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 @@ -67,30 +63,12 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(BeforeAssistantNotificationEvent::class, BeforeAssistantNotificationListener::class);
$context->registerEventListener(RegisterTemplateCreatorEvent::class, RegisterTemplateCreatorListener::class);

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

$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
}
}
}
47 changes: 47 additions & 0 deletions lib/Listeners/VersionRestoredListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Text\Listeners;

use OCA\Files_Versions\Events\VersionRestoredEvent;
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
use OCA\Text\Service\DocumentService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\File;
use OCP\Files\NotFoundException;

/**
* @implements IEventListener<Event|VersionRestoredEvent>
*/
class VersionRestoredListener implements IEventListener {
public function __construct(
private DocumentService $documentService,
) {
}

public function handle(Event $event): void {
if (!($event instanceof VersionRestoredEvent)) {
return;
}

$sourceFile = $event->getVersion()->getSourceFile();
if (!$sourceFile instanceof File) {
return;
}

// Reset document session to avoid manual conflict resolution if there's no unsaved steps
try {
$this->documentService->resetDocument($sourceFile->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 on a file that was just created so a NonExistingFile object is used that has no id yet
}
}
}
13 changes: 12 additions & 1 deletion tests/stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ abstract public function setNotificationTarget(?string $notificationTarget): voi
}
}


namespace OCA\NotifyPush\Queue {
interface IQueue {
/**
Expand All @@ -61,3 +60,15 @@ interface IQueue {
public function push(string $channel, $message);
}
}

namespace OCA\Files_Versions\Events {
abstract class VersionRestoredEvent extends \OCP\EventDispatcher\Event {
abstract public function getVersion(): \OCA\Files_Versions\Versions\IVersion;
}
}

namespace OCA\Files_Versions\Versions {
abstract class IVersion {
abstract public function getSourceFile(): \OCP\Files\File;
}
}
Loading