Notes/src/services/sync_update.js

226 lines
7.8 KiB
JavaScript
Raw Normal View History

2017-11-09 20:52:47 -05:00
const sql = require('./sql');
const log = require('./log');
const syncTableService = require('./sync_table');
2019-01-03 23:27:10 +01:00
const eventService = require('./events');
2017-11-09 20:52:47 -05:00
2018-04-12 18:31:29 -04:00
async function updateEntity(sync, entity, sourceId) {
2020-03-08 21:59:19 +01:00
// can be undefined for options with isSynced=false
if (!entity) {
return;
}
2018-04-12 18:31:29 -04:00
const {entityName} = sync;
2018-04-07 22:25:28 -04:00
if (entityName === 'notes') {
await updateNote(entity, sourceId);
}
2019-02-15 00:15:09 +01:00
else if (entityName === 'note_contents') {
await updateNoteContent(entity, sourceId);
}
2018-04-07 22:25:28 -04:00
else if (entityName === 'branches') {
await updateBranch(entity, sourceId);
}
else if (entityName === 'note_revisions') {
await updateNoteRevision(entity, sourceId);
}
2019-11-09 15:21:14 +01:00
else if (entityName === 'note_revision_contents') {
await updateNoteRevisionContent(entity, sourceId);
}
2018-04-07 22:25:28 -04:00
else if (entityName === 'note_reordering') {
2018-04-12 18:31:29 -04:00
await updateNoteReordering(sync.entityId, entity, sourceId);
2018-04-07 22:25:28 -04:00
}
else if (entityName === 'options') {
await updateOptions(entity, sourceId);
}
else if (entityName === 'recent_notes') {
await updateRecentNotes(entity, sourceId);
}
else if (entityName === 'attributes') {
await updateAttribute(entity, sourceId);
}
2018-04-07 22:25:28 -04:00
else if (entityName === 'api_tokens') {
await updateApiToken(entity, sourceId);
}
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
2019-11-01 20:00:56 +01:00
if (!['notes', 'note_contents', 'note_revisions', 'note_revision_contents'].includes(entityName) || !entity.isProtected) {
2019-01-03 23:27:10 +01:00
await eventService.emit(eventService.ENTITY_SYNCED, {
entityName,
entity
});
}
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;
}
async function updateNote(remoteEntity, sourceId) {
const localEntity = await 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)) {
await sql.transactional(async () => {
2019-12-24 17:49:44 +01:00
await sql.replace("notes", remoteEntity);
2017-11-09 20:52:47 -05:00
2019-12-24 17:49:44 +01:00
await syncTableService.addNoteSync(remoteEntity.noteId, sourceId);
2017-11-09 20:52:47 -05:00
});
2019-12-24 17:49:44 +01:00
log.info("Update/sync note " + remoteEntity.noteId);
2017-11-09 20:52:47 -05:00
}
}
2019-12-24 17:49:44 +01:00
async function updateNoteContent(remoteEntity, sourceId) {
const localEntity = await 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
await sql.transactional(async () => {
2019-12-24 17:49:44 +01:00
await sql.replace("note_contents", remoteEntity);
2019-02-15 00:15:09 +01:00
2019-12-24 17:49:44 +01:00
await syncTableService.addNoteContentSync(remoteEntity.noteId, sourceId);
2019-02-15 00:15:09 +01:00
});
2019-12-24 17:49:44 +01:00
log.info("Update/sync note content for noteId=" + remoteEntity.noteId);
2019-02-15 00:15:09 +01:00
}
}
2019-12-24 17:49:44 +01:00
async function updateBranch(remoteEntity, sourceId) {
const localEntity = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [remoteEntity.branchId]);
2017-11-09 20:52:47 -05:00
await sql.transactional(async () => {
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
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
}
2019-12-24 17:49:44 +01:00
await sql.replace('branches', remoteEntity);
2017-11-09 20:52:47 -05:00
2019-12-24 17:49:44 +01:00
await syncTableService.addBranchSync(remoteEntity.branchId, sourceId);
2017-11-09 20:52:47 -05:00
2019-12-24 17:49:44 +01:00
log.info("Update/sync branch " + remoteEntity.branchId);
}
});
2017-11-09 20:52:47 -05:00
}
2019-12-24 17:49:44 +01:00
async function updateNoteRevision(remoteEntity, sourceId) {
const localEntity = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);
2017-11-09 20:52:47 -05:00
await sql.transactional(async () => {
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.replace('note_revisions', remoteEntity);
2017-11-09 20:52:47 -05:00
2019-12-24 17:49:44 +01:00
await syncTableService.addNoteRevisionSync(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
}
2019-12-24 17:49:44 +01:00
async function updateNoteRevisionContent(remoteEntity, sourceId) {
const localEntity = await sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [remoteEntity.noteRevisionId]);
2019-11-01 20:00:56 +01:00
await sql.transactional(async () => {
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
remoteEntity.content = remoteEntity.content === null ? null : Buffer.from(remoteEntity.content, 'base64');
2019-11-01 20:00:56 +01:00
2019-12-24 17:49:44 +01:00
await sql.replace('note_revision_contents', remoteEntity);
2019-11-01 20:00:56 +01:00
2019-12-24 17:49:44 +01:00
await syncTableService.addNoteRevisionContentSync(remoteEntity.noteRevisionId, sourceId);
2019-11-01 20:00:56 +01:00
2019-12-24 17:49:44 +01:00
log.info("Update/sync note revision content " + remoteEntity.noteRevisionId);
2019-11-01 20:00:56 +01:00
}
});
}
2019-12-24 17:49:44 +01:00
async function updateNoteReordering(entityId, remote, sourceId) {
await sql.transactional(async () => {
2019-12-24 17:49:44 +01:00
for (const key in remote) {
await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [remote[key], key]);
}
2017-11-09 20:52:47 -05:00
2018-04-12 18:31:29 -04:00
await syncTableService.addNoteReorderingSync(entityId, sourceId);
2017-11-09 20:52:47 -05:00
});
}
2019-12-24 17:49:44 +01:00
async function updateOptions(remoteEntity, sourceId) {
const localEntity = await 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;
}
await sql.transactional(async () => {
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.replace('options', remoteEntity);
2017-11-09 20:52:47 -05:00
2020-03-09 21:28:41 +01:00
await syncTableService.addOptionsSync(remoteEntity.name, sourceId, true);
}
});
2017-11-09 20:52:47 -05:00
}
2019-12-24 17:49:44 +01:00
async function updateRecentNotes(remoteEntity, sourceId) {
const localEntity = await 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)) {
await sql.transactional(async () => {
2019-12-24 17:49:44 +01:00
await sql.replace('recent_notes', remoteEntity);
2017-11-09 20:52:47 -05:00
2019-12-24 17:49:44 +01:00
await syncTableService.addRecentNoteSync(remoteEntity.noteId, sourceId);
2017-11-09 20:52:47 -05:00
});
2018-11-08 10:11:00 +01:00
}
}
2019-12-24 17:49:44 +01:00
async function updateAttribute(remoteEntity, sourceId) {
const localEntity = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [remoteEntity.attributeId]);
2019-12-24 17:49:44 +01:00
if (shouldWeUpdateEntity(localEntity, remoteEntity)) {
await sql.transactional(async () => {
2019-12-24 17:49:44 +01:00
await sql.replace("attributes", remoteEntity);
2019-12-24 17:49:44 +01:00
await syncTableService.addAttributeSync(remoteEntity.attributeId, sourceId);
});
2019-12-24 17:49:44 +01:00
log.info("Update/sync attribute " + remoteEntity.attributeId);
}
}
2018-02-11 00:18:59 -05:00
async function updateApiToken(entity, sourceId) {
const apiTokenId = await sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [entity.apiTokenId]);
if (!apiTokenId) {
await sql.transactional(async () => {
2018-02-11 00:18:59 -05:00
await sql.replace("api_tokens", entity);
await syncTableService.addApiTokenSync(entity.apiTokenId, sourceId);
2018-02-11 00:18:59 -05:00
});
log.info("Update/sync API token " + entity.apiTokenId);
}
}
2017-11-09 20:52:47 -05:00
module.exports = {
2018-04-07 22:32:46 -04:00
updateEntity
2017-11-09 20:52:47 -05:00
};