Skip to content

Commit

Permalink
fix(sync): include document state in response to queries
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Nov 18, 2024
1 parent 6b030d5 commit 620ac7a
Showing 1 changed file with 24 additions and 7 deletions.
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

0 comments on commit 620ac7a

Please sign in to comment.