Skip to content

Commit 6260d0a

Browse files
committed
fix(backend): Remove yjs file and all steps when resetting document session
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 <jonas@freesources.org>
1 parent 88f2ce2 commit 6260d0a

File tree

3 files changed

+22
-42
lines changed

3 files changed

+22
-42
lines changed

lib/Command/ResetDocument.php

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323

2424
namespace OCA\Text\Command;
2525

26-
use OCA\Text\Db\DocumentMapper;
27-
use OCA\Text\Db\SessionMapper;
28-
use OCA\Text\Db\StepMapper;
26+
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
2927
use OCA\Text\Service\DocumentService;
3028
use Symfony\Component\Console\Command\Command;
3129
use Symfony\Component\Console\Input\InputArgument;
@@ -34,33 +32,26 @@
3432

3533
class ResetDocument extends Command {
3634
protected DocumentService $documentService;
37-
protected DocumentMapper $documentMapper;
38-
protected StepMapper $stepMapper;
39-
protected SessionMapper $sessionMapper;
4035

41-
public function __construct(DocumentService $documentService, DocumentMapper $documentMapper, StepMapper $stepMapper, SessionMapper $sessionMapper) {
36+
public function __construct(DocumentService $documentService) {
4237
parent::__construct();
43-
4438
$this->documentService = $documentService;
45-
$this->documentMapper = $documentMapper;
46-
$this->stepMapper = $stepMapper;
47-
$this->sessionMapper = $sessionMapper;
4839
}
4940

5041
protected function configure(): void {
5142
$this
5243
->setName('text:reset')
53-
->setDescription('Reset a text document')
44+
->setDescription('Reset a text document session')
5445
->addArgument(
5546
'file-id',
5647
InputArgument::REQUIRED,
57-
'File id of the document to rest'
48+
'File id of the document to reset'
5849
)
5950
->addOption(
60-
'full',
51+
'force',
6152
'f',
6253
null,
63-
'Drop all existing steps and use the currently saved version'
54+
'Reset the document session even with unsaved changes'
6455
)
6556
;
6657
}
@@ -72,27 +63,23 @@ protected function configure(): void {
7263
*/
7364
protected function execute(InputInterface $input, OutputInterface $output): int {
7465
$fileId = $input->getArgument('file-id');
75-
$fullReset = $input->getOption('full');
66+
$fullReset = $input->getOption('force');
7667

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

8172
return 0;
82-
} else {
83-
$output->writeln('Trying to restore to last saved version');
84-
$document = $this->documentMapper->find($fileId);
85-
$deleted = $this->stepMapper->deleteAfterVersion($fileId, $document->getLastSavedVersion());
86-
if ($deleted > 0) {
87-
$this->sessionMapper->deleteByDocumentId($fileId);
88-
$output->writeln('Reverted document to the last saved version');
89-
90-
return 0;
91-
} else {
92-
$output->writeln('Failed revert changes that are newer than the last saved version');
93-
}
73+
}
9474

75+
$output->writeln('Reset the document session for file ' . $fileId);
76+
try {
77+
$this->documentService->resetDocument($fileId);
78+
} catch (DocumentHasUnsavedChangesException) {
79+
$output->writeln('Not resetting due to unsaved changes');
9580
return 1;
9681
}
82+
83+
return 0;
9784
}
9885
}

lib/Cron/Cleanup.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
namespace OCA\Text\Cron;
3030

31-
use OCA\Text\Service\AttachmentService;
31+
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
3232
use OCA\Text\Service\DocumentService;
3333
use OCA\Text\Service\SessionService;
3434
use OCP\AppFramework\Utility\ITimeFactory;
@@ -39,26 +39,22 @@ class Cleanup extends TimedJob {
3939
private SessionService $sessionService;
4040
private DocumentService $documentService;
4141
private LoggerInterface $logger;
42-
private AttachmentService $attachmentService;
4342

4443
public function __construct(ITimeFactory $time,
4544
SessionService $sessionService,
4645
DocumentService $documentService,
47-
AttachmentService $attachmentService,
4846
LoggerInterface $logger) {
4947
parent::__construct($time);
5048
$this->sessionService = $sessionService;
5149
$this->documentService = $documentService;
52-
$this->attachmentService = $attachmentService;
5350
$this->logger = $logger;
5451
$this->setInterval(SessionService::SESSION_VALID_TIME);
5552
}
5653

5754
/**
5855
* @param array $argument
59-
* @return void
6056
*/
61-
protected function run($argument) {
57+
protected function run($argument): void {
6258
$this->logger->debug('Run cleanup job for text documents');
6359
$documents = $this->documentService->getAll();
6460
foreach ($documents as $document) {
@@ -69,11 +65,10 @@ protected function run($argument) {
6965
continue;
7066
}
7167

72-
if ($this->documentService->hasUnsavedChanges($document)) {
73-
continue;
68+
try {
69+
$this->documentService->resetDocument($document->getId());
70+
} catch (DocumentHasUnsavedChangesException) {
7471
}
75-
76-
$this->documentService->resetDocument($document->getId());
7772
}
7873

7974
$this->logger->debug('Run cleanup job for text sessions');

lib/Service/DocumentService.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,8 @@ public function resetDocument(int $documentId, bool $force = false): void {
432432
$this->stepMapper->deleteAll($documentId);
433433
$this->sessionMapper->deleteByDocumentId($documentId);
434434
$this->documentMapper->delete($document);
435+
$this->getStateFile($documentId)->delete();
435436

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

0 commit comments

Comments
 (0)