Notes/src/services/sync_update.js

253 lines
7.6 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-06-20 12:31:38 +02:00
function updateEntity(sync, 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
}
2018-04-12 18:31:29 -04:00
const {entityName} = sync;
let updated;
2018-04-12 18:31:29 -04:00
2018-04-07 22:25:28 -04:00
if (entityName === 'notes') {
2020-06-20 12:31:38 +02:00
updated = updateNote(entity, sourceId);
2018-04-07 22:25:28 -04:00
}
2019-02-15 00:15:09 +01:00
else if (entityName === 'note_contents') {
2020-06-20 12:31:38 +02:00
updated = updateNoteContent(entity, sourceId);
2019-02-15 00:15:09 +01:00
}
2018-04-07 22:25:28 -04:00
else if (entityName === 'branches') {
2020-06-20 12:31:38 +02:00
updated = updateBranch(entity, sourceId);
2018-04-07 22:25:28 -04:00
}
else if (entityName === 'note_revisions') {
2020-06-20 12:31:38 +02:00
updated = updateNoteRevision(entity, sourceId);
2018-04-07 22:25:28 -04:00
}
2019-11-09 15:21:14 +01:00
else if (entityName === 'note_revision_contents') {
2020-06-20 12:31:38 +02:00
updated = updateNoteRevisionContent(entity, sourceId);
2019-11-09 15:21:14 +01:00
}
2018-04-07 22:25:28 -04:00
else if (entityName === 'note_reordering') {
2020-06-20 12:31:38 +02:00
updated = updateNoteReordering(sync.entityId, entity, sourceId);
2018-04-07 22:25:28 -04:00
}
else if (entityName === 'options') {
2020-06-20 12:31:38 +02:00
updated = updateOptions(entity, sourceId);
2018-04-07 22:25:28 -04:00
}
else if (entityName === 'recent_notes') {
2020-06-20 12:31:38 +02:00
updated = updateRecentNotes(entity, sourceId);
2018-04-07 22:25:28 -04:00
}
else if (entityName === 'attributes') {
2020-06-20 12:31:38 +02:00
updated = updateAttribute(entity, sourceId);
}
2018-04-07 22:25:28 -04:00
else if (entityName === 'api_tokens') {
2020-06-20 12:31:38 +02:00
updated = updateApiToken(entity, sourceId);
2018-04-07 22:25:28 -04:00
}
else {
throw new Error(`Unrecognized entity type ${entityName}`);
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
}
2019-12-24 17:49:44 +01:00
function shouldWeUpdateEntity(localEntity, remoteEntity) {
if (!localEntity) {
return true;
}
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;
}
2020-06-20 12:31:38 +02:00
function updateNote(remoteEntity, sourceId) {
const localEntity = sql.getRow("SELECT * FROM notes WHERE noteId = ?", [remoteEntity.noteId]);
2017-11-09 20:52:47 -05:00
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
sql.replace("notes", remoteEntity);
2017-11-09 20:52:47 -05:00
entityChangesService.addNoteEntityChange(remoteEntity.noteId, sourceId);
2017-11-09 20:52:47 -05:00
});
return true;
2017-11-09 20:52:47 -05:00
}
return false;
2017-11-09 20:52:47 -05:00
}
2020-06-20 12:31:38 +02:00
function updateNoteContent(remoteEntity, sourceId) {
const localEntity = sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [remoteEntity.noteId]);
2019-02-15 00:15:09 +01:00
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
remoteEntity.content = remoteEntity.content === null ? null : Buffer.from(remoteEntity.content, 'base64');
2019-02-15 00:15:09 +01:00
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
sql.replace("note_contents", remoteEntity);
2019-02-15 00:15:09 +01:00
entityChangesService.addNoteContentEntityChange(remoteEntity.noteId, sourceId);
2019-02-15 00:15:09 +01:00
});
return true;
2019-02-15 00:15:09 +01:00
}
return false;
2019-02-15 00:15:09 +01:00
}
2020-06-20 12:31:38 +02:00
function updateBranch(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [remoteEntity.branchId]);
2017-11-09 20:52:47 -05:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
2018-07-24 22:03:36 +02:00
// 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.
2019-12-24 17:49:44 +01:00
if (localEntity) {
delete remoteEntity.isExpanded;
2018-07-24 22:03:36 +02:00
}
2020-06-20 12:31:38 +02:00
sql.replace('branches', remoteEntity);
2017-11-09 20:52:47 -05:00
entityChangesService.addBranchEntityChange(remoteEntity.branchId, sourceId);
});
2017-11-09 20:52:47 -05:00
return true;
}
return false;
2017-11-09 20:52:47 -05:00
}
2020-06-20 12:31:38 +02:00
function updateNoteRevision(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);
2017-11-09 20:52:47 -05:00
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
2020-06-20 12:31:38 +02:00
sql.replace('note_revisions', remoteEntity);
2017-11-09 20:52:47 -05:00
entityChangesService.addNoteRevisionEntityChange(remoteEntity.noteRevisionId, sourceId);
2017-11-09 20:52:47 -05:00
2019-12-24 17:49:44 +01:00
log.info("Update/sync note revision " + remoteEntity.noteRevisionId);
}
});
2017-11-09 20:52:47 -05:00
}
2020-06-20 12:31:38 +02:00
function updateNoteRevisionContent(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);
2019-11-01 20:00:56 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
2019-12-24 17:49:44 +01:00
remoteEntity.content = remoteEntity.content === null ? null : Buffer.from(remoteEntity.content, 'base64');
2019-11-01 20:00:56 +01:00
2020-06-20 12:31:38 +02:00
sql.replace('note_revision_contents', remoteEntity);
2019-11-01 20:00:56 +01:00
entityChangesService.addNoteRevisionContentEntityChange(remoteEntity.noteRevisionId, sourceId);
});
2019-11-01 20:00:56 +01:00
return true;
}
return false;
2019-11-01 20:00:56 +01:00
}
2020-06-20 12:31:38 +02:00
function updateNoteReordering(entityId, remote, sourceId) {
sql.transactional(() => {
2019-12-24 17:49:44 +01:00
for (const key in remote) {
2020-06-20 12:31:38 +02:00
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remote[key], key]);
}
2017-11-09 20:52:47 -05:00
entityChangesService.addNoteReorderingEntityChange(entityId, sourceId);
2017-11-09 20:52:47 -05:00
});
return true;
2017-11-09 20:52:47 -05:00
}
2020-06-20 12:31:38 +02:00
function updateOptions(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [remoteEntity.name]);
2019-12-24 17:49:44 +01:00
if (localEntity && !localEntity.isSynced) {
2017-11-09 20:52:47 -05:00
return;
}
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
sql.replace('options', remoteEntity);
2017-11-09 20:52:47 -05:00
entityChangesService.addOptionEntityChange(remoteEntity.name, sourceId, true);
});
return true;
}
return false;
2017-11-09 20:52:47 -05:00
}
2020-06-20 12:31:38 +02:00
function updateRecentNotes(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteId = ?", [remoteEntity.noteId]);
2017-11-09 20:52:47 -05:00
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
sql.replace('recent_notes', remoteEntity);
2017-11-09 20:52:47 -05:00
entityChangesService.addRecentNoteEntityChange(remoteEntity.noteId, sourceId);
2017-11-09 20:52:47 -05:00
});
return true;
2018-11-08 10:11:00 +01:00
}
return false;
2018-11-08 10:11:00 +01:00
}
2020-06-20 12:31:38 +02:00
function updateAttribute(remoteEntity, sourceId) {
const localEntity = sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [remoteEntity.attributeId]);
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
sql.replace("attributes", remoteEntity);
entityChangesService.addAttributeEntityChange(remoteEntity.attributeId, sourceId);
});
return true;
}
return false;
}
2020-06-20 12:31:38 +02:00
function updateApiToken(entity, sourceId) {
const apiTokenId = sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [entity.apiTokenId]);
2018-02-11 00:18:59 -05:00
if (!apiTokenId) {
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
sql.replace("api_tokens", entity);
2018-02-11 00:18:59 -05:00
entityChangesService.addApiTokenEntityChange(entity.apiTokenId, sourceId);
2018-02-11 00:18:59 -05:00
});
return true;
2018-02-11 00:18:59 -05:00
}
return false;
2018-02-11 00:18:59 -05:00
}
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
};