Skip to content
Merged
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
Next Next commit
fix(sync): include document state in response to queries
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud authored and backportbot[bot] committed Nov 26, 2024
commit 31237d65d5f2c3c0d18e31098e53b6d7d11422db
31 changes: 24 additions & 7 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ public function addStep(Document $document, Session $session, array $steps, int
$documentId = $session->getDocumentId();
$readOnly = $this->isReadOnly($this->getFileForSession($session, $shareToken), $shareToken);
$stepsToInsert = [];
$querySteps = [];
$stepsIncludeQuery = false;
$documentState = null;
$newVersion = $version;
foreach ($steps as $step) {
$message = YjsMessage::fromBase64($step);
Expand All @@ -217,7 +218,7 @@ public function addStep(Document $document, Session $session, array $steps, int
}
// Filter out query steps as they would just trigger clients to send their steps again
if ($message->getYjsMessageType() === YjsMessage::YJS_MESSAGE_SYNC && $message->getYjsSyncType() === YjsMessage::YJS_MESSAGE_SYNC_STEP1) {
$querySteps[] = $step;
$stepsIncludeQuery = true;
} else {
$stepsToInsert[] = $step;
}
Expand All @@ -228,10 +229,24 @@ public function addStep(Document $document, Session $session, array $steps, int
}
$newVersion = $this->insertSteps($document, $session, $stepsToInsert);
}
// If there were any queries in the steps send all steps since last save.
$getStepsSinceVersion = count($querySteps) > 0
? $document->getLastSavedVersion()
: $version;

// By default send all steps the user has not received yet.
$getStepsSinceVersion = $version;
if ($stepsIncludeQuery) {
$this->logger->debug('Loading document state for ' . $documentId);
try {
$stateFile = $this->getStateFile($documentId);
$documentState = $stateFile->getContent();
$this->logger->debug('Existing document, state file loaded ' . $documentId);
// If there were any queries in the steps send all steps since last save.
$getStepsSinceVersion = $document->getLastSavedVersion();
} catch (NotFoundException $e) {
$this->logger->debug('Existing document, but no state file found for ' . $documentId);
// If there is no state file include all the steps.
$getStepsSinceVersion = 0;
}
}

$allSteps = $this->getSteps($documentId, $getStepsSinceVersion);
$stepsToReturn = [];
foreach ($allSteps as $step) {
Expand All @@ -240,9 +255,11 @@ public function addStep(Document $document, Session $session, array $steps, int
$stepsToReturn[] = $step;
}
}

return [
'steps' => $stepsToReturn,
'version' => $newVersion
'version' => $newVersion,
'documentState' => $documentState
];
}

Expand Down