Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(sync): never push a sync stub along with a winning row
Elian Doran289f617aae30
diff --git a/packages/trilium-core/src/services/blob.ts b/packages/trilium-core/src/services/blob.tsindex 1c15720369..c8b873f593 100644--- a/packages/trilium-core/src/services/blob.ts+++ b/packages/trilium-core/src/services/blob.ts@@ -27,11 +27,9 @@ function getBlobPojo(entityName: string, entityId: string, opts?: { preview: booconst pojo = blob.getPojo();- // A sync stub: the blob carries empty content but its (content-derived) blobId is not the hash of- // empty content — i.e. its real content was withheld by the sync server because it exceeded this- // device's `syncMaxBlobContentSize`. Detected before content is decoded/nulled below, off the raw- // stored length. The client shows an "open on server" placeholder instead of empty content.- const isStubbed = pojo.contentLength === 0 && pojo.blobId !== EMPTY_BLOB_ID;+ // Detected before content is decoded/nulled below, off the raw stored length. The client shows an+ // "open on server" placeholder instead of empty content.+ const isStubbed = isSyncStub(pojo.blobId, pojo.contentLength);if (!entity.hasStringContent()) {pojo.content = null;@@ -106,7 +104,7 @@ function getDeletedNoteBlobPojo(noteId: string) {utcDateModified: blobRow.utcDateModified};- const isStubbed = pojo.contentLength === 0 && pojo.blobId !== EMPTY_BLOB_ID;+ const isStubbed = isSyncStub(pojo.blobId, pojo.contentLength);if (!isStringNote(noteRow.type, noteRow.mime)) {pojo.content = null;@@ -208,8 +206,18 @@ function calculateContentHash({ blobId, content, textRepresentation }: Pick<Blobreturn hash(`${blobId}|${content.toString()}${textRepresentationSegment}`);}+/**+ * Whether a stored blob is a sync stub: empty content under a `blobId` that is not the hash of empty+ * content, because the sync server withheld content larger than this device's+ * `syncMaxBlobContentSize`.+ */+function isSyncStub(blobId: string | undefined, contentLength: number) {+ return contentLength === 0 && blobId !== EMPTY_BLOB_ID;+}+export default {getBlobPojo,+ isSyncStub,getDeletedNoteBlobPojo,processContent,encryptTextRepresentation,diff --git a/packages/trilium-core/src/services/sync_update.spec.ts b/packages/trilium-core/src/services/sync_update.spec.tsindex 69ef7fe238..7a2029c2a5 100644--- a/packages/trilium-core/src/services/sync_update.spec.ts+++ b/packages/trilium-core/src/services/sync_update.spec.ts@@ -147,6 +147,23 @@ describe("sync_update service (real DB)", () => {expect(requeuedSince(maxIdBefore, "blobs", localBlobId)).toBe(false);});+ it("does not requeue a sync stub with a newer note, so the remote keeps the full content", () => {+ const noteId = createNote();+ const stubBlobId = "suStubbedBlob";+ cls.init(() => {+ const sql = getSql();+ const now = dateUtils.utcNowDateTime();+ sql.execute("INSERT INTO blobs (blobId, content, dateModified, utcDateModified) VALUES (?, '', ?, ?)", [stubBlobId, now, now]);+ sql.execute("UPDATE notes SET blobId = ? WHERE noteId = ?", [stubBlobId, noteId]);+ });+ const maxIdBefore = maxEntityChangeId();++ apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: PAST }), entity: rowWith(noteId, { blobId: "su_remote_blob" }) }]);++ expect(requeuedSince(maxIdBefore, "notes", noteId)).toBe(true);+ expect(requeuedSince(maxIdBefore, "blobs", stubBlobId)).toBe(false);+ });+it("lets a remote change win against a local change dated in the future", () => {const noteId = createNote();const clockAhead = dateUtils.utcDateTimeStr(new Date(Date.now() + 3600 * 1000));diff --git a/packages/trilium-core/src/services/sync_update.ts b/packages/trilium-core/src/services/sync_update.tsindex 61dbcf20d7..00b92cb84a 100644--- a/packages/trilium-core/src/services/sync_update.ts+++ b/packages/trilium-core/src/services/sync_update.ts@@ -1,5 +1,6 @@import type { EntityChange, EntityChangeRecord, EntityRow } from "@triliumnext/commons";+import blobService from "./blob.js";import entityChangesService from "./entity_changes.js";import { getLog } from "./log.js";import { getSql } from "./sql/index.js";@@ -131,6 +132,7 @@ const BLOB_OWNING_ENTITIES = new Set(["notes", "attachments", "revisions"]);* Sends the local blob along with a local row that won a conflict. The remote side can have erased* that blob in `deleteBlobIfNotUsed()` when its own row moved to another blob, and the blob's own* entity change was pushed long ago, so without this the winning row arrives pointing at nothing.+ * A sync stub is never sent: its empty content would replace the full copy the remote holds.*/function requeueOwnBlobIfRemoteLacksIt(remoteEC: EntityChange, remoteEntityRow: EntityRow | undefined) {if (!BLOB_OWNING_ENTITIES.has(remoteEC.entityName)) {@@ -138,13 +140,17 @@ function requeueOwnBlobIfRemoteLacksIt(remoteEC: EntityChange, remoteEntityRow:}const primaryKeyName = entity_constructor.getEntityFromEntityName(remoteEC.entityName).primaryKeyName;- const localBlobId = getSql().getValue<string>(- /*sql*/`SELECT blobId FROM ${remoteEC.entityName} WHERE ${primaryKeyName} = ?`,+ const localBlob = getSql().getRowOrNull<{ blobId: string; contentLength: number }>(+ /*sql*/`SELECT blobs.blobId, LENGTH(blobs.content) AS contentLength+ FROM ${remoteEC.entityName} JOIN blobs USING (blobId)+ WHERE ${remoteEC.entityName}.${primaryKeyName} = ?`,[remoteEC.entityId]);- if (localBlobId && localBlobId !== remoteEntityRow?.blobId) {- entityChangesService.putBlobEntityChange(localBlobId);+ if (localBlob+ && localBlob.blobId !== remoteEntityRow?.blobId+ && !blobService.isSyncStub(localBlob.blobId, localBlob.contentLength)) {+ entityChangesService.putBlobEntityChange(localBlob.blobId);}}