sync fixes etc., push/pull sync is now working in basic form

This commit is contained in:
azivner 2017-10-26 23:21:31 -04:00
parent 297da47b34
commit a2f0a372a5
6 changed files with 38 additions and 17 deletions

2
app.js
View File

@ -48,7 +48,7 @@ app.use((req, res, next) => {
next(); next();
}); });
app.use(bodyParser.json()); app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({extended: false})); app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser()); app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));

View File

@ -10,11 +10,14 @@ router.get('/:full_load_time', auth.checkApiAuth, async (req, res, next) => {
const browserId = req.get('x-browser-id'); const browserId = req.get('x-browser-id');
const count = await sql.getSingleResult("SELECT COUNT(*) AS 'count' FROM audit_log WHERE browser_id != ? " + const row = await sql.getSingleResult("SELECT COUNT(*) AS 'count' FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " +
"AND date_modified >= ?", [browserId, fullLoadTime])['count']; "AND date_modified >= ?", [browserId, fullLoadTime]);
console.log("SELECT COUNT(*) AS 'count' FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " +
"AND date_modified >= ?");
res.send({ res.send({
'changed': count > 0 'changed': row.count > 0
}); });
}); });

View File

@ -9,5 +9,6 @@ module.exports = {
CHANGE_PARENT: 'PARENT', CHANGE_PARENT: 'PARENT',
ENCRYPTION: 'ENCRYPTION', ENCRYPTION: 'ENCRYPTION',
CHANGE_PASSWORD: 'PASSWORD', CHANGE_PASSWORD: 'PASSWORD',
SETTINGS: 'SETTINGS' SETTINGS: 'SETTINGS',
SYNC: 'SYNC'
}; };

View File

@ -6,6 +6,10 @@ const fs = require('fs-extra');
const dataDir = require('./data_dir'); const dataDir = require('./data_dir');
const log = require('./log'); const log = require('./log');
if (!fs.existsSync(dataDir.BACKUP_DIR)) {
fs.mkdirSync(dataDir.BACKUP_DIR, 0o700);
}
async function regularBackup() { async function regularBackup() {
const now = utils.nowTimestamp(); const now = utils.nowTimestamp();
const last_backup_date = parseInt(await sql.getOption('last_backup_date')); const last_backup_date = parseInt(await sql.getOption('last_backup_date'));
@ -22,10 +26,6 @@ async function backupNow() {
const date_str = new Date().toISOString().substr(0, 19); const date_str = new Date().toISOString().substr(0, 19);
if (!fs.existsSync(dataDir.BACKUP_DIR)) {
fs.mkdirSync(dataDir.BACKUP_DIR, 0o700);
}
const backupFile = dataDir.BACKUP_DIR + "/" + "backup-" + date_str + ".db"; const backupFile = dataDir.BACKUP_DIR + "/" + "backup-" + date_str + ".db";
fs.copySync(dataDir.DOCUMENT_PATH, backupFile); fs.copySync(dataDir.DOCUMENT_PATH, backupFile);

View File

@ -82,8 +82,10 @@ async function addAudit(category, req=null, noteId=null, changeFrom=null, change
log.info("audit: " + category + ", browserId=" + browserId + ", noteId=" + noteId + ", from=" + changeFrom log.info("audit: " + category + ", browserId=" + browserId + ", noteId=" + noteId + ", from=" + changeFrom
+ ", to=" + changeTo + ", comment=" + comment); + ", to=" + changeTo + ", comment=" + comment);
await execute("INSERT INTO audit_log (date_modified, category, browser_id, note_id, change_from, change_to, comment)" const id = utils.randomToken(14);
+ " VALUES (?, ?, ?, ?, ?, ?, ?)", [now, category, browserId, noteId, changeFrom, changeTo, comment]);
await execute("INSERT INTO audit_log (id, date_modified, category, browser_id, note_id, change_from, change_to, comment)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [id, now, category, browserId, noteId, changeFrom, changeTo, comment]);
} }
async function deleteRecentAudits(category, req, noteId) { async function deleteRecentAudits(category, req, noteId) {

View File

@ -5,8 +5,10 @@ const rp = require('request-promise');
const sql = require('./sql'); const sql = require('./sql');
const migration = require('./migration'); const migration = require('./migration');
const utils = require('./utils'); const utils = require('./utils');
const config = require('./config');
const audit_category = require('./audit_category');
const SYNC_SERVER = 'http://localhost:3000'; const SYNC_SERVER = config['Sync']['syncServerHost'];
let syncInProgress = false; let syncInProgress = false;
@ -81,7 +83,7 @@ async function pushSync() {
}); });
} }
await sql.setOption('last_synced_pull', syncStarted); await sql.setOption('last_synced_push', syncStarted);
} }
async function sync() { async function sync() {
@ -139,12 +141,16 @@ async function putChanged(changed) {
log.info("Update/sync audit_log for noteId=" + audit.note_id); log.info("Update/sync audit_log for noteId=" + audit.note_id);
} }
if (changed.tree.length > 0 || changed.audit_log.length > 0) {
await sql.addAudit(audit_category.SYNC);
}
} }
async function putNote(note) { async function putNote(note) {
await sql.insert("notes", note.detail, true); await sql.insert("notes", note.detail, true);
await sql.remove("images", node.detail.note_id); await sql.remove("images", note.detail.note_id);
for (const image of note.images) { for (const image of note.images) {
await sql.insert("images", image); await sql.insert("images", image);
@ -156,13 +162,22 @@ async function putNote(note) {
await sql.insert("notes_history", history); await sql.insert("notes_history", history);
} }
await sql.addAudit(audit_category.SYNC);
log.info("Update/sync note " + note.detail.note_id); log.info("Update/sync note " + note.detail.note_id);
} }
setInterval(sync, 60000); if (SYNC_SERVER) {
log.info("Setting up sync");
// kickoff initial sync immediately setInterval(sync, 60000);
setTimeout(sync, 1000);
// kickoff initial sync immediately
setTimeout(sync, 1000);
}
else {
log.info("Sync server not configured, sync timer not running.")
}
module.exports = { module.exports = {
getChangedSince, getChangedSince,