trilium/src/services/sync_update.js

199 lines
6.5 KiB
JavaScript
Raw Normal View History

2017-11-10 09:52:47 +08:00
const sql = require('./sql');
const log = require('./log');
const eventLogService = require('./event_log');
const syncTableService = require('./sync_table');
2017-11-10 09:52:47 +08:00
2018-04-08 10:25:28 +08:00
async function updateEntity(entityName, entity, sourceId) {
if (entityName === 'notes') {
await updateNote(entity, sourceId);
}
else if (entityName === 'branches') {
await updateBranch(entity, sourceId);
}
else if (entityName === 'note_revisions') {
await updateNoteRevision(entity, sourceId);
}
else if (entityName === 'note_reordering') {
await updateNoteReordering(entity, sourceId);
}
else if (entityName === 'options') {
await updateOptions(entity, sourceId);
}
else if (entityName === 'recent_notes') {
await updateRecentNotes(entity, sourceId);
}
else if (entityName === 'images') {
await updateImage(entity, sourceId);
}
else if (entityName === 'note_images') {
await updateNoteImage(entity, sourceId);
}
else if (entityName === 'labels') {
await updateLabel(entity, sourceId);
}
else if (entityName === 'api_tokens') {
await updateApiToken(entity, sourceId);
}
else {
throw new Error(`Unrecognized entity type ${entityName}`);
}
}
2018-02-19 11:55:36 +08:00
function deserializeNoteContentBuffer(note) {
if (note.type === 'file') {
note.content = new Buffer(note.content, 'binary');
}
}
async function updateNote(entity, sourceId) {
2018-02-19 11:55:36 +08:00
deserializeNoteContentBuffer(entity);
const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);
2017-11-10 09:52:47 +08:00
2018-01-29 08:30:14 +08:00
if (!origNote || origNote.dateModified <= entity.dateModified) {
await sql.transactional(async () => {
await sql.replace("notes", entity);
2017-11-10 09:52:47 +08:00
await syncTableService.addNoteSync(entity.noteId, sourceId);
await eventLogService.addNoteEvent(entity.noteId, "Synced note <note>");
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
}
}
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
await sql.transactional(async () => {
2018-01-29 08:30:14 +08:00
if (orig === null || orig.dateModified < entity.dateModified) {
delete entity.isExpanded;
2018-03-25 09:39:15 +08:00
await sql.replace('branches', entity);
2017-11-10 09:52:47 +08:00
await syncTableService.addBranchSync(entity.branchId, sourceId);
2017-11-10 09:52:47 +08:00
log.info("Update/sync branch " + entity.branchId);
}
});
2017-11-10 09:52:47 +08:00
}
async function updateNoteRevision(entity, sourceId) {
const orig = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]);
2017-11-10 09:52:47 +08:00
await sql.transactional(async () => {
// we update note revision even if date modified to is the same because the only thing which might have changed
2018-01-29 08:30:14 +08:00
// is the protected status (and correnspondingly title and content) which doesn't affect the dateModifiedTo
if (orig === null || orig.dateModifiedTo <= entity.dateModifiedTo) {
await sql.replace('note_revisions', entity);
2017-11-10 09:52:47 +08:00
await syncTableService.addNoteRevisionSync(entity.noteRevisionId, sourceId);
2017-11-10 09:52:47 +08:00
log.info("Update/sync note revision " + entity.noteRevisionId);
}
});
2017-11-10 09:52:47 +08:00
}
async function updateNoteReordering(entity, sourceId) {
await sql.transactional(async () => {
2017-11-10 09:52:47 +08:00
Object.keys(entity.ordering).forEach(async key => {
2018-03-25 09:39:15 +08:00
await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [entity.ordering[key], key]);
2017-11-10 09:52:47 +08:00
});
await syncTableService.addNoteReorderingSync(entity.parentNoteId, sourceId);
2017-11-10 09:52:47 +08:00
});
}
async function updateOptions(entity, sourceId) {
const orig = await sql.getRowOrNull("SELECT * FROM options WHERE name = ?", [entity.name]);
2018-01-29 08:30:14 +08:00
if (!orig.isSynced) {
2017-11-10 09:52:47 +08:00
return;
}
await sql.transactional(async () => {
2018-01-29 08:30:14 +08:00
if (orig === null || orig.dateModified < entity.dateModified) {
await sql.replace('options', entity);
2017-11-10 09:52:47 +08:00
await syncTableService.addOptionsSync(entity.name, sourceId);
2017-11-10 09:52:47 +08:00
await eventLogService.addEvent("Synced option " + entity.name);
}
});
2017-11-10 09:52:47 +08:00
}
async function updateRecentNotes(entity, sourceId) {
2018-03-25 09:39:15 +08:00
const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE branchId = ?", [entity.branchId]);
2017-11-10 09:52:47 +08:00
2018-01-29 08:30:14 +08:00
if (orig === null || orig.dateAccessed < entity.dateAccessed) {
await sql.transactional(async () => {
await sql.replace('recent_notes', entity);
2017-11-10 09:52:47 +08:00
await syncTableService.addRecentNoteSync(entity.branchId, sourceId);
2017-11-10 09:52:47 +08:00
});
}
}
2018-01-07 04:56:00 +08:00
async function updateImage(entity, sourceId) {
if (entity.data !== null) {
entity.data = Buffer.from(entity.data, 'base64');
}
const origImage = await sql.getRow("SELECT * FROM images WHERE imageId = ?", [entity.imageId]);
2018-01-07 04:56:00 +08:00
2018-01-29 08:30:14 +08:00
if (!origImage || origImage.dateModified <= entity.dateModified) {
await sql.transactional(async () => {
2018-01-07 04:56:00 +08:00
await sql.replace("images", entity);
await syncTableService.addImageSync(entity.imageId, sourceId);
2018-01-07 04:56:00 +08:00
});
2018-01-29 08:30:14 +08:00
log.info("Update/sync image " + entity.imageId);
2018-01-07 04:56:00 +08:00
}
}
2018-01-07 10:49:02 +08:00
async function updateNoteImage(entity, sourceId) {
const origNoteImage = await sql.getRow("SELECT * FROM note_images WHERE noteImageId = ?", [entity.noteImageId]);
2018-01-07 10:49:02 +08:00
2018-01-29 08:30:14 +08:00
if (!origNoteImage || origNoteImage.dateModified <= entity.dateModified) {
await sql.transactional(async () => {
await sql.replace("note_images", entity);
2018-01-07 10:49:02 +08:00
await syncTableService.addNoteImageSync(entity.noteImageId, sourceId);
2018-01-07 10:49:02 +08:00
});
2018-01-29 08:30:14 +08:00
log.info("Update/sync note image " + entity.noteImageId);
2018-01-07 10:49:02 +08:00
}
}
async function updateLabel(entity, sourceId) {
const origLabel = await sql.getRow("SELECT * FROM labels WHERE labelId = ?", [entity.labelId]);
if (!origLabel || origLabel.dateModified <= entity.dateModified) {
await sql.transactional(async () => {
await sql.replace("labels", entity);
await syncTableService.addLabelSync(entity.labelId, sourceId);
});
log.info("Update/sync label " + entity.labelId);
}
}
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) {
await sql.transactional(async () => {
2018-02-11 13:18:59 +08:00
await sql.replace("api_tokens", entity);
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
};