Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix(backend): Remove yjs file and all steps when resetting document s…
…ession

Instead of just deleting the newest steps, always remove all session
data: document, sessions and steps from the database as well as the yjs
(document state) file.

Without the `--force` option, don't reset document sessions with unsaved
steps.

Signed-off-by: Jonas <[email protected]>
  • Loading branch information
mejo- committed Mar 14, 2024
commit eded54c95117b23e111527ff7eaef3a3afb5bc0f
45 changes: 16 additions & 29 deletions lib/Command/ResetDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

namespace OCA\Text\Command;

use OCA\Text\Db\DocumentMapper;
use OCA\Text\Db\SessionMapper;
use OCA\Text\Db\StepMapper;
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
use OCA\Text\Service\DocumentService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -34,33 +32,26 @@

class ResetDocument extends Command {
protected DocumentService $documentService;
protected DocumentMapper $documentMapper;
protected StepMapper $stepMapper;
protected SessionMapper $sessionMapper;

public function __construct(DocumentService $documentService, DocumentMapper $documentMapper, StepMapper $stepMapper, SessionMapper $sessionMapper) {
public function __construct(DocumentService $documentService) {
parent::__construct();

$this->documentService = $documentService;
$this->documentMapper = $documentMapper;
$this->stepMapper = $stepMapper;
$this->sessionMapper = $sessionMapper;
}

protected function configure(): void {
$this
->setName('text:reset')
->setDescription('Reset a text document')
->setDescription('Reset a text document session to the current file content')
->addArgument(
'file-id',
InputArgument::REQUIRED,
'File id of the document to rest'
'File id of the document to reset'
)
->addOption(
'full',
'force',
'f',
null,
'Drop all existing steps and use the currently saved version'
'Reset the document session even with unsaved changes'
)
;
}
Expand All @@ -72,27 +63,23 @@ protected function configure(): void {
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$fileId = $input->getArgument('file-id');
$fullReset = $input->getOption('full');
$fullReset = $input->getOption('force');

if ($fullReset) {
$output->writeln('Full document reset');
$output->writeln('Force-reset the document session for file ' . $fileId);
$this->documentService->resetDocument($fileId, true);

return 0;
} else {
$output->writeln('Trying to restore to last saved version');
$document = $this->documentMapper->find($fileId);
$deleted = $this->stepMapper->deleteAfterVersion($fileId, $document->getLastSavedVersion());
if ($deleted > 0) {
$this->sessionMapper->deleteByDocumentId($fileId);
$output->writeln('Reverted document to the last saved version');

return 0;
} else {
$output->writeln('Failed revert changes that are newer than the last saved version');
}
}

$output->writeln('Reset the document session for file ' . $fileId);
try {
$this->documentService->resetDocument($fileId);
} catch (DocumentHasUnsavedChangesException) {
$output->writeln('Not resetting due to unsaved changes');
return 1;
}

return 0;
}
}
15 changes: 5 additions & 10 deletions lib/Cron/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

namespace OCA\Text\Cron;

use OCA\Text\Service\AttachmentService;
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
use OCA\Text\Service\DocumentService;
use OCA\Text\Service\SessionService;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -39,26 +39,22 @@ class Cleanup extends TimedJob {
private SessionService $sessionService;
private DocumentService $documentService;
private LoggerInterface $logger;
private AttachmentService $attachmentService;

public function __construct(ITimeFactory $time,
SessionService $sessionService,
DocumentService $documentService,
AttachmentService $attachmentService,
LoggerInterface $logger) {
parent::__construct($time);
$this->sessionService = $sessionService;
$this->documentService = $documentService;
$this->attachmentService = $attachmentService;
$this->logger = $logger;
$this->setInterval(SessionService::SESSION_VALID_TIME);
}

/**
* @param array $argument
* @return void
*/
protected function run($argument) {
protected function run($argument): void {
$this->logger->debug('Run cleanup job for text documents');
$documents = $this->documentService->getAll();
foreach ($documents as $document) {
Expand All @@ -69,11 +65,10 @@ protected function run($argument) {
continue;
}

if ($this->documentService->hasUnsavedChanges($document)) {
continue;
try {
$this->documentService->resetDocument($document->getId());
} catch (DocumentHasUnsavedChangesException) {
}

$this->documentService->resetDocument($document->getId());
}

$this->logger->debug('Run cleanup job for text sessions');
Expand Down
4 changes: 1 addition & 3 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,8 @@ public function resetDocument(int $documentId, bool $force = false): void {
$this->stepMapper->deleteAll($documentId);
$this->sessionMapper->deleteByDocumentId($documentId);
$this->documentMapper->delete($document);
$this->getStateFile($documentId)->delete();

if ($force) {
$this->getStateFile($documentId)->delete();
}
$this->logger->debug('document reset for ' . $documentId);
} catch (DoesNotExistException|NotFoundException $e) {
// Ignore if document not found or state file not found
Expand Down