From a2f0a372a50e6613d1fc4c8c2a59709e22771ff4 Mon Sep 17 00:00:00 2001 From: azivner Date: Thu, 26 Oct 2017 23:21:31 -0400 Subject: [PATCH] sync fixes etc., push/pull sync is now working in basic form --- app.js | 2 +- routes/api/audit.js | 9 ++++++--- services/audit_category.js | 3 ++- services/backup.js | 8 ++++---- services/sql.js | 6 ++++-- services/sync.js | 27 +++++++++++++++++++++------ 6 files changed, 38 insertions(+), 17 deletions(-) diff --git a/app.js b/app.js index 7cfb930fe..231f3aa08 100644 --- a/app.js +++ b/app.js @@ -48,7 +48,7 @@ app.use((req, res, next) => { next(); }); -app.use(bodyParser.json()); +app.use(bodyParser.json({limit: '50mb'})); app.use(bodyParser.urlencoded({extended: false})); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); diff --git a/routes/api/audit.js b/routes/api/audit.js index f05b46762..4311d0d9e 100644 --- a/routes/api/audit.js +++ b/routes/api/audit.js @@ -10,11 +10,14 @@ router.get('/:full_load_time', auth.checkApiAuth, async (req, res, next) => { const browserId = req.get('x-browser-id'); - const count = await sql.getSingleResult("SELECT COUNT(*) AS 'count' FROM audit_log WHERE browser_id != ? " + - "AND date_modified >= ?", [browserId, fullLoadTime])['count']; + 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]); + + console.log("SELECT COUNT(*) AS 'count' FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " + + "AND date_modified >= ?"); res.send({ - 'changed': count > 0 + 'changed': row.count > 0 }); }); diff --git a/services/audit_category.js b/services/audit_category.js index 5f191f08c..b36906ce4 100644 --- a/services/audit_category.js +++ b/services/audit_category.js @@ -9,5 +9,6 @@ module.exports = { CHANGE_PARENT: 'PARENT', ENCRYPTION: 'ENCRYPTION', CHANGE_PASSWORD: 'PASSWORD', - SETTINGS: 'SETTINGS' + SETTINGS: 'SETTINGS', + SYNC: 'SYNC' }; \ No newline at end of file diff --git a/services/backup.js b/services/backup.js index fbbdd91b7..d0999f8d7 100644 --- a/services/backup.js +++ b/services/backup.js @@ -6,6 +6,10 @@ const fs = require('fs-extra'); const dataDir = require('./data_dir'); const log = require('./log'); +if (!fs.existsSync(dataDir.BACKUP_DIR)) { + fs.mkdirSync(dataDir.BACKUP_DIR, 0o700); +} + async function regularBackup() { const now = utils.nowTimestamp(); 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); - if (!fs.existsSync(dataDir.BACKUP_DIR)) { - fs.mkdirSync(dataDir.BACKUP_DIR, 0o700); - } - const backupFile = dataDir.BACKUP_DIR + "/" + "backup-" + date_str + ".db"; fs.copySync(dataDir.DOCUMENT_PATH, backupFile); diff --git a/services/sql.js b/services/sql.js index b59a8377b..6e6e25d1d 100644 --- a/services/sql.js +++ b/services/sql.js @@ -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 + ", to=" + changeTo + ", comment=" + comment); - await execute("INSERT INTO audit_log (date_modified, category, browser_id, note_id, change_from, change_to, comment)" - + " VALUES (?, ?, ?, ?, ?, ?, ?)", [now, category, browserId, noteId, changeFrom, changeTo, comment]); + const id = utils.randomToken(14); + + 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) { diff --git a/services/sync.js b/services/sync.js index 217c384ae..c54efc49e 100644 --- a/services/sync.js +++ b/services/sync.js @@ -5,8 +5,10 @@ const rp = require('request-promise'); const sql = require('./sql'); const migration = require('./migration'); 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; @@ -81,7 +83,7 @@ async function pushSync() { }); } - await sql.setOption('last_synced_pull', syncStarted); + await sql.setOption('last_synced_push', syncStarted); } async function sync() { @@ -139,12 +141,16 @@ async function putChanged(changed) { 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) { 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) { await sql.insert("images", image); @@ -156,13 +162,22 @@ async function putNote(note) { await sql.insert("notes_history", history); } + await sql.addAudit(audit_category.SYNC); + log.info("Update/sync note " + note.detail.note_id); } -setInterval(sync, 60000); +if (SYNC_SERVER) { + log.info("Setting up sync"); -// kickoff initial sync immediately -setTimeout(sync, 1000); + setInterval(sync, 60000); + + // kickoff initial sync immediately + setTimeout(sync, 1000); +} +else { + log.info("Sync server not configured, sync timer not running.") +} module.exports = { getChangedSince,