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-23 08:22:09 +08:00
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
const SYNC_SERVER = 'http://localhost:3000';
|
|
|
|
|
|
|
|
|
|
|
|
let syncInProgress = false;
|
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
async function pullSync() {
|
|
|
|
const lastSynced = parseInt(await sql.getOption('last_synced_pull'));
|
|
|
|
|
|
|
|
const resp = await rp({
|
|
|
|
uri: SYNC_SERVER + '/api/sync/changed/' + lastSynced,
|
|
|
|
headers: {
|
|
|
|
auth: 'sync'
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
});
|
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
try {
|
2017-10-27 08:31:31 +08:00
|
|
|
await sql.beginTransaction();
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
for (const treeItem of resp.tree) {
|
|
|
|
delete treeItem['id'];
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
await sql.insert("notes_tree", treeItem, true);
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
log.info("Syncing notes_tree " + treeItem.note_id);
|
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
for (const audit of resp.audit_log) {
|
|
|
|
delete audit['id'];
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
await sql.insert("audit_log", audit, true);
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
log.info("Syncing audit_log for noteId=" + audit.note_id);
|
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
for (const noteId of resp.notes) {
|
|
|
|
const note = await rp({
|
|
|
|
uri: SYNC_SERVER + "/api/sync/note/" + noteId + "/" + lastSynced,
|
|
|
|
headers: {
|
|
|
|
auth: 'sync'
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
});
|
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
await sql.insert("notes", note.detail, true);
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
await sql.remove("images", noteId);
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
for (const image of note.images) {
|
|
|
|
await sql.insert("images", image);
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
for (const history of note.history) {
|
|
|
|
delete history['id'];
|
|
|
|
|
|
|
|
await sql.insert("notes_history", history);
|
|
|
|
}
|
2017-10-27 08:54:10 +08:00
|
|
|
|
|
|
|
log.info("Syncing note" + noteId);
|
2017-10-27 08:31:31 +08:00
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:54:10 +08:00
|
|
|
await sql.setOption('last_synced_pull', resp.syncTimestamp);
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
await sql.commit();
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
await sql.rollback();
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
async function pushSync() {
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
async function sync() {
|
|
|
|
if (syncInProgress) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-26 10:39:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
syncInProgress = true;
|
2017-10-27 07:22:21 +08:00
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
try {
|
|
|
|
if (!await migration.isDbUpToDate()) {
|
|
|
|
return;
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
await pushSync();
|
|
|
|
|
|
|
|
await pullSync();
|
2017-10-26 10:39:21 +08:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
log.error("sync failed: " + e.stack);
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
syncInProgress = false;
|
|
|
|
}
|
2017-10-23 08:22:09 +08:00
|
|
|
}
|
|
|
|
|
2017-10-26 10:39:21 +08:00
|
|
|
setInterval(sync, 60000);
|
|
|
|
|
2017-10-27 08:31:31 +08:00
|
|
|
// kickoff initial sync immediately
|
2017-10-26 10:39:21 +08:00
|
|
|
setTimeout(sync, 1000);
|