Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix document state issues #3842

Merged
merged 3 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-editors.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-editors.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

44 changes: 33 additions & 11 deletions lib/Service/DocumentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,45 @@ public function createDocument(File $file): Document {
}

/**
* @param $document
* @param int $documentId
* @return ISimpleFile
* @throws NotFoundException
*/
public function getStateFile($document): ISimpleFile {
public function getStateFile(int $documentId): ISimpleFile {
$filename = (string)$documentId . '.yjs';
if (!$this->ensureDocumentsFolder()) {
throw new NotFoundException('No app data folder present for text documents');
}
return $this->appData->getFolder('documents')->getFile((string) $document);
return $this->appData->getFolder('documents')->getFile($filename);
}

/**
* @param int $documentId
* @return ISimpleFile
* @throws NotPermittedException
*/
public function createStateFile(int $documentId): ISimpleFile {
$filename = (string)$documentId . '.yjs';
return $this->appData->getFolder('documents')->newFile($filename);
}

/**
* @param int $documentId
* @param string $content
*/
public function writeDocumentState(int $documentId, string $content): void {
try {
$documentStateFile = $this->getStateFile($documentId);
} catch (NotFoundException $e) {
$documentStateFile = $this->createStateFile($documentId);
} catch (NotPermittedException $e) {
$this->logger->error('Failed to create document state file', ['exception' => $e]);
return;
mejo- marked this conversation as resolved.
Show resolved Hide resolved
}
$documentStateFile->putContent($content);
}


public function get($documentId) {
return $this->documentMapper->find($documentId);
}
Expand Down Expand Up @@ -304,22 +332,16 @@ public function autosave(?File $file, int $documentId, int $version, ?string $au
return $document;
}

try {
$documentStateFile = $this->getStateFile((string)$file->getId());
} catch (NotFoundException $e) {
$documentStateFile = $this->appData->getFolder('documents')->newFile((string)$file->getId());
}

$this->cache->set('document-save-lock-' . $documentId, true, 10);
try {
$this->lockManager->runInScope(new LockContext(
$file,
ILock::TYPE_APP,
Application::APP_NAME
), function () use ($file, $autoaveDocument, $documentStateFile, $documentState) {
), function () use ($file, $autoaveDocument, $documentState) {
$file->putContent($autoaveDocument);
if ($documentState) {
$documentStateFile->putContent($documentState);
$this->writeDocumentState($file->getId(), $documentState);
}
});
} catch (LockedException $e) {
Expand Down
10 changes: 5 additions & 5 deletions src/services/SessionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SessionApi {
export class Connection {

#content
#documentumentState
#documentState
#document
#session
#lock
Expand All @@ -64,24 +64,24 @@ export class Connection {
this.#lock = lock
this.#readOnly = readOnly
this.#content = content
this.#documentumentState = documentState
this.#documentState = documentState
this.#options = options
}

get document() {
return this.#document
}

get lastSavedVersion() {
return this.#document.lastSavedVersion
get docStateVersion() {
return this.#documentState ? this.#document.lastSavedVersion : 0
}

get state() {
return {
document: { ...this.#document, readOnly: this.#readOnly },
session: this.#session,
documentSource: this.#content || '',
documentState: this.#documentumentState,
documentState: this.#documentState,
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class SyncService {
// TODO: Only continue if a connection was made
this.connection = await connect

this.version = this.connection.lastSavedVersion
this.version = this.connection.docStateVersion
this.emit('opened', {
...this.connection.state,
version: this.version,
Expand Down