mirror of
https://github.com/zadam/trilium.git
synced 2025-06-06 18:08:33 +02:00
sync fix, #4288
This commit is contained in:
parent
be918628c3
commit
9f726304aa
@ -208,6 +208,7 @@ class Becca {
|
|||||||
return this.etapiTokens[etapiTokenId];
|
return this.etapiTokens[etapiTokenId];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @returns {AbstractBeccaEntity|null} */
|
||||||
getEntity(entityName, entityId) {
|
getEntity(entityName, entityId) {
|
||||||
if (!entityName || !entityId) {
|
if (!entityName || !entityId) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -18,31 +18,11 @@ let becca = null;
|
|||||||
class AbstractBeccaEntity {
|
class AbstractBeccaEntity {
|
||||||
/** @protected */
|
/** @protected */
|
||||||
beforeSaving() {
|
beforeSaving() {
|
||||||
this.generateIdIfNecessary();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @protected */
|
|
||||||
generateIdIfNecessary() {
|
|
||||||
if (!this[this.constructor.primaryKeyName]) {
|
if (!this[this.constructor.primaryKeyName]) {
|
||||||
this[this.constructor.primaryKeyName] = utils.newEntityId();
|
this[this.constructor.primaryKeyName] = utils.newEntityId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @protected */
|
|
||||||
generateHash(isDeleted = false) {
|
|
||||||
let contentToHash = "";
|
|
||||||
|
|
||||||
for (const propertyName of this.constructor.hashedProperties) {
|
|
||||||
contentToHash += `|${this[propertyName]}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isDeleted) {
|
|
||||||
contentToHash += "|deleted";
|
|
||||||
}
|
|
||||||
|
|
||||||
return utils.hash(contentToHash).substr(0, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @protected */
|
/** @protected */
|
||||||
getUtcDateChanged() {
|
getUtcDateChanged() {
|
||||||
return this.utcDateModified || this.utcDateCreated;
|
return this.utcDateModified || this.utcDateCreated;
|
||||||
@ -61,7 +41,7 @@ class AbstractBeccaEntity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @protected */
|
/** @protected */
|
||||||
putEntityChange(isDeleted = false) {
|
putEntityChange(isDeleted) {
|
||||||
entityChangesService.putEntityChange({
|
entityChangesService.putEntityChange({
|
||||||
entityName: this.constructor.entityName,
|
entityName: this.constructor.entityName,
|
||||||
entityId: this[this.constructor.primaryKeyName],
|
entityId: this[this.constructor.primaryKeyName],
|
||||||
@ -72,11 +52,37 @@ class AbstractBeccaEntity {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
generateHash(isDeleted) {
|
||||||
|
let contentToHash = "";
|
||||||
|
|
||||||
|
for (const propertyName of this.constructor.hashedProperties) {
|
||||||
|
contentToHash += `|${this[propertyName]}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isDeleted) {
|
||||||
|
contentToHash += "|deleted";
|
||||||
|
}
|
||||||
|
|
||||||
|
return utils.hash(contentToHash).substr(0, 10);
|
||||||
|
}
|
||||||
|
|
||||||
/** @protected */
|
/** @protected */
|
||||||
getPojoToSave() {
|
getPojoToSave() {
|
||||||
return this.getPojo();
|
return this.getPojo();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @protected
|
||||||
|
* @abstract
|
||||||
|
*/
|
||||||
|
getPojo() {
|
||||||
|
throw new Error(`Unimplemented getPojo() for entity '${this.constructor.name}'`)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves entity - executes SQL, but doesn't commit the transaction on its own
|
* Saves entity - executes SQL, but doesn't commit the transaction on its own
|
||||||
*
|
*
|
||||||
@ -88,9 +94,7 @@ class AbstractBeccaEntity {
|
|||||||
|
|
||||||
const isNewEntity = !this[primaryKeyName];
|
const isNewEntity = !this[primaryKeyName];
|
||||||
|
|
||||||
if (this.beforeSaving) {
|
this.beforeSaving(opts);
|
||||||
this.beforeSaving(opts);
|
|
||||||
}
|
|
||||||
|
|
||||||
const pojo = this.getPojoToSave();
|
const pojo = this.getPojoToSave();
|
||||||
|
|
||||||
@ -101,7 +105,7 @@ class AbstractBeccaEntity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.putEntityChange(false);
|
this.putEntityChange(!!this.isDeleted);
|
||||||
|
|
||||||
if (!cls.isEntityEventsDisabled()) {
|
if (!cls.isEntityEventsDisabled()) {
|
||||||
const eventPayload = {
|
const eventPayload = {
|
||||||
|
@ -20,8 +20,7 @@ const attachmentRoleToNoteTypeMapping = {
|
|||||||
class BAttachment extends AbstractBeccaEntity {
|
class BAttachment extends AbstractBeccaEntity {
|
||||||
static get entityName() { return "attachments"; }
|
static get entityName() { return "attachments"; }
|
||||||
static get primaryKeyName() { return "attachmentId"; }
|
static get primaryKeyName() { return "attachmentId"; }
|
||||||
static get hashedProperties() { return ["attachmentId", "ownerId", "role", "mime", "title", "blobId",
|
static get hashedProperties() { return ["attachmentId", "ownerId", "role", "mime", "title", "blobId", "utcDateScheduledForErasureSince"]; }
|
||||||
"utcDateScheduledForErasureSince", "utcDateModified"]; }
|
|
||||||
|
|
||||||
constructor(row) {
|
constructor(row) {
|
||||||
super();
|
super();
|
||||||
|
@ -148,12 +148,13 @@ function fillEntityChanges(entityName, entityPrimaryKey, condition = '') {
|
|||||||
const entity = becca.getEntity(entityName, entityId);
|
const entity = becca.getEntity(entityName, entityId);
|
||||||
|
|
||||||
if (entity) {
|
if (entity) {
|
||||||
ec.hash = entity.generateHash() || "|deleted";
|
ec.hash = entity.generateHash();
|
||||||
ec.utcDateChanged = entity.getUtcDateChanged() || dateUtils.utcNowDateTime();
|
ec.utcDateChanged = entity.getUtcDateChanged() || dateUtils.utcNowDateTime();
|
||||||
ec.isSynced = entityName !== 'options' || !!entity.isSynced;
|
ec.isSynced = entityName !== 'options' || !!entity.isSynced;
|
||||||
} else {
|
} else {
|
||||||
// entity might be null (not present in becca) when it's deleted
|
// entity might be null (not present in becca) when it's deleted
|
||||||
// FIXME: hacky, not sure if it might cause some problems
|
// this will produce different hash value than when entity is being deleted since then
|
||||||
|
// all normal hashed attributes are being used. Sync should recover from that, though.
|
||||||
ec.hash = "deleted";
|
ec.hash = "deleted";
|
||||||
ec.utcDateChanged = dateUtils.utcNowDateTime();
|
ec.utcDateChanged = dateUtils.utcNowDateTime();
|
||||||
ec.isSynced = true; // deletable (the ones with isDeleted) entities are synced
|
ec.isSynced = true; // deletable (the ones with isDeleted) entities are synced
|
||||||
|
@ -456,7 +456,7 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
|
|||||||
position: attachmentMeta.position
|
position: attachmentMeta.position
|
||||||
});
|
});
|
||||||
|
|
||||||
attachment.setContent(content);
|
attachment.setContent(content, { forceSave: true });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ function updateNormalEntity(remoteEC, remoteEntityRow, instanceId, updateContext
|
|||||||
updateContext.updated[remoteEC.entityName] = updateContext.updated[remoteEC.entityName] || [];
|
updateContext.updated[remoteEC.entityName] = updateContext.updated[remoteEC.entityName] || [];
|
||||||
updateContext.updated[remoteEC.entityName].push(remoteEC.entityId);
|
updateContext.updated[remoteEC.entityName].push(remoteEC.entityId);
|
||||||
|
|
||||||
if (!localEC || localEC.utcDateChanged < remoteEC.utcDateChanged) {
|
if (!localEC || localEC.utcDateChanged < remoteEC.utcDateChanged || localEC.hash !== remoteEC.hash) {
|
||||||
entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId);
|
entityChangesService.putEntityChangeWithInstanceId(remoteEC, instanceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user