diff --git a/lib/Command/ResetDocument.php b/lib/Command/ResetDocument.php index 65a614e2f1a..0864ccb68ec 100644 --- a/lib/Command/ResetDocument.php +++ b/lib/Command/ResetDocument.php @@ -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; @@ -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' ) ; } @@ -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; } } diff --git a/lib/Cron/Cleanup.php b/lib/Cron/Cleanup.php index 19933cfc17c..e374e742f10 100644 --- a/lib/Cron/Cleanup.php +++ b/lib/Cron/Cleanup.php @@ -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; @@ -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) { @@ -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'); diff --git a/lib/Service/DocumentService.php b/lib/Service/DocumentService.php index f2ecab8dc91..39944996a00 100644 --- a/lib/Service/DocumentService.php +++ b/lib/Service/DocumentService.php @@ -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