mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
sync fixes and refactorings
This commit is contained in:
parent
04b125afc0
commit
e8b52f9e6c
@ -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 });
|
||||
}
|
||||
|
@ -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);
|
||||
});
|
||||
|
@ -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}`);
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
};
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -39,7 +39,7 @@ function setEntityChangesAsErased(entityChanges) {
|
||||
ec.isErased = true;
|
||||
ec.utcDateChanged = dateUtils.utcNowDateTime();
|
||||
|
||||
entityChangesService.addEntityChange(ec);
|
||||
entityChangesService.putEntityChange(ec);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -265,7 +265,7 @@ function createNewNoteWithTarget(target, targetBranchId, params) {
|
||||
|
||||
const retObject = createNewNote(params);
|
||||
|
||||
entityChangesService.addNoteReorderingEntityChange(params.parentNoteId);
|
||||
entityChangesService.putNoteReorderingEntityChange(params.parentNoteId);
|
||||
|
||||
return retObject;
|
||||
}
|
||||
|
@ -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 = {
|
||||
|
@ -165,7 +165,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
|
||||
}
|
||||
|
||||
if (someBranchUpdated) {
|
||||
entityChangesService.addNoteReorderingEntityChange(parentNoteId);
|
||||
entityChangesService.putNoteReorderingEntityChange(parentNoteId);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user