trilium/services/sync_update.js

150 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-11-10 09:52:47 +08:00
const sql = require('./sql');
const log = require('./log');
const eventLog = require('./event_log');
const notes = require('./notes');
const sync_table = require('./sync_table');
2017-11-10 09:52:47 +08:00
async function updateNote(entity, sourceId) {
const origNote = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [entity.note_id]);
2017-11-10 09:52:47 +08:00
if (!origNote || origNote.date_modified <= entity.date_modified) {
await sql.doInTransaction(async () => {
await sql.replace("notes", entity);
2017-11-10 09:52:47 +08:00
await sync_table.addNoteSync(entity.note_id, sourceId);
await eventLog.addNoteEvent(entity.note_id, "Synced note <note>");
2017-11-10 09:52:47 +08:00
});
log.info("Update/sync note " + entity.note_id);
}
}
async function updateNoteTree(entity, sourceId) {
const orig = await sql.getFirstOrNull("SELECT * FROM notes_tree WHERE note_tree_id = ?", [entity.note_tree_id]);
2017-11-10 09:52:47 +08:00
await sql.doInTransaction(async () => {
if (orig === null || orig.date_modified < entity.date_modified) {
delete entity.is_expanded;
await sql.replace('notes_tree', entity);
2017-11-10 09:52:47 +08:00
await sync_table.addNoteTreeSync(entity.note_tree_id, sourceId);
2017-11-10 09:52:47 +08:00
log.info("Update/sync note tree " + entity.note_tree_id);
}
});
2017-11-10 09:52:47 +08:00
}
async function updateNoteHistory(entity, sourceId) {
const orig = await sql.getFirstOrNull("SELECT * FROM notes_history WHERE note_history_id = ?", [entity.note_history_id]);
2017-11-10 09:52:47 +08:00
await sql.doInTransaction(async () => {
// we update note history even if date modified to is the same because the only thing which might have changed
// is the protected status (and correnspondingly note_title and note_text) which doesn't affect the date_modified_to
if (orig === null || orig.date_modified_to <= entity.date_modified_to) {
await sql.replace('notes_history', entity);
2017-11-10 09:52:47 +08:00
await sync_table.addNoteHistorySync(entity.note_history_id, sourceId);
2017-11-10 09:52:47 +08:00
log.info("Update/sync note history " + entity.note_history_id);
}
});
2017-11-10 09:52:47 +08:00
}
async function updateNoteReordering(entity, sourceId) {
await sql.doInTransaction(async () => {
2017-11-10 09:52:47 +08:00
Object.keys(entity.ordering).forEach(async key => {
await sql.execute("UPDATE notes_tree SET note_position = ? WHERE note_tree_id = ?", [entity.ordering[key], key]);
2017-11-10 09:52:47 +08:00
});
await sync_table.addNoteReorderingSync(entity.parent_note_id, sourceId);
2017-11-10 09:52:47 +08:00
});
}
async function updateOptions(entity, sourceId) {
const orig = await sql.getFirstOrNull("SELECT * FROM options WHERE opt_name = ?", [entity.opt_name]);
if (!orig.is_synced) {
2017-11-10 09:52:47 +08:00
return;
}
await sql.doInTransaction(async () => {
if (orig === null || orig.date_modified < entity.date_modified) {
await sql.replace('options', entity);
2017-11-10 09:52:47 +08:00
await sync_table.addOptionsSync(entity.opt_name, sourceId);
2017-11-10 09:52:47 +08:00
await eventLog.addEvent("Synced option " + entity.opt_name);
}
});
2017-11-10 09:52:47 +08:00
}
async function updateRecentNotes(entity, sourceId) {
const orig = await sql.getFirstOrNull("SELECT * FROM recent_notes WHERE note_tree_id = ?", [entity.note_tree_id]);
2017-11-10 09:52:47 +08:00
if (orig === null || orig.date_accessed < entity.date_accessed) {
await sql.doInTransaction(async () => {
await sql.replace('recent_notes', entity);
2017-11-10 09:52:47 +08:00
await sync_table.addRecentNoteSync(entity.note_tree_id, 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.getFirst("SELECT * FROM images WHERE image_id = ?", [entity.image_id]);
if (!origImage || origImage.date_modified <= entity.date_modified) {
await sql.doInTransaction(async () => {
await sql.replace("images", entity);
await sync_table.addImageSync(entity.image_id, sourceId);
});
log.info("Update/sync image " + entity.image_id);
}
}
2018-01-07 10:49:02 +08:00
async function updateNoteImage(entity, sourceId) {
const origNoteImage = await sql.getFirst("SELECT * FROM notes_image WHERE note_image_id = ?", [entity.note_image_id]);
if (!origNoteImage || origNoteImage.date_modified <= entity.date_modified) {
await sql.doInTransaction(async () => {
await sql.replace("notes_image", entity);
await sync_table.addNoteImageSync(entity.note_image_id, sourceId);
});
log.info("Update/sync note image " + entity.note_image_id);
}
}
async function updateAttribute(entity, sourceId) {
const origAttribute = await sql.getFirst("SELECT * FROM attribute WHERE attribute_id = ?", [entity.attribute_id]);
if (!origAttribute || origAttribute.date_modified <= entity.date_modified) {
await sql.doInTransaction(async () => {
await sql.replace("attribute", entity);
await sync_table.addAttributeSync(entity.attribute_id, sourceId);
});
log.info("Update/sync attribute " + entity.attribute_id);
}
}
2017-11-10 09:52:47 +08:00
module.exports = {
updateNote,
updateNoteTree,
updateNoteHistory,
updateNoteReordering,
updateOptions,
2018-01-07 04:56:00 +08:00
updateRecentNotes,
2018-01-07 10:49:02 +08:00
updateImage,
updateNoteImage,
updateAttribute
2017-11-10 09:52:47 +08:00
};