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
more sync recovery fixes
zadamf75c00815490
diff --git a/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml b/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xmlindex 6cc1e5572e..b6650b6578 100644--- a/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml+++ b/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml@@ -2,7 +2,7 @@<dataSource name="document.db"><database-model serializer="dbm" dbms="SQLITE" family-id="SQLITE" format-version="4.17"><root id="1">- <ServerVersion>3.25.1</ServerVersion>+ <ServerVersion>3.16.1</ServerVersion></root><schema id="2" parent="1" name="main"><Current>1</Current>@@ -57,6 +57,7 @@<index id="24" parent="6" name="sqlite_autoindex_api_tokens_1"><NameSurrogate>1</NameSurrogate><ColNames>apiTokenId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><key id="25" parent="6">@@ -125,17 +126,21 @@<index id="37" parent="7" name="sqlite_autoindex_attributes_1"><NameSurrogate>1</NameSurrogate><ColNames>attributeId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><index id="38" parent="7" name="IDX_attributes_noteId_index"><ColNames>noteId</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="39" parent="7" name="IDX_attributes_name_value"><ColNames>namevalue</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="40" parent="7" name="IDX_attributes_value_index"><ColNames>value</ColNames>+ <ColumnCollations></ColumnCollations></index><key id="41" parent="7"><ColNames>attributeId</ColNames>@@ -197,14 +202,17 @@ value</ColNames><index id="52" parent="8" name="sqlite_autoindex_branches_1"><NameSurrogate>1</NameSurrogate><ColNames>branchId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><index id="53" parent="8" name="IDX_branches_noteId_parentNoteId"><ColNames>noteIdparentNoteId</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="54" parent="8" name="IDX_branches_parentNoteId"><ColNames>parentNoteId</ColNames>+ <ColumnCollations></ColumnCollations></index><key id="55" parent="8"><ColNames>branchId</ColNames>@@ -235,6 +243,7 @@ parentNoteId</ColNames><index id="60" parent="9" name="sqlite_autoindex_note_contents_1"><NameSurrogate>1</NameSurrogate><ColNames>noteId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><key id="61" parent="9">@@ -265,6 +274,7 @@ parentNoteId</ColNames><index id="66" parent="10" name="sqlite_autoindex_note_revision_contents_1"><NameSurrogate>1</NameSurrogate><ColNames>noteRevisionId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><key id="67" parent="10">@@ -349,22 +359,28 @@ parentNoteId</ColNames><index id="82" parent="11" name="sqlite_autoindex_note_revisions_1"><NameSurrogate>1</NameSurrogate><ColNames>noteRevisionId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><index id="83" parent="11" name="IDX_note_revisions_noteId"><ColNames>noteId</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="84" parent="11" name="IDX_note_revisions_utcDateLastEdited"><ColNames>utcDateLastEdited</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="85" parent="11" name="IDX_note_revisions_utcDateCreated"><ColNames>utcDateCreated</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="86" parent="11" name="IDX_note_revisions_dateLastEdited"><ColNames>dateLastEdited</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="87" parent="11" name="IDX_note_revisions_dateCreated"><ColNames>dateCreated</ColNames>+ <ColumnCollations></ColumnCollations></index><key id="88" parent="11"><ColNames>noteRevisionId</ColNames>@@ -446,28 +462,36 @@ parentNoteId</ColNames><index id="102" parent="12" name="sqlite_autoindex_notes_1"><NameSurrogate>1</NameSurrogate><ColNames>noteId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><index id="103" parent="12" name="IDX_notes_title"><ColNames>title</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="104" parent="12" name="IDX_notes_type"><ColNames>type</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="105" parent="12" name="IDX_notes_isDeleted"><ColNames>isDeleted</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="106" parent="12" name="IDX_notes_dateCreated"><ColNames>dateCreated</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="107" parent="12" name="IDX_notes_dateModified"><ColNames>dateModified</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="108" parent="12" name="IDX_notes_utcDateCreated"><ColNames>utcDateCreated</ColNames>+ <ColumnCollations></ColumnCollations></index><index id="109" parent="12" name="IDX_notes_utcDateModified"><ColNames>utcDateModified</ColNames>+ <ColumnCollations></ColumnCollations></index><key id="110" parent="12"><ColNames>noteId</ColNames>@@ -508,6 +532,7 @@ parentNoteId</ColNames><index id="117" parent="13" name="sqlite_autoindex_options_1"><NameSurrogate>1</NameSurrogate><ColNames>name</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><key id="118" parent="13">@@ -543,6 +568,7 @@ parentNoteId</ColNames><index id="124" parent="14" name="sqlite_autoindex_recent_notes_1"><NameSurrogate>1</NameSurrogate><ColNames>noteId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><key id="125" parent="14">@@ -563,10 +589,12 @@ parentNoteId</ColNames><index id="128" parent="15" name="sqlite_autoindex_source_ids_1"><NameSurrogate>1</NameSurrogate><ColNames>sourceId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><index id="129" parent="15" name="IDX_source_ids_utcDateCreated"><ColNames>utcDateCreated</ColNames>+ <ColumnCollations></ColumnCollations></index><key id="130" parent="15"><ColNames>sourceId</ColNames>@@ -587,7 +615,7 @@ parentNoteId</ColNames></column><column id="134" parent="16" name="rootpage"><Position>4</Position>- <DataType>int|0s</DataType>+ <DataType>integer|0s</DataType></column><column id="135" parent="16" name="sql"><Position>5</Position>@@ -628,10 +656,12 @@ parentNoteId</ColNames><index id="143" parent="18" name="IDX_sync_entityName_entityId"><ColNames>entityNameentityId</ColNames>+ <ColumnCollations></ColumnCollations><Unique>1</Unique></index><index id="144" parent="18" name="IDX_sync_utcSyncDate"><ColNames>utcSyncDate</ColNames>+ <ColumnCollations></ColumnCollations></index><key id="145" parent="18"><ColNames>id</ColNames>diff --git a/package-lock.json b/package-lock.jsonindex 07b252c0d8..ea19b714f8 100644--- a/package-lock.json+++ b/package-lock.json@@ -805,7 +805,7 @@"dependencies": {"file-type": {"version": "3.9.0",- "resolved": "https://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz",+ "resolved": "http://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz","integrity": "sha1-JXoHg4TR24CHvESdEH1SpSZyuek="}}@@ -1082,7 +1082,7 @@},"uuid": {"version": "2.0.3",- "resolved": "https://registry.npmjs.org/uuid/-/uuid-2.0.3.tgz",+ "resolved": "http://registry.npmjs.org/uuid/-/uuid-2.0.3.tgz","integrity": "sha1-Z+LoY3lyFVMN/zGOW/nc6/1Hsho="}}@@ -1116,7 +1116,7 @@"dependencies": {"semver": {"version": "4.3.6",- "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.6.tgz",+ "resolved": "http://registry.npmjs.org/semver/-/semver-4.3.6.tgz","integrity": "sha1-MAvG4OhjdPe6YQaLWx7NV/xlMto="}}@@ -1136,7 +1136,7 @@},"bl": {"version": "1.2.2",- "resolved": "https://registry.npmjs.org/bl/-/bl-1.2.2.tgz",+ "resolved": "http://registry.npmjs.org/bl/-/bl-1.2.2.tgz","integrity": "sha512-e8tQYnZodmebYDWGH7KMRvtzKXaJHx3BbilrgZCfvyLUYdKpK1t5PSPmpkny/SgiTSCnjfLW7v5rlONXVFkQEA==","requires": {"readable-stream": "^2.3.5",@@ -1376,12 +1376,12 @@"dependencies": {"file-type": {"version": "3.9.0",- "resolved": "https://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz",+ "resolved": "http://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz","integrity": "sha1-JXoHg4TR24CHvESdEH1SpSZyuek="},"uuid": {"version": "2.0.3",- "resolved": "https://registry.npmjs.org/uuid/-/uuid-2.0.3.tgz",+ "resolved": "http://registry.npmjs.org/uuid/-/uuid-2.0.3.tgz","integrity": "sha1-Z+LoY3lyFVMN/zGOW/nc6/1Hsho="}}@@ -1470,7 +1470,7 @@},"readable-stream": {"version": "1.1.14",- "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",+ "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz","integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=","requires": {"core-util-is": "~1.0.0",@@ -1570,7 +1570,7 @@},"chalk": {"version": "1.1.3",- "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz",+ "resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz","integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=","requires": {"ansi-styles": "^2.2.1",@@ -1793,7 +1793,7 @@},"commander": {"version": "2.8.1",- "resolved": "https://registry.npmjs.org/commander/-/commander-2.8.1.tgz",+ "resolved": "http://registry.npmjs.org/commander/-/commander-2.8.1.tgz","integrity": "sha1-Br42f+v9oMMwqh4qBy09yXYkJdQ=","requires": {"graceful-readlink": ">= 1.0.0"@@ -2400,7 +2400,7 @@},"readable-stream": {"version": "1.1.14",- "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",+ "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz","integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=","requires": {"core-util-is": "~1.0.0",@@ -4298,7 +4298,7 @@},"get-stream": {"version": "3.0.0",- "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz",+ "resolved": "http://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz","integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ="},"getpass": {@@ -4565,7 +4565,7 @@},"got": {"version": "5.7.1",- "resolved": "https://registry.npmjs.org/got/-/got-5.7.1.tgz",+ "resolved": "http://registry.npmjs.org/got/-/got-5.7.1.tgz","integrity": "sha1-X4FjWmHkplifGAVp6k44FoClHzU=","requires": {"create-error-class": "^3.0.1",@@ -5184,7 +5184,7 @@},"into-stream": {"version": "3.1.0",- "resolved": "https://registry.npmjs.org/into-stream/-/into-stream-3.1.0.tgz",+ "resolved": "http://registry.npmjs.org/into-stream/-/into-stream-3.1.0.tgz","integrity": "sha1-lvsKk2wSur1v8XUqF9BWFqvQlMY=","requires": {"from2": "^2.1.1",@@ -5346,7 +5346,7 @@},"is-obj": {"version": "1.0.1",- "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-1.0.1.tgz",+ "resolved": "http://registry.npmjs.org/is-obj/-/is-obj-1.0.1.tgz","integrity": "sha1-PkcprB9f3gJc19g6iW2rn09n2w8="},"is-object": {@@ -5809,7 +5809,7 @@},"load-json-file": {"version": "1.1.0",- "resolved": "https://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz",+ "resolved": "http://registry.npmjs.org/load-json-file/-/load-json-file-1.1.0.tgz","integrity": "sha1-lWkFcI1YtLq0wiYbBPWfMcmTdMA=","requires": {"graceful-fs": "^4.1.2",@@ -6262,7 +6262,7 @@},"minimist": {"version": "1.2.0",- "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",+ "resolved": "http://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz","integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ="},"minipass": {@@ -6284,7 +6284,7 @@},"mkdirp": {"version": "0.5.1",- "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz",+ "resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz","integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=","requires": {"minimist": "0.0.8"@@ -6292,7 +6292,7 @@"dependencies": {"minimist": {"version": "0.0.8",- "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",+ "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz","integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="}}@@ -6447,7 +6447,7 @@},"get-stream": {"version": "3.0.0",- "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz",+ "resolved": "http://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz","integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ="},"got": {@@ -6483,7 +6483,7 @@},"p-cancelable": {"version": "0.4.1",- "resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-0.4.1.tgz",+ "resolved": "http://registry.npmjs.org/p-cancelable/-/p-cancelable-0.4.1.tgz","integrity": "sha512-HNa1A8LvB1kie7cERyy21VNeHb2CWJJYqyyC2o3klWFfMGlFmWv2Z7sFgZH8ZiaYL95ydToKTFVXgMV/Os0bBQ=="},"p-event": {@@ -6607,7 +6607,7 @@"dependencies": {"file-type": {"version": "3.9.0",- "resolved": "https://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz",+ "resolved": "http://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz","integrity": "sha1-JXoHg4TR24CHvESdEH1SpSZyuek="}}@@ -6632,7 +6632,7 @@"dependencies": {"get-stream": {"version": "3.0.0",- "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz",+ "resolved": "http://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz","integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ="},"pify": {@@ -6689,7 +6689,7 @@},"get-stream": {"version": "2.3.1",- "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-2.3.1.tgz",+ "resolved": "http://registry.npmjs.org/get-stream/-/get-stream-2.3.1.tgz","integrity": "sha1-Xzj5PzRgCWZu4BUKBUFn+Rvdld4=","requires": {"object-assign": "^4.0.1",@@ -6719,7 +6719,7 @@"dependencies": {"get-stream": {"version": "3.0.0",- "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz",+ "resolved": "http://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz","integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ="}}@@ -6759,7 +6759,7 @@},"pify": {"version": "2.3.0",- "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz",+ "resolved": "http://registry.npmjs.org/pify/-/pify-2.3.0.tgz","integrity": "sha1-7RQaasBDqEnqWISY59yosVMw6Qw="},"prepend-http": {@@ -6864,7 +6864,7 @@},"readable-stream": {"version": "1.1.14",- "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",+ "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz","integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=","requires": {"core-util-is": "~1.0.0",@@ -7265,7 +7265,7 @@},"onetime": {"version": "1.1.0",- "resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz",+ "resolved": "http://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz","integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k="},"open": {@@ -7410,7 +7410,7 @@},"p-is-promise": {"version": "1.1.0",- "resolved": "https://registry.npmjs.org/p-is-promise/-/p-is-promise-1.1.0.tgz",+ "resolved": "http://registry.npmjs.org/p-is-promise/-/p-is-promise-1.1.0.tgz","integrity": "sha1-nJRWmJ6fZYgBewQ01WCXZ1w9oF4="},"p-limit": {@@ -8445,7 +8445,7 @@},"readable-stream": {"version": "1.1.14",- "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz",+ "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz","integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=","dev": true,"requires": {@@ -8535,7 +8535,7 @@},"query-string": {"version": "5.1.1",- "resolved": "https://registry.npmjs.org/query-string/-/query-string-5.1.1.tgz",+ "resolved": "http://registry.npmjs.org/query-string/-/query-string-5.1.1.tgz","integrity": "sha512-gjWOsm2SoGlgLEdAGt7a6slVOk9mGiXmPFMqrEhLQ68rhQuBnpfs3+EmlvqKyxnCo9/PPlF+9MtY02S1aFg+Jw==","requires": {"decode-uri-component": "^0.2.0",@@ -8675,7 +8675,7 @@},"readable-stream": {"version": "2.3.6",- "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz",+ "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz","integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==","requires": {"core-util-is": "~1.0.0",@@ -9462,7 +9462,7 @@},"strip-ansi": {"version": "3.0.1",- "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz",+ "resolved": "http://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz","integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=","requires": {"ansi-regex": "^2.0.0"@@ -9487,7 +9487,7 @@},"strip-dirs": {"version": "1.1.1",- "resolved": "https://registry.npmjs.org/strip-dirs/-/strip-dirs-1.1.1.tgz",+ "resolved": "http://registry.npmjs.org/strip-dirs/-/strip-dirs-1.1.1.tgz","integrity": "sha1-lgu9EoeETzl1pFWKoQOoJV4kVqA=","requires": {"chalk": "^1.0.0",@@ -9660,7 +9660,7 @@},"through": {"version": "2.3.8",- "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz",+ "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz","integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU="},"through2": {@@ -9679,7 +9679,7 @@},"readable-stream": {"version": "1.0.34",- "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz",+ "resolved": "http://registry.npmjs.org/readable-stream/-/readable-stream-1.0.34.tgz","integrity": "sha1-Elgg40vIQtLyqq+v5MKRbuMsFXw=","requires": {"core-util-is": "~1.0.0",diff --git a/src/services/sync.js b/src/services/sync.jsindex cddff01829..8cb12339ce 100644--- a/src/services/sync.js+++ b/src/services/sync.js@@ -251,6 +251,8 @@ async function checkContentHash(syncContext) {await syncTableService.addEntitySyncsForSector(entityName, entityPrimaryKey, sector);await syncRequest(syncContext, 'POST', `/api/sync/queue-sector/${entityName}/${sector}`);++ log.info(`Added sector ${sector} of ${entityName} to sync queue.`);}return failedChecks.length > 0;diff --git a/src/services/sync_update.js b/src/services/sync_update.jsindex 21c76ed793..4415a62df3 100644--- a/src/services/sync_update.js+++ b/src/services/sync_update.js@@ -50,135 +50,155 @@ async function updateEntity(sync, entity, sourceId) {}}-async function updateNote(entity, sourceId) {- const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);+function shouldWeUpdateEntity(localEntity, remoteEntity) {+ if (!localEntity) {+ return true;+ }++ const localDate = localEntity.utcDateModified || localEntity.utcDateCreated;+ const remoteDate = remoteEntity.utcDateModified || remoteEntity.utcDateCreated;++ if (localDate < remoteDate) {+ return true;+ }++ // this can happen in case of sync error when hashes are different but dates are the same - we should still update+ if (localEntity.hash !== remoteEntity.hash && localDate === remoteDate) {+ return true;+ }++ return false;+}++async function updateNote(remoteEntity, sourceId) {+ const localEntity = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [remoteEntity.noteId]);- if (!origNote || origNote.utcDateModified < entity.utcDateModified || origNote.hash !== entity.hash) {+ if (shouldWeUpdateEntity(localEntity, remoteEntity)) {await sql.transactional(async () => {- await sql.replace("notes", entity);+ await sql.replace("notes", remoteEntity);- await syncTableService.addNoteSync(entity.noteId, sourceId);+ await syncTableService.addNoteSync(remoteEntity.noteId, sourceId);});- log.info("Update/sync note " + entity.noteId);+ log.info("Update/sync note " + remoteEntity.noteId);}}-async function updateNoteContent(entity, sourceId) {- const origNoteContent = await sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [entity.noteId]);+async function updateNoteContent(remoteEntity, sourceId) {+ const localEntity = await sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [remoteEntity.noteId]);- if (!origNoteContent || origNoteContent.utcDateModified < entity.utcDateModified || origNoteContent.hash !== entity.hash) {- entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');+ if (shouldWeUpdateEntity(localEntity, remoteEntity)) {+ remoteEntity.content = remoteEntity.content === null ? null : Buffer.from(remoteEntity.content, 'base64');await sql.transactional(async () => {- await sql.replace("note_contents", entity);+ await sql.replace("note_contents", remoteEntity);- await syncTableService.addNoteContentSync(entity.noteId, sourceId);+ await syncTableService.addNoteContentSync(remoteEntity.noteId, sourceId);});- log.info("Update/sync note content for noteId=" + entity.noteId);+ log.info("Update/sync note content for noteId=" + remoteEntity.noteId);}}-async function updateBranch(entity, sourceId) {- const orig = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [entity.branchId]);+async function updateBranch(remoteEntity, sourceId) {+ const localEntity = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [remoteEntity.branchId]);await sql.transactional(async () => {- if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {+ if (shouldWeUpdateEntity(localEntity, remoteEntity)) {// isExpanded is not synced unless it's a new branch instance// otherwise in case of full new sync we'll get all branches (even root) collapsed.- if (orig) {- delete entity.isExpanded;+ if (localEntity) {+ delete remoteEntity.isExpanded;}- await sql.replace('branches', entity);+ await sql.replace('branches', remoteEntity);- await syncTableService.addBranchSync(entity.branchId, sourceId);+ await syncTableService.addBranchSync(remoteEntity.branchId, sourceId);- log.info("Update/sync branch " + entity.branchId);+ log.info("Update/sync branch " + remoteEntity.branchId);}});}-async function updateNoteRevision(entity, sourceId) {- const orig = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]);+async function updateNoteRevision(remoteEntity, sourceId) {+ const localEntity = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);await sql.transactional(async () => {- if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {- await sql.replace('note_revisions', entity);+ if (shouldWeUpdateEntity(localEntity, remoteEntity)) {+ await sql.replace('note_revisions', remoteEntity);- await syncTableService.addNoteRevisionSync(entity.noteRevisionId, sourceId);+ await syncTableService.addNoteRevisionSync(remoteEntity.noteRevisionId, sourceId);- log.info("Update/sync note revision " + entity.noteRevisionId);+ log.info("Update/sync note revision " + remoteEntity.noteRevisionId);}});}-async function updateNoteRevisionContent(entity, sourceId) {- const orig = await sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [entity.noteRevisionId]);+async function updateNoteRevisionContent(remoteEntity, sourceId) {+ const localEntity = await sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);await sql.transactional(async () => {- if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {- entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');+ if (shouldWeUpdateEntity(localEntity, remoteEntity)) {+ remoteEntity.content = remoteEntity.content === null ? null : Buffer.from(remoteEntity.content, 'base64');- await sql.replace('note_revision_contents', entity);+ await sql.replace('note_revision_contents', remoteEntity);- await syncTableService.addNoteRevisionContentSync(entity.noteRevisionId, sourceId);+ await syncTableService.addNoteRevisionContentSync(remoteEntity.noteRevisionId, sourceId);- log.info("Update/sync note revision content " + entity.noteRevisionId);+ log.info("Update/sync note revision content " + remoteEntity.noteRevisionId);}});}-async function updateNoteReordering(entityId, entity, sourceId) {+async function updateNoteReordering(entityId, remote, sourceId) {await sql.transactional(async () => {- for (const key in entity) {- await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [entity[key], key]);+ for (const key in remote) {+ await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remote[key], key]);}await syncTableService.addNoteReorderingSync(entityId, sourceId);});}-async function updateOptions(entity, sourceId) {- const orig = await sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [entity.name]);+async function updateOptions(remoteEntity, sourceId) {+ const localEntity = await sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [remoteEntity.name]);- if (orig && !orig.isSynced) {+ if (localEntity && !localEntity.isSynced) {return;}await sql.transactional(async () => {- if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {- await sql.replace('options', entity);+ if (shouldWeUpdateEntity(localEntity, remoteEntity)) {+ await sql.replace('options', remoteEntity);- await syncTableService.addOptionsSync(entity.name, sourceId);+ await syncTableService.addOptionsSync(remoteEntity.name, sourceId);}});}-async function updateRecentNotes(entity, sourceId) {- const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteId = ?", [entity.noteId]);+async function updateRecentNotes(remoteEntity, sourceId) {+ const localEntity = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteId = ?", [remoteEntity.noteId]);- if (orig === null || orig.utcDateCreated < entity.utcDateCreated || orig.hash !== entity.hash) {+ if (shouldWeUpdateEntity(localEntity, remoteEntity)) {await sql.transactional(async () => {- await sql.replace('recent_notes', entity);+ await sql.replace('recent_notes', remoteEntity);- await syncTableService.addRecentNoteSync(entity.noteId, sourceId);+ await syncTableService.addRecentNoteSync(remoteEntity.noteId, sourceId);});}}-async function updateAttribute(entity, sourceId) {- const origAttribute = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]);+async function updateAttribute(remoteEntity, sourceId) {+ const localEntity = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [remoteEntity.attributeId]);- if (!origAttribute || origAttribute.utcDateModified <= entity.utcDateModified || origAttribute.hash !== entity.hash) {+ if (shouldWeUpdateEntity(localEntity, remoteEntity)) {await sql.transactional(async () => {- await sql.replace("attributes", entity);+ await sql.replace("attributes", remoteEntity);- await syncTableService.addAttributeSync(entity.attributeId, sourceId);+ await syncTableService.addAttributeSync(remoteEntity.attributeId, sourceId);});- log.info("Update/sync attribute " + entity.attributeId);+ log.info("Update/sync attribute " + remoteEntity.attributeId);}}