Skip to content

Build your personal knowledge base with Trilium Notes

sync fixes and refactorings

zadame8b52f9e6c23
diff --git a/src/becca/entities/abstract_becca_entity.js b/src/becca/entities/abstract_becca_entity.js
index 9fdc189b06..cb539ef5d6 100644
--- a/src/becca/entities/abstract_becca_entity.js
+++ b/src/becca/entities/abstract_becca_entity.js
@@ -61,8 +61,8 @@ class AbstractBeccaEntity {
}
/** @protected */
- addEntityChange(isDeleted = false) {
- entityChangesService.addEntityChange({
+ putEntityChange(isDeleted = false) {
+ entityChangesService.putEntityChange({
entityName: this.constructor.entityName,
entityId: this[this.constructor.primaryKeyName],
hash: this.generateHash(isDeleted),
@@ -101,7 +101,7 @@ class AbstractBeccaEntity {
return;
}
- this.addEntityChange(false);
+ this.putEntityChange(false);
if (!cls.isEntityEventsDisabled()) {
const eventPayload = {
@@ -219,7 +219,7 @@ class AbstractBeccaEntity {
// access to the decrypted content
const hash = blobService.calculateContentHash(pojo);
- entityChangesService.addEntityChange({
+ entityChangesService.putEntityChange({
entityName: 'blobs',
entityId: newBlobId,
hash: hash,
@@ -279,7 +279,7 @@ class AbstractBeccaEntity {
log.info(`Marking ${entityName} ${entityId} as deleted`);
- this.addEntityChange(true);
+ this.putEntityChange(true);
eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this });
}
@@ -296,7 +296,7 @@ class AbstractBeccaEntity {
log.info(`Marking ${entityName} ${entityId} as deleted`);
- this.addEntityChange(true);
+ this.putEntityChange(true);
eventService.emit(eventService.ENTITY_DELETED, { entityName, entityId, entity: this });
}
diff --git a/src/etapi/branches.js b/src/etapi/branches.js
index c62caf0360..a0141bd408 100644
--- a/src/etapi/branches.js
+++ b/src/etapi/branches.js
@@ -75,7 +75,7 @@ function register(router) {
eu.route(router, 'post' ,'/etapi/refresh-note-ordering/:parentNoteId', (req, res, next) => {
eu.getAndCheckNote(req.params.parentNoteId);
- entityChangesService.addNoteReorderingEntityChange(req.params.parentNoteId, "etapi");
+ entityChangesService.putNoteReorderingEntityChange(req.params.parentNoteId, "etapi");
res.sendStatus(204);
});
diff --git a/src/routes/api/branches.js b/src/routes/api/branches.js
index f61b92147e..0af2ae7a01 100644
--- a/src/routes/api/branches.js
+++ b/src/routes/api/branches.js
@@ -72,7 +72,7 @@ function moveBranchBeforeNote(req) {
treeService.sortNotesIfNeeded(parentNote.noteId);
// if sorting is not needed, then still the ordering might have changed above manually
- entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
+ entityChangesService.putNoteReorderingEntityChange(parentNote.noteId);
log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} before note ${beforeBranch.noteId}, branch ${beforeBranchId}`);
@@ -123,7 +123,7 @@ function moveBranchAfterNote(req) {
treeService.sortNotesIfNeeded(parentNote.noteId);
// if sorting is not needed, then still the ordering might have changed above manually
- entityChangesService.addNoteReorderingEntityChange(parentNote.noteId);
+ entityChangesService.putNoteReorderingEntityChange(parentNote.noteId);
log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} after note ${afterNote.noteId}, branch ${afterBranchId}`);
diff --git a/src/services/cloning.js b/src/services/cloning.js
index cdf7b36534..ed1c242141 100644
--- a/src/services/cloning.js
+++ b/src/services/cloning.js
@@ -161,7 +161,7 @@ function cloneNoteAfter(noteId, afterBranchId) {
sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
- eventChangesService.addNoteReorderingEntityChange(afterNote.parentNoteId);
+ eventChangesService.putNoteReorderingEntityChange(afterNote.parentNoteId);
const branch = new BBranch({
noteId: noteId,
diff --git a/src/services/cls.js b/src/services/cls.js
index 462f13e8ef..b5fe379f9b 100644
--- a/src/services/cls.js
+++ b/src/services/cls.js
@@ -56,7 +56,7 @@ function getAndClearEntityChangeIds() {
return entityChangeIds;
}
-function addEntityChange(entityChange) {
+function putEntityChange(entityChange) {
if (namespace.get('ignoreEntityChangeIds')) {
return;
}
@@ -91,6 +91,6 @@ module.exports = {
isEntityEventsDisabled,
reset,
getAndClearEntityChangeIds,
- addEntityChange,
+ putEntityChange,
ignoreEntityChangeIds,
};
diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js
index 512f16325c..954f174200 100644
--- a/src/services/consistency_checks.js
+++ b/src/services/consistency_checks.js
@@ -414,7 +414,7 @@ class ConsistencyChecks {
const hash = utils.hash(utils.randomString(10));
- entityChangesService.addEntityChange({
+ entityChangesService.putEntityChange({
entityName: 'blobs',
entityId: blobId,
hash: hash,
@@ -605,7 +605,7 @@ class ConsistencyChecks {
const entityRow = sql.getRow(`SELECT * FROM ${entityName} WHERE ${key} = ?`, [entityId]);
if (this.autoFix) {
- entityChangesService.addEntityChange({
+ entityChangesService.putEntityChange({
entityName,
entityId,
hash: utils.randomString(10), // doesn't matter, will force sync, but that's OK
diff --git a/src/services/content_hash.js b/src/services/content_hash.js
index e7c4a5d6f3..1c1d5c4d5e 100644
--- a/src/services/content_hash.js
+++ b/src/services/content_hash.js
@@ -31,10 +31,6 @@ function getEntityHashes() {
const sector = entityId[0];
- if (entityName === 'revisions' && sector === '5') {
- console.log(entityId, hash, isErased);
- }
-
// if the entity is erased, its hash is not updated, so it has to be added extra
entityHashMap[sector] = (entityHashMap[sector] || "") + hash + isErased;
}
diff --git a/src/services/entity_changes.js b/src/services/entity_changes.js
index 17c01f588f..cb32563ebc 100644
--- a/src/services/entity_changes.js
+++ b/src/services/entity_changes.js
@@ -9,13 +9,13 @@ const blobService = require("../services/blob");
let maxEntityChangeId = 0;
-function addEntityChangeWithInstanceId(origEntityChange, instanceId) {
+function putEntityChangeWithInstanceId(origEntityChange, instanceId) {
const ec = {...origEntityChange, instanceId};
- return addEntityChange(ec);
+ return putEntityChange(ec);
}
-function addEntityChange(origEntityChange) {
+function putEntityChange(origEntityChange) {
const ec = {...origEntityChange};
delete ec.id;
@@ -32,11 +32,11 @@ function addEntityChange(origEntityChange) {
maxEntityChangeId = Math.max(maxEntityChangeId, ec.id);
- cls.addEntityChange(ec);
+ cls.putEntityChange(ec);
}
-function addNoteReorderingEntityChange(parentNoteId, componentId) {
- addEntityChange({
+function putNoteReorderingEntityChange(parentNoteId, componentId) {
+ putEntityChange({
entityName: "note_reordering",
entityId: parentNoteId,
hash: 'N/A',
@@ -55,10 +55,8 @@ function addNoteReorderingEntityChange(parentNoteId, componentId) {
});
}
-function moveEntityChangeToTop(entityName, entityId) {
- const ec = sql.getRow(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [entityName, entityId]);
-
- addEntityChange(ec);
+function putEntityChangeForOtherInstances(ec) {
+ putEntityChangeWithInstanceId(ec, null);
}
function addEntityChangesForSector(entityName, sector) {
@@ -66,7 +64,7 @@ function addEntityChangesForSector(entityName, sector) {
sql.transactional(() => {
for (const ec of entityChanges) {
- addEntityChange(ec);
+ putEntityChange(ec);
}
});
@@ -128,7 +126,7 @@ function fillEntityChanges(entityName, entityPrimaryKey, condition = '') {
}
}
- addEntityChange(ec);
+ putEntityChange(ec);
}
if (createdCount > 0) {
@@ -157,10 +155,10 @@ function recalculateMaxEntityChangeId() {
}
module.exports = {
- addNoteReorderingEntityChange,
- moveEntityChangeToTop,
- addEntityChange,
- addEntityChangeWithInstanceId,
+ putNoteReorderingEntityChange,
+ putEntityChangeForOtherInstances,
+ putEntityChange,
+ putEntityChangeWithInstanceId,
fillAllEntityChanges,
addEntityChangesForSector,
getMaxEntityChangeId: () => maxEntityChangeId,
diff --git a/src/services/erase.js b/src/services/erase.js
index 790a314e55..b9e124a2c9 100644
--- a/src/services/erase.js
+++ b/src/services/erase.js
@@ -39,7 +39,7 @@ function setEntityChangesAsErased(entityChanges) {
ec.isErased = true;
ec.utcDateChanged = dateUtils.utcNowDateTime();
- entityChangesService.addEntityChange(ec);
+ entityChangesService.putEntityChange(ec);
}
}
diff --git a/src/services/notes.js b/src/services/notes.js
index 3272b4b71b..cd74b08a62 100644
--- a/src/services/notes.js
+++ b/src/services/notes.js
@@ -265,7 +265,7 @@ function createNewNoteWithTarget(target, targetBranchId, params) {
const retObject = createNewNote(params);
- entityChangesService.addNoteReorderingEntityChange(params.parentNoteId);
+ entityChangesService.putNoteReorderingEntityChange(params.parentNoteId);
return retObject;
}
diff --git a/src/services/sync_update.js b/src/services/sync_update.js
index 86b2c5b580..b8a12cf249 100644
--- a/src/services/sync_update.js
+++ b/src/services/sync_update.js
@@ -38,7 +38,7 @@ function updateNormalEntity(remoteEC, remoteEntityRow, instanceId) {
return true;
} else if (localEC?.isErased && !remoteEC.isErased) {
// on this side, we can't unerase the entity, so force the entity to be erased on the other side.
- entityChangesService.addEntityChangeWithInstanceId(localEC, null);
+ entityChangesService.putEntityChangeForOtherInstances(localEC);
return false;
}
@@ -62,12 +62,12 @@ function updateNormalEntity(remoteEC, remoteEntityRow, instanceId) {
sql.replace(remoteEC.entityName, remoteEntityRow);
- entityChangesService.addEntityChangeWithInstanceId(remoteEC, instanceId);
+ entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId);
return true;
} else if (localEC.hash !== remoteEC.hash && localEC.utcDateChanged > remoteEC.utcDateChanged) {
// the change on our side is newer than on the other side, so the other side should update
- entityChangesService.addEntityChangeWithInstanceId(localEC, null);
+ entityChangesService.putEntityChangeForOtherInstances(localEC);
return false;
}
@@ -80,7 +80,7 @@ function updateNoteReordering(remoteEC, remoteEntityRow, instanceId) {
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remoteEntityRow[key], key]);
}
- entityChangesService.addEntityChangeWithInstanceId(remoteEC, instanceId);
+ entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId);
return true;
}
@@ -106,7 +106,7 @@ function eraseEntity(entityChange, instanceId) {
sql.execute(`DELETE FROM ${entityName} WHERE ${primaryKeyName} = ?`, [entityId]);
- entityChangesService.addEntityChangeWithInstanceId(entityChange, instanceId);
+ entityChangesService.putEntityChangeWithInstanceId(entityChange, instanceId);
}
module.exports = {
diff --git a/src/services/tree.js b/src/services/tree.js
index 0feeba2642..920e265618 100644
--- a/src/services/tree.js
+++ b/src/services/tree.js
@@ -165,7 +165,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
}
if (someBranchUpdated) {
- entityChangesService.addNoteReorderingEntityChange(parentNoteId);
+ entityChangesService.putNoteReorderingEntityChange(parentNoteId);
}
});
}