Notes/src/services/sync_update.js

82 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-11-09 20:52:47 -05:00
const sql = require('./sql');
const log = require('./log');
const entityChangesService = require('./entity_changes.js');
2019-01-03 23:27:10 +01:00
const eventService = require('./events');
2017-11-09 20:52:47 -05:00
2020-08-27 22:11:17 +02:00
function updateEntity(entityChange, entity, sourceId) {
2020-03-08 21:59:19 +01:00
// can be undefined for options with isSynced=false
if (!entity) {
return false;
2020-03-08 21:59:19 +01:00
}
const {entityName, hash} = entityChange;
let updated;
2018-04-12 18:31:29 -04:00
2020-12-14 13:15:32 +01:00
if (entityName === 'note_reordering') {
updated = updateNoteReordering(entityChange, entity, sourceId);
2018-04-07 22:25:28 -04:00
}
else {
2020-12-14 13:15:32 +01:00
updated = updateNormalEntity(entityChange, entity, sourceId);
2018-04-07 22:25:28 -04:00
}
2019-01-03 23:27:10 +01:00
// currently making exception for protected notes and note revisions because here
// the title and content are not available decrypted as listeners would expect
if (updated &&
(!['notes', 'note_contents', 'note_revisions', 'note_revision_contents'].includes(entityName) || !entity.isProtected)) {
2020-06-20 12:31:38 +02:00
eventService.emit(eventService.ENTITY_SYNCED, {
2019-01-03 23:27:10 +01:00
entityName,
entity
});
}
return updated;
2018-04-07 22:25:28 -04:00
}
2020-12-14 13:15:32 +01:00
function updateNormalEntity(entityChange, entity, sourceId) {
const {utcDateChanged, hash} = sql.getRow(`
SELECT utcDateChanged, hash
FROM entity_changes
WHERE entityName = ? AND entityId = ?`, [entityChange.entityName, entityChange.entityId]);
if (utcDateChanged < entityChange.utcDateChanged
|| hash !== entityChange.hash // sync error, we should still update
) {
if (['note_contents', 'note_revision_contents'].includes(entityChange.entityName)) {
// we always use Buffer object which is different from normal saving - there we use simple string type for "string notes"
// the problem is that in general it's not possible to whether a note_content is string note or note (syncs can arrive out of order)
entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');
if (entity.content && entity.content.byteLength === 0) {
// there seems to be a bug which causes empty buffer to be stored as NULL which is then picked up as inconsistency
entity.content = "";
2018-07-24 22:03:36 +02:00
}
}
2019-11-01 20:00:56 +01:00
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
2020-12-14 13:15:32 +01:00
sql.replace(entityChange.entityName, entity);
2019-11-01 20:00:56 +01:00
2020-12-14 13:15:32 +01:00
entityChangesService.addEntityChange(entityChange, sourceId);
});
2019-11-01 20:00:56 +01:00
return true;
}
return false;
2019-11-01 20:00:56 +01:00
}
2020-12-14 13:15:32 +01:00
function updateNoteReordering(entityChange, entity, sourceId) {
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
2020-12-14 13:15:32 +01:00
for (const key in entity) {
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [entity[key], key]);
}
2017-11-09 20:52:47 -05:00
2020-12-14 13:15:32 +01:00
entityChangesService.addEntityChange(entityChange, sourceId);
2017-11-09 20:52:47 -05:00
});
return true;
2017-11-09 20:52:47 -05:00
}
module.exports = {
2018-04-07 22:32:46 -04:00
updateEntity
2020-06-20 12:31:38 +02:00
};