Skip to content

Commit 8603958

Browse files
committed
fix(context): do not return document from updateDocument
Changing the object and returning it is confusing. Use `$document->getUpdatedFields()` to determine what changed. Signed-off-by: Max <max@nextcloud.com>
1 parent 8c72189 commit 8603958

5 files changed

Lines changed: 24 additions & 26 deletions

File tree

lib/Context/FileContext.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,21 @@ public function isReadOnly(): bool {
143143
* @throws GenericFileException if the file changed and reading the content fails.
144144
* @throws LockedException if the file changed and a lock prevents reading the content.
145145
* @throws NotPermittedException if the file changed and reading is not allowed.
146-
* @return Document|null Updated document if there was an update
147146
*/
148-
public function updateDocument(Document $document): ?Document {
147+
public function updateDocument(Document $document): void {
149148
$lastMTime = $document->getLastSavedVersionTime();
150149
$lastEtag = $document->getLastSavedVersionEtag();
151150

152151
if ($this->isReadOnly()) {
153-
return null;
152+
return;
154153
}
155154

156155
$file = $this->getFile();
157156
$fileMtime = $file->getMtime();
158157
$fileEtag = $file->getEtag();
159158

160159
if ($lastEtag === $fileEtag && $lastMTime === $fileMtime) {
161-
return null;
160+
return;
162161
}
163162

164163
$fileContent = $file->getContent();
@@ -167,7 +166,6 @@ public function updateDocument(Document $document): ?Document {
167166
$document->setChecksum($fileChecksum);
168167
$document->setLastSavedVersionTime($fileMtime);
169168
$document->setLastSavedVersionEtag($fileEtag);
170-
return $document;
171169
}
172170

173171
public function loadContent(): ?string {

lib/Context/IContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function toString(): string;
2525
public function buildDocument(): Document;
2626
public function prepareSession(DocumentData $documentData): SessionInfo;
2727
public function isReadOnly(): bool;
28-
public function updateDocument(Document $document): ?Document;
28+
public function updateDocument(Document $document): void;
2929
/**
3030
* @throws NotFoundException
3131
* @throws NotPermittedException

lib/Context/UnauthorizedFileContext.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ public function isReadOnly(): bool {
7272
* @throws GenericFileException if the file changed and reading the content fails.
7373
* @throws LockedException if the file changed and a lock prevents reading the content.
7474
* @throws NotPermittedException if the file changed and reading is not allowed.
75-
* @return Document|null Updated document if there was an update
7675
*/
77-
public function updateDocument(Document $document): ?Document {
76+
public function updateDocument(Document $document): void {
7877
$lastMTime = $document->getLastSavedVersionTime();
7978
$lastEtag = $document->getLastSavedVersionEtag();
8079

@@ -83,7 +82,7 @@ public function updateDocument(Document $document): ?Document {
8382
$fileEtag = $file->getEtag();
8483

8584
if ($lastEtag === $fileEtag && $lastMTime === $fileMtime) {
86-
return null;
85+
return;
8786
}
8887

8988
$fileContent = $file->getContent();
@@ -92,7 +91,7 @@ public function updateDocument(Document $document): ?Document {
9291
$document->setChecksum($fileChecksum);
9392
$document->setLastSavedVersionTime($fileMtime);
9493
$document->setLastSavedVersionEtag($fileEtag);
95-
return $document;
94+
return;
9695
}
9796

9897
public function loadContent(): ?string {

lib/Listeners/DocumentContentUpdatedListener.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,26 @@ public function handle(Event $event): void {
4343
}
4444

4545
$document = $this->documentMapper->load($context->getType(), $context->getId());
46-
if (!$document || $this->documentService->isSaveFromText()) {
47-
$this->logger->debug('No document or event triggered by text itself', ['document' => $document]);
46+
if (!$document) {
47+
$this->logger->debug('No document for context.', ['context' => $context->toString()]);
4848
return;
4949
}
5050

51-
$oldChecksum = $document->getChecksum();
52-
$updatedDocument = $context->updateDocument($document);
53-
if (!$updatedDocument) {
54-
$this->logger->info('Nothing changed', ['document' => $document]);
55-
// nothing changed.
51+
if ($this->documentService->isSaveFromText()) {
52+
$this->logger->debug('DocumentContentUpdated triggered by text itself', ['document' => $document->jsonSerialize()]);
5653
return;
5754
}
5855

59-
$newChecksum = $updatedDocument->getChecksum();
60-
if ($oldChecksum !== null && $newChecksum !== null && $oldChecksum === $newChecksum) {
61-
// Same content: no need to reset document session. Still update document mtime and etag as they might have changed
62-
$this->logger->info('Same checksum', ['document' => $document->jsonSerialize(), 'updated' => $updatedDocument->jsonSerialize()]);
63-
$this->documentMapper->update($updatedDocument);
56+
$context->updateDocument($document);
57+
if (empty($document->getUpdatedFields())) {
58+
$this->logger->debug('Nothing changed', ['document' => $document->jsonSerialize()]);
59+
return;
60+
}
61+
62+
if (!isset($document->getUpdatedFields()['checksum'])) {
63+
// Same content: no need to reset document session. Still update document mtime and etag.
64+
$this->logger->debug('DocumentContentUpdated with same checksum', ['document' => $document->jsonSerialize()]);
65+
$this->documentMapper->update($document);
6466
return;
6567
}
6668

lib/Service/DocumentService.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,16 +355,15 @@ public function autosave(Document $document, IContext $context, int $version, st
355355

356356
$lastMTime = $document->getLastSavedVersionTime();
357357
if ($lastMTime > 0 && !$force && !$this->cache->get('document-save-lock-' . $document->id)) {
358-
$updatedDocument = $context->updateDocument($document);
359-
if ($updatedDocument !== null) {
358+
$context->updateDocument($document);
359+
if (!empty($document->getUpdatedFields())) {
360360
// Content was overwritten in the meantime and content changed.
361-
if ($updatedDocument->getChecksum() !== $document->getChecksum()) {
361+
if (isset($document->getUpdatedFields()['checksum'])) {
362362
$content = $context->loadContent();
363363
if ($content !== null) {
364364
throw new DocumentSaveConflictException($content);
365365
}
366366
}
367-
$document = $updatedDocument;
368367
$lastMTime = $document->getLastSavedVersionTime();
369368
$this->documentMapper->update($document);
370369
}

0 commit comments

Comments
 (0)