Skip to content

Commit

Permalink
Merge pull request #5890 from nextcloud/backport/5887/stable28
Browse files Browse the repository at this point in the history
[stable28] Reset all sessions (in occ command and on upgrade)
  • Loading branch information
mejo- authored Jun 12, 2024
2 parents ba9e139 + 26501e1 commit 8b75a67
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
- **💾 Open format:** Files are saved as [Markdown](https://en.wikipedia.org/wiki/Markdown), so you can edit them from any other text app too.
- **✊ Strong foundation:** We use [🐈 tiptap](https://tiptap.scrumpy.io) which is based on [🦉 ProseMirror](https://prosemirror.net) – huge thanks to them!
]]></description>
<version>3.9.1</version>
<version>3.9.2</version>
<licence>agpl</licence>
<author mail="jus@bitgrid.net">Julius Härtl</author>
<namespace>Text</namespace>
Expand Down
56 changes: 39 additions & 17 deletions lib/Command/ResetDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Text\Command;

use OCA\Text\Db\Document;
use OCA\Text\Exception\DocumentHasUnsavedChangesException;
use OCA\Text\Service\DocumentService;
use Symfony\Component\Console\Command\Command;
Expand All @@ -44,9 +45,15 @@ protected function configure(): void {
->setDescription('Reset a text document session to the current file content')
->addArgument(
'file-id',
InputArgument::REQUIRED,
InputArgument::OPTIONAL,
'File id of the document to reset'
)
->addOption(
'all',
'a',
null,
'Reset all document sessions'
)
->addOption(
'force',
'f',
Expand All @@ -56,30 +63,45 @@ protected function configure(): void {
;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$fileId = $input->getArgument('file-id');
$all = $input->getOption('all');
$fullReset = $input->getOption('force');

if ($fullReset) {
$output->writeln('Force-reset the document session for file ' . $fileId);
$this->documentService->resetDocument($fileId, true);
if (!$fileId && !$all) {
$output->writeln('<error>Either --all option or file-id argument is required.</error>');
return 1;
}
if ($fileId && $all) {
$output->writeln('<error>The --all option and file id argument are exclusionary.</error>');
return 1;
}

return 0;
if ($all) {
$fileIds = array_map(static function (Document $document) {
return $document->getId();
}, $this->documentService->getAll());
} else {
$fileIds = [$fileId];
}

$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;
$rc = 0;
foreach ($fileIds as $id) {
if ($fullReset) {
$output->writeln('Force-reset the document session for file ' . $id);
$this->documentService->resetDocument($id, true);
continue;
}

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

return 0;
return $rc;
}
}
30 changes: 12 additions & 18 deletions lib/Migration/ResetSessionsBeforeYjs.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,48 @@

namespace OCA\Text\Migration;

use OCA\Text\Db\SessionMapper;
use OCA\Text\Db\Document;
use OCA\Text\Service\DocumentService;
use OCP\IConfig;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class ResetSessionsBeforeYjs implements IRepairStep {
private IConfig $config;
private SessionMapper $sessionMapper;
private DocumentService $documentService;

public function __construct(IConfig $config,
SessionMapper $sessionMapper,
DocumentService $documentService) {
$this->config = $config;
$this->sessionMapper = $sessionMapper;
$this->documentService = $documentService;
}

/**
* @return string
*/
public function getName(): string {
return 'Force-reset all Text sessions before Yjs migration';
return 'Force-reset all Text document sessions';
}

/**
* @param IOutput $output
*
* @return void
*/
public function run(IOutput $output): void {
$appVersion = $this->config->getAppValue('text', 'installed_version');

if (!$appVersion || version_compare($appVersion, '3.7.2') !== -1) {
if (!$appVersion || version_compare($appVersion, '3.9.2') !== -1) {
return;
}

$sessions = $this->sessionMapper->findAllDocuments();
if (!$sessions) {
$fileIds = array_map(static function (Document $document) {
return $document->getId();
}, $this->documentService->getAll());

if (!$fileIds) {
return;
}

$output->startProgress(count($sessions));
foreach ($sessions as $session) {
$documentId = $session->getDocumentId();
$this->documentService->unlock($documentId);
$this->documentService->resetDocument($documentId, true);
$output->startProgress(count($fileIds));
foreach ($fileIds as $fileId) {
$this->documentService->unlock($fileId);
$this->documentService->resetDocument($fileId, true);
$output->advance();
}
$output->finishProgress();
Expand Down

0 comments on commit 8b75a67

Please sign in to comment.