diff --git a/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml b/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml index 6cc1e5572..b6650b657 100644 --- a/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml +++ b/.idea/dataSources/a2c75661-f9e2-478f-a69f-6a9409e69997.xml @@ -2,7 +2,7 @@ - 3.25.1 + 3.16.1 1 @@ -57,6 +57,7 @@ 1 apiTokenId + 1 @@ -125,17 +126,21 @@ 1 attributeId + 1 noteId + name value + value + attributeId @@ -197,14 +202,17 @@ value 1 branchId + 1 noteId parentNoteId + parentNoteId + branchId @@ -235,6 +243,7 @@ parentNoteId 1 noteId + 1 @@ -265,6 +274,7 @@ parentNoteId 1 noteRevisionId + 1 @@ -349,22 +359,28 @@ parentNoteId 1 noteRevisionId + 1 noteId + utcDateLastEdited + utcDateCreated + dateLastEdited + dateCreated + noteRevisionId @@ -446,28 +462,36 @@ parentNoteId 1 noteId + 1 title + type + isDeleted + dateCreated + dateModified + utcDateCreated + utcDateModified + noteId @@ -508,6 +532,7 @@ parentNoteId 1 name + 1 @@ -543,6 +568,7 @@ parentNoteId 1 noteId + 1 @@ -563,10 +589,12 @@ parentNoteId 1 sourceId + 1 utcDateCreated + sourceId @@ -587,7 +615,7 @@ parentNoteId 4 - int|0s + integer|0s 5 @@ -628,10 +656,12 @@ parentNoteId entityName entityId + 1 utcSyncDate + id diff --git a/package-lock.json b/package-lock.json index 07b252c0d..ea19b714f 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.js index cddff0182..8cb12339c 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.js index 21c76ed79..4415a62df 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; + } - if (!origNote || origNote.utcDateModified < entity.utcDateModified || origNote.hash !== entity.hash) { + 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 (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); } }