syncification

This commit is contained in:
zadam
2020-06-20 12:31:38 +02:00
parent 30062d687f
commit 88348c560c
97 changed files with 1673 additions and 1700 deletions

View File

@@ -3,7 +3,7 @@ const log = require('./log');
const syncTableService = require('./sync_table');
const eventService = require('./events');
async function updateEntity(sync, entity, sourceId) {
function updateEntity(sync, entity, sourceId) {
// can be undefined for options with isSynced=false
if (!entity) {
return false;
@@ -13,34 +13,34 @@ async function updateEntity(sync, entity, sourceId) {
let updated;
if (entityName === 'notes') {
updated = await updateNote(entity, sourceId);
updated = updateNote(entity, sourceId);
}
else if (entityName === 'note_contents') {
updated = await updateNoteContent(entity, sourceId);
updated = updateNoteContent(entity, sourceId);
}
else if (entityName === 'branches') {
updated = await updateBranch(entity, sourceId);
updated = updateBranch(entity, sourceId);
}
else if (entityName === 'note_revisions') {
updated = await updateNoteRevision(entity, sourceId);
updated = updateNoteRevision(entity, sourceId);
}
else if (entityName === 'note_revision_contents') {
updated = await updateNoteRevisionContent(entity, sourceId);
updated = updateNoteRevisionContent(entity, sourceId);
}
else if (entityName === 'note_reordering') {
updated = await updateNoteReordering(sync.entityId, entity, sourceId);
updated = updateNoteReordering(sync.entityId, entity, sourceId);
}
else if (entityName === 'options') {
updated = await updateOptions(entity, sourceId);
updated = updateOptions(entity, sourceId);
}
else if (entityName === 'recent_notes') {
updated = await updateRecentNotes(entity, sourceId);
updated = updateRecentNotes(entity, sourceId);
}
else if (entityName === 'attributes') {
updated = await updateAttribute(entity, sourceId);
updated = updateAttribute(entity, sourceId);
}
else if (entityName === 'api_tokens') {
updated = await updateApiToken(entity, sourceId);
updated = updateApiToken(entity, sourceId);
}
else {
throw new Error(`Unrecognized entity type ${entityName}`);
@@ -50,7 +50,7 @@ async function updateEntity(sync, entity, sourceId) {
// 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)) {
await eventService.emit(eventService.ENTITY_SYNCED, {
eventService.emit(eventService.ENTITY_SYNCED, {
entityName,
entity
});
@@ -79,14 +79,14 @@ function shouldWeUpdateEntity(localEntity, remoteEntity) {
return false;
}
async function updateNote(remoteEntity, sourceId) {
const localEntity = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [remoteEntity.noteId]);
function updateNote(remoteEntity, sourceId) {
const localEntity = sql.getRow("SELECT * FROM notes WHERE noteId = ?", [remoteEntity.noteId]);
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.transactional(async () => {
await sql.replace("notes", remoteEntity);
sql.transactional(() => {
sql.replace("notes", remoteEntity);
await syncTableService.addNoteSync(remoteEntity.noteId, sourceId);
syncTableService.addNoteSync(remoteEntity.noteId, sourceId);
});
return true;
@@ -95,16 +95,16 @@ async function updateNote(remoteEntity, sourceId) {
return false;
}
async function updateNoteContent(remoteEntity, sourceId) {
const localEntity = await sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [remoteEntity.noteId]);
function updateNoteContent(remoteEntity, sourceId) {
const localEntity = sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [remoteEntity.noteId]);
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
remoteEntity.content = remoteEntity.content === null ? null : Buffer.from(remoteEntity.content, 'base64');
await sql.transactional(async () => {
await sql.replace("note_contents", remoteEntity);
sql.transactional(() => {
sql.replace("note_contents", remoteEntity);
await syncTableService.addNoteContentSync(remoteEntity.noteId, sourceId);
syncTableService.addNoteContentSync(remoteEntity.noteId, sourceId);
});
return true;
@@ -113,20 +113,20 @@ async function updateNoteContent(remoteEntity, sourceId) {
return false;
}
async function updateBranch(remoteEntity, sourceId) {
const localEntity = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [remoteEntity.branchId]);
function updateBranch(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [remoteEntity.branchId]);
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.transactional(async () => {
sql.transactional(() => {
// 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.
if (localEntity) {
delete remoteEntity.isExpanded;
}
await sql.replace('branches', remoteEntity);
sql.replace('branches', remoteEntity);
await syncTableService.addBranchSync(remoteEntity.branchId, sourceId);
syncTableService.addBranchSync(remoteEntity.branchId, sourceId);
});
return true;
@@ -135,30 +135,30 @@ async function updateBranch(remoteEntity, sourceId) {
return false;
}
async function updateNoteRevision(remoteEntity, sourceId) {
const localEntity = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);
function updateNoteRevision(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);
await sql.transactional(async () => {
sql.transactional(() => {
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.replace('note_revisions', remoteEntity);
sql.replace('note_revisions', remoteEntity);
await syncTableService.addNoteRevisionSync(remoteEntity.noteRevisionId, sourceId);
syncTableService.addNoteRevisionSync(remoteEntity.noteRevisionId, sourceId);
log.info("Update/sync note revision " + remoteEntity.noteRevisionId);
}
});
}
async function updateNoteRevisionContent(remoteEntity, sourceId) {
const localEntity = await sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);
function updateNoteRevisionContent(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.transactional(async () => {
sql.transactional(() => {
remoteEntity.content = remoteEntity.content === null ? null : Buffer.from(remoteEntity.content, 'base64');
await sql.replace('note_revision_contents', remoteEntity);
sql.replace('note_revision_contents', remoteEntity);
await syncTableService.addNoteRevisionContentSync(remoteEntity.noteRevisionId, sourceId);
syncTableService.addNoteRevisionContentSync(remoteEntity.noteRevisionId, sourceId);
});
return true;
@@ -167,30 +167,30 @@ async function updateNoteRevisionContent(remoteEntity, sourceId) {
return false;
}
async function updateNoteReordering(entityId, remote, sourceId) {
await sql.transactional(async () => {
function updateNoteReordering(entityId, remote, sourceId) {
sql.transactional(() => {
for (const key in remote) {
await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remote[key], key]);
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remote[key], key]);
}
await syncTableService.addNoteReorderingSync(entityId, sourceId);
syncTableService.addNoteReorderingSync(entityId, sourceId);
});
return true;
}
async function updateOptions(remoteEntity, sourceId) {
const localEntity = await sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [remoteEntity.name]);
function updateOptions(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [remoteEntity.name]);
if (localEntity && !localEntity.isSynced) {
return;
}
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.transactional(async () => {
await sql.replace('options', remoteEntity);
sql.transactional(() => {
sql.replace('options', remoteEntity);
await syncTableService.addOptionsSync(remoteEntity.name, sourceId, true);
syncTableService.addOptionsSync(remoteEntity.name, sourceId, true);
});
return true;
@@ -199,14 +199,14 @@ async function updateOptions(remoteEntity, sourceId) {
return false;
}
async function updateRecentNotes(remoteEntity, sourceId) {
const localEntity = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteId = ?", [remoteEntity.noteId]);
function updateRecentNotes(remoteEntity, sourceId) {
const localEntity = sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteId = ?", [remoteEntity.noteId]);
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.transactional(async () => {
await sql.replace('recent_notes', remoteEntity);
sql.transactional(() => {
sql.replace('recent_notes', remoteEntity);
await syncTableService.addRecentNoteSync(remoteEntity.noteId, sourceId);
syncTableService.addRecentNoteSync(remoteEntity.noteId, sourceId);
});
return true;
@@ -215,14 +215,14 @@ async function updateRecentNotes(remoteEntity, sourceId) {
return false;
}
async function updateAttribute(remoteEntity, sourceId) {
const localEntity = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [remoteEntity.attributeId]);
function updateAttribute(remoteEntity, sourceId) {
const localEntity = sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [remoteEntity.attributeId]);
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.transactional(async () => {
await sql.replace("attributes", remoteEntity);
sql.transactional(() => {
sql.replace("attributes", remoteEntity);
await syncTableService.addAttributeSync(remoteEntity.attributeId, sourceId);
syncTableService.addAttributeSync(remoteEntity.attributeId, sourceId);
});
return true;
@@ -231,14 +231,14 @@ async function updateAttribute(remoteEntity, sourceId) {
return false;
}
async function updateApiToken(entity, sourceId) {
const apiTokenId = await sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [entity.apiTokenId]);
function updateApiToken(entity, sourceId) {
const apiTokenId = sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [entity.apiTokenId]);
if (!apiTokenId) {
await sql.transactional(async () => {
await sql.replace("api_tokens", entity);
sql.transactional(() => {
sql.replace("api_tokens", entity);
await syncTableService.addApiTokenSync(entity.apiTokenId, sourceId);
syncTableService.addApiTokenSync(entity.apiTokenId, sourceId);
});
return true;
@@ -249,4 +249,4 @@ async function updateApiToken(entity, sourceId) {
module.exports = {
updateEntity
};
};