2017-11-10 09:52:47 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const log = require('./log');
|
2018-04-02 09:27:46 +08:00
|
|
|
const syncTableService = require('./sync_table');
|
2019-01-04 06:27:10 +08:00
|
|
|
const eventService = require('./events');
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-04-13 06:31:29 +08:00
|
|
|
async function updateEntity(sync, entity, sourceId) {
|
|
|
|
const {entityName} = sync;
|
|
|
|
|
2018-04-08 10:25:28 +08:00
|
|
|
if (entityName === 'notes') {
|
|
|
|
await updateNote(entity, sourceId);
|
|
|
|
}
|
2019-02-15 07:15:09 +08:00
|
|
|
else if (entityName === 'note_contents') {
|
|
|
|
await updateNoteContent(entity, sourceId);
|
|
|
|
}
|
2018-04-08 10:25:28 +08:00
|
|
|
else if (entityName === 'branches') {
|
|
|
|
await updateBranch(entity, sourceId);
|
|
|
|
}
|
|
|
|
else if (entityName === 'note_revisions') {
|
|
|
|
await updateNoteRevision(entity, sourceId);
|
|
|
|
}
|
|
|
|
else if (entityName === 'note_reordering') {
|
2018-04-13 06:31:29 +08:00
|
|
|
await updateNoteReordering(sync.entityId, entity, sourceId);
|
2018-04-08 10:25:28 +08:00
|
|
|
}
|
|
|
|
else if (entityName === 'options') {
|
|
|
|
await updateOptions(entity, sourceId);
|
|
|
|
}
|
|
|
|
else if (entityName === 'recent_notes') {
|
|
|
|
await updateRecentNotes(entity, sourceId);
|
|
|
|
}
|
2018-08-03 04:48:21 +08:00
|
|
|
else if (entityName === 'attributes') {
|
|
|
|
await updateAttribute(entity, sourceId);
|
|
|
|
}
|
2018-04-08 10:25:28 +08:00
|
|
|
else if (entityName === 'api_tokens') {
|
|
|
|
await updateApiToken(entity, sourceId);
|
|
|
|
}
|
|
|
|
else {
|
2018-07-30 02:33:42 +08:00
|
|
|
throw new Error(`Unrecognized entity type ${entityName}`);
|
2018-04-08 10:25:28 +08:00
|
|
|
}
|
2019-01-04 06:27:10 +08: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-02 03:00:56 +08:00
|
|
|
if (!['notes', 'note_contents', 'note_revisions', 'note_revision_contents'].includes(entityName) || !entity.isProtected) {
|
2019-01-04 06:27:10 +08:00
|
|
|
await eventService.emit(eventService.ENTITY_SYNCED, {
|
|
|
|
entityName,
|
|
|
|
entity
|
|
|
|
});
|
|
|
|
}
|
2018-04-08 10:25:28 +08:00
|
|
|
}
|
|
|
|
|
2017-11-30 09:47:01 +08:00
|
|
|
async function updateNote(entity, sourceId) {
|
2018-01-30 06:41:59 +08:00
|
|
|
const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2019-03-13 03:58:31 +08:00
|
|
|
if (!origNote || origNote.utcDateModified <= entity.utcDateModified) {
|
2018-04-08 01:03:16 +08:00
|
|
|
await sql.transactional(async () => {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.replace("notes", entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await syncTableService.addNoteSync(entity.noteId, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
});
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
log.info("Update/sync note " + entity.noteId);
|
2017-11-10 09:52:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-15 07:15:09 +08:00
|
|
|
async function updateNoteContent(entity, sourceId) {
|
|
|
|
const origNoteContent = await sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [entity.noteId]);
|
|
|
|
|
2019-03-13 03:58:31 +08:00
|
|
|
if (!origNoteContent || origNoteContent.utcDateModified <= entity.utcDateModified) {
|
2019-02-15 07:15:09 +08:00
|
|
|
entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');
|
|
|
|
|
|
|
|
await sql.transactional(async () => {
|
|
|
|
await sql.replace("note_contents", entity);
|
|
|
|
|
2019-03-27 05:24:04 +08:00
|
|
|
await syncTableService.addNoteContentSync(entity.noteId, sourceId);
|
2019-02-15 07:15:09 +08:00
|
|
|
});
|
|
|
|
|
2019-03-27 05:24:04 +08:00
|
|
|
log.info("Update/sync note content for noteId=" + entity.noteId);
|
2019-02-15 07:15:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
async function updateBranch(entity, sourceId) {
|
|
|
|
const orig = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [entity.branchId]);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
await sql.transactional(async () => {
|
2019-03-13 03:58:31 +08:00
|
|
|
if (orig === null || orig.utcDateModified < entity.utcDateModified) {
|
2018-07-25 04:03:36 +08: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.
|
|
|
|
if (orig) {
|
|
|
|
delete entity.isExpanded;
|
|
|
|
}
|
2017-11-10 10:11:33 +08:00
|
|
|
|
2018-03-25 09:39:15 +08:00
|
|
|
await sql.replace('branches', entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await syncTableService.addBranchSync(entity.branchId, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-04-04 10:15:28 +08:00
|
|
|
log.info("Update/sync branch " + entity.branchId);
|
2017-11-29 06:04:47 +08:00
|
|
|
}
|
|
|
|
});
|
2017-11-10 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2018-03-26 08:52:38 +08:00
|
|
|
async function updateNoteRevision(entity, sourceId) {
|
2018-01-30 06:41:59 +08:00
|
|
|
const orig = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
await sql.transactional(async () => {
|
2019-11-02 03:00:56 +08:00
|
|
|
if (orig === null || orig.utcDateModified < entity.utcDateModified) {
|
2018-01-29 08:38:05 +08:00
|
|
|
await sql.replace('note_revisions', entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await syncTableService.addNoteRevisionSync(entity.noteRevisionId, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-03-26 08:52:38 +08:00
|
|
|
log.info("Update/sync note revision " + entity.noteRevisionId);
|
2017-11-29 06:04:47 +08:00
|
|
|
}
|
|
|
|
});
|
2017-11-10 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
2019-11-02 03:00:56 +08:00
|
|
|
async function updateNoteRevisionContent(entity, sourceId) {
|
|
|
|
const orig = await sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [entity.noteRevisionId]);
|
|
|
|
|
|
|
|
await sql.transactional(async () => {
|
|
|
|
if (orig === null || orig.utcDateModified < entity.utcDateModified) {
|
|
|
|
entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');
|
|
|
|
|
|
|
|
await sql.replace('note_revision_contents', entity);
|
|
|
|
|
|
|
|
await syncTableService.addNoteRevisionContentSync(entity.noteRevisionId, sourceId);
|
|
|
|
|
|
|
|
log.info("Update/sync note revision content " + entity.noteRevisionId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-13 06:31:29 +08:00
|
|
|
async function updateNoteReordering(entityId, entity, sourceId) {
|
2018-04-08 01:03:16 +08:00
|
|
|
await sql.transactional(async () => {
|
2019-11-01 04:58:34 +08:00
|
|
|
for (const key in entity) {
|
2018-04-13 06:13:48 +08:00
|
|
|
await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [entity[key], key]);
|
2019-11-01 04:58:34 +08:00
|
|
|
}
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-04-13 06:31:29 +08:00
|
|
|
await syncTableService.addNoteReorderingSync(entityId, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateOptions(entity, sourceId) {
|
2018-01-30 06:41:59 +08:00
|
|
|
const orig = await sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [entity.name]);
|
2018-01-12 11:45:25 +08:00
|
|
|
|
2018-07-25 02:35:03 +08:00
|
|
|
if (orig && !orig.isSynced) {
|
2017-11-10 09:52:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-08 01:03:16 +08:00
|
|
|
await sql.transactional(async () => {
|
2019-03-13 03:58:31 +08:00
|
|
|
if (orig === null || orig.utcDateModified < entity.utcDateModified) {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.replace('options', entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await syncTableService.addOptionsSync(entity.name, sourceId);
|
2017-11-29 06:04:47 +08:00
|
|
|
}
|
|
|
|
});
|
2017-11-10 09:52:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function updateRecentNotes(entity, sourceId) {
|
2019-05-22 03:47:28 +08:00
|
|
|
const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteId = ?", [entity.noteId]);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2019-03-13 03:58:31 +08:00
|
|
|
if (orig === null || orig.utcDateCreated < entity.utcDateCreated) {
|
2018-04-08 01:03:16 +08:00
|
|
|
await sql.transactional(async () => {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.replace('recent_notes', entity);
|
2017-11-10 09:52:47 +08:00
|
|
|
|
2019-05-22 03:47:28 +08:00
|
|
|
await syncTableService.addRecentNoteSync(entity.noteId, sourceId);
|
2017-11-10 09:52:47 +08:00
|
|
|
});
|
2018-11-08 17:11:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 04:48:21 +08:00
|
|
|
async function updateAttribute(entity, sourceId) {
|
|
|
|
const origAttribute = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]);
|
|
|
|
|
2019-03-13 03:58:31 +08:00
|
|
|
if (!origAttribute || origAttribute.utcDateModified <= entity.utcDateModified) {
|
2018-08-03 04:48:21 +08:00
|
|
|
await sql.transactional(async () => {
|
|
|
|
await sql.replace("attributes", entity);
|
|
|
|
|
|
|
|
await syncTableService.addAttributeSync(entity.attributeId, sourceId);
|
|
|
|
});
|
|
|
|
|
|
|
|
log.info("Update/sync attribute " + entity.attributeId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 13:18:59 +08:00
|
|
|
async function updateApiToken(entity, sourceId) {
|
|
|
|
const apiTokenId = await sql.getRow("SELECT * FROM api_tokens WHERE apiTokenId = ?", [entity.apiTokenId]);
|
|
|
|
|
|
|
|
if (!apiTokenId) {
|
2018-04-08 01:03:16 +08:00
|
|
|
await sql.transactional(async () => {
|
2018-02-11 13:18:59 +08:00
|
|
|
await sql.replace("api_tokens", entity);
|
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
await syncTableService.addApiTokenSync(entity.apiTokenId, sourceId);
|
2018-02-11 13:18:59 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
log.info("Update/sync API token " + entity.apiTokenId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 09:52:47 +08:00
|
|
|
module.exports = {
|
2018-04-08 10:32:46 +08:00
|
|
|
updateEntity
|
2017-11-10 09:52:47 +08:00
|
|
|
};
|