trilium/services/sync.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
2017-10-26 10:39:21 +08:00
const log = require('./log');
const rp = require('request-promise');
const sql = require('./sql');
const migration = require('./migration');
2017-10-26 10:39:21 +08:00
const SYNC_SERVER = 'http://localhost:3000';
let syncInProgress = false;
async function sync() {
try {
syncInProgress = true;
if (!await migration.isDbUpToDate()) {
return;
}
const lastSynced = parseInt(await sql.getOption('last_synced'));
const resp = await rp({
uri: SYNC_SERVER + '/api/sync/changed/' + lastSynced,
headers: {
auth: 'sync'
},
json: true
});
try {
sql.beginTransaction();
for (const treeItem of resp.tree) {
delete treeItem['id'];
await sql.insert("notes_tree", treeItem, true);
log.info("Syncing notes_tree " + treeItem.note_id);
}
for (const audit of resp.audit_log) {
delete audit['id'];
await sql.insert("audit_log", audit, true);
log.info("Syncing audit_log for noteId=" + audit.note_id);
}
for (const noteId of resp.notes) {
const note = await rp({
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSynced,
headers: {
auth: 'sync'
},
json: true
});
console.log(noteId);
await sql.insert("notes", note.detail, true);
await sql.remove("images", noteId);
for (const image of note.images) {
await sql.insert("images", image);
}
for (const history of note.history) {
delete history['id'];
await sql.insert("notes_history", history);
}
}
sql.commit();
}
catch (e) {
sql.rollback();
throw e;
}
}
catch (e) {
log.error("sync failed: " + e.stack);
}
finally {
syncInProgress = false;
}
}
2017-10-26 10:39:21 +08:00
setInterval(sync, 60000);
setTimeout(sync, 1000);