trilium/services/sync_update.js

121 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-11-10 09:52:47 +08:00
const sql = require('./sql');
const log = require('./log');
const options = require('./options');
const utils = require('./utils');
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, links, sourceId) {
2017-11-21 12:51:28 +08:00
const origNote = await sql.getSingleResult("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 db => {
await sql.replace(db, "notes", entity);
2017-11-10 09:52:47 +08:00
await sql.remove(db, "links", entity.note_id);
2017-11-10 09:52:47 +08:00
for (const link of links) {
delete link['lnk_id'];
//await sql.insert(db, 'link', link);
2017-11-10 09:52:47 +08:00
}
await sync_table.addNoteSync(db, entity.note_id, sourceId);
await eventLog.addNoteEvent(db, entity.note_id, "Synced note <note>");
2017-11-10 09:52:47 +08:00
});
log.info("Update/sync note " + entity.note_id);
}
else {
await eventLog.addNoteEvent(db, entity.note_id, "Sync conflict in note <note>, " + utils.formatTwoTimestamps(origNote.date_modified, entity.date_modified));
2017-11-10 09:52:47 +08:00
}
}
async function updateNoteTree(entity, sourceId) {
2017-11-21 12:51:28 +08:00
const orig = await sql.getSingleResultOrNull("SELECT * FROM notes_tree WHERE note_tree_id = ?", [entity.note_tree_id]);
2017-11-10 09:52:47 +08:00
await sql.doInTransaction(async db => {
if (orig === null || orig.date_modified < entity.date_modified) {
delete entity.is_expanded;
await sql.replace(db, 'notes_tree', entity);
2017-11-10 09:52:47 +08:00
await sync_table.addNoteTreeSync(db, entity.note_tree_id, sourceId);
2017-11-10 09:52:47 +08:00
log.info("Update/sync note tree " + entity.note_tree_id);
}
else {
await eventLog.addNoteEvent(db, entity.note_tree_id, "Sync conflict in note tree <note>, " + utils.formatTwoTimestamps(orig.date_modified, entity.date_modified));
}
});
2017-11-10 09:52:47 +08:00
}
async function updateNoteHistory(entity, sourceId) {
2017-11-21 12:51:28 +08:00
const orig = await sql.getSingleResultOrNull("SELECT * FROM notes_history WHERE note_history_id = ?", [entity.note_history_id]);
2017-11-10 09:52:47 +08:00
await sql.doInTransaction(async db => {
if (orig === null || orig.date_modified_to < entity.date_modified_to) {
await sql.replace(db, 'notes_history', entity);
2017-11-10 09:52:47 +08:00
await sync_table.addNoteHistorySync(db, entity.note_history_id, sourceId);
2017-11-10 09:52:47 +08:00
log.info("Update/sync note history " + entity.note_history_id);
}
else {
await eventLog.addNoteEvent(db, entity.note_id, "Sync conflict in note history for <note>, " + utils.formatTwoTimestamps(orig.date_modified_to, entity.date_modified_to));
}
});
2017-11-10 09:52:47 +08:00
}
async function updateNoteReordering(entity, sourceId) {
await sql.doInTransaction(async db => {
2017-11-10 09:52:47 +08:00
Object.keys(entity.ordering).forEach(async key => {
await sql.execute(db, "UPDATE notes_tree SET note_pos = ? WHERE note_tree_id = ?", [entity.ordering[key], key]);
2017-11-10 09:52:47 +08:00
});
await sync_table.addNoteReorderingSync(db, entity.note_pid, sourceId);
2017-11-10 09:52:47 +08:00
});
}
async function updateOptions(entity, sourceId) {
if (!options.SYNCED_OPTIONS.includes(entity.opt_name)) {
return;
}
2017-11-21 12:51:28 +08:00
const orig = await sql.getSingleResultOrNull("SELECT * FROM options WHERE opt_name = ?", [entity.opt_name]);
2017-11-10 09:52:47 +08:00
await sql.doInTransaction(async db => {
if (orig === null || orig.date_modified < entity.date_modified) {
await sql.replace(db, 'options', entity);
2017-11-10 09:52:47 +08:00
await sync_table.addOptionsSync(db, entity.opt_name, sourceId);
2017-11-10 09:52:47 +08:00
await eventLog.addEvent(db, "Synced option " + entity.opt_name);
}
else {
await eventLog.addEvent(db, "Sync conflict in options for " + entity.opt_name + ", " + utils.formatTwoTimestamps(orig.date_modified, entity.date_modified));
}
});
2017-11-10 09:52:47 +08:00
}
async function updateRecentNotes(entity, sourceId) {
2017-11-21 12:51:28 +08:00
const orig = await sql.getSingleResultOrNull("SELECT * FROM recent_notes WHERE note_path = ?", [entity.note_path]);
2017-11-10 09:52:47 +08:00
if (orig === null || orig.date_accessed < entity.date_accessed) {
await sql.doInTransaction(async db => {
await sql.replace(db, 'recent_notes', entity);
2017-11-10 09:52:47 +08:00
await sync_table.addRecentNoteSync(db, entity.note_path, sourceId);
2017-11-10 09:52:47 +08:00
});
}
}
module.exports = {
updateNote,
updateNoteTree,
updateNoteHistory,
updateNoteReordering,
updateOptions,
updateRecentNotes
};