diff --git a/app.js b/app.js index 944c6432e..2e1194221 100644 --- a/app.js +++ b/app.js @@ -20,7 +20,7 @@ const migrationRoute = require('./routes/migration'); const treeApiRoute = require('./routes/api/tree'); const notesApiRoute = require('./routes/api/notes'); const notesMoveApiRoute = require('./routes/api/notes_move'); -const auditApiRoute = require('./routes/api/audit'); +const statusApiRoute = require('./routes/api/status'); const noteHistoryApiRoute = require('./routes/api/note_history'); const recentChangesApiRoute = require('./routes/api/recent_changes'); const settingsApiRoute = require('./routes/api/settings'); @@ -88,7 +88,7 @@ app.use('/migration', migrationRoute); app.use('/api/tree', treeApiRoute); app.use('/api/notes', notesApiRoute); app.use('/api/notes', notesMoveApiRoute); -app.use('/api/audit', auditApiRoute); +app.use('/api/status', statusApiRoute); app.use('/api/notes-history', noteHistoryApiRoute); app.use('/api/recent-changes', recentChangesApiRoute); app.use('/api/settings', settingsApiRoute); diff --git a/public/javascripts/status.js b/public/javascripts/status.js new file mode 100644 index 000000000..b9cab1c26 --- /dev/null +++ b/public/javascripts/status.js @@ -0,0 +1,27 @@ +function checkStatus() { + $.ajax({ + url: baseApiUrl + 'status/' + globalFullLoadTime, + type: 'GET', + success: resp => { + if (resp.changed) { + window.location.reload(true); + } + else { + $("#changesToPushCount").html(resp.changesToPushCount); + } + }, + statusCode: { + 401: () => { + // if the user got logged out then we should display the page + // here we do that by reloading which will force the redirect if the user is really logged out + window.location.reload(true); + }, + 409: () => { + // 409 means we need to migrate database, reload will take care of it + window.location.reload(true); + } + } + }); +} + +setInterval(checkStatus, 10 * 1000); \ No newline at end of file diff --git a/public/javascripts/sync.js b/public/javascripts/sync.js index 38515a614..513da9f54 100644 --- a/public/javascripts/sync.js +++ b/public/javascripts/sync.js @@ -4,7 +4,7 @@ function syncNow() { type: 'POST', success: result => { if (result.success) { - checkAudit(); + checkStatus(); message("Sync finished successfully"); diff --git a/public/javascripts/tree.js b/public/javascripts/tree.js index cf04b3abd..8bcc59c46 100644 --- a/public/javascripts/tree.js +++ b/public/javascripts/tree.js @@ -88,31 +88,6 @@ let globalEncryptionSessionTimeout; let globalEncryptedDataKey; let globalFullLoadTime; -function checkAudit() { - $.ajax({ - url: baseApiUrl + 'audit/' + globalFullLoadTime, - type: 'GET', - success: resp => { - if (resp.changed) { - window.location.reload(true); - } - }, - statusCode: { - 401: () => { - // if the user got logged out then we should display the page - // here we do that by reloading which will force the redirect if the user is really logged out - window.location.reload(true); - }, - 409: () => { - // 409 means we need to migrate database, reload will take care of it - window.location.reload(true); - } - } - }); -} - -setInterval(checkAudit, 10 * 1000); - $(() => { $.get(baseApiUrl + 'tree').then(resp => { const notes = resp.notes; diff --git a/routes/api/audit.js b/routes/api/status.js similarity index 54% rename from routes/api/audit.js rename to routes/api/status.js index 4e376d680..58d32304c 100644 --- a/routes/api/audit.js +++ b/routes/api/status.js @@ -10,11 +10,15 @@ router.get('/:full_load_time', auth.checkApiAuth, async (req, res, next) => { const browserId = req.get('x-browser-id'); - const row = await sql.getSingleResult("SELECT COUNT(*) AS 'count' FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " + + const rowCount = await sql.getSingleValue("SELECT COUNT(*) FROM audit_log WHERE (browser_id IS NULL OR browser_id != ?) " + "AND date_modified >= ?", [browserId, fullLoadTime]); + const lastSyncedPush = await sql.getOption('last_synced_push'); + const changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]); + res.send({ - 'changed': row.count > 0 + 'changed': rowCount > 0, + 'changesToPushCount': changesToPushCount }); }); diff --git a/services/sql.js b/services/sql.js index 34a2f705a..5851ac829 100644 --- a/services/sql.js +++ b/services/sql.js @@ -55,11 +55,11 @@ async function setOption(optName, optValue) { } async function getSingleResult(query, params = []) { - return await db.get(query, ...params); + return await wrap(async () => db.get(query, ...params)); } async function getSingleResultOrNull(query, params = []) { - const all = await db.all(query, ...params); + const all = await wrap(async () => db.all(query, ...params)); return all.length > 0 ? all[0] : null; } @@ -75,7 +75,7 @@ async function getSingleValue(query, params = []) { } async function getResults(query, params = []) { - return await db.all(query, ...params); + return await wrap(async () => db.all(query, ...params)); } async function getFlattenedResults(key, query, params = []) { @@ -90,11 +90,11 @@ async function getFlattenedResults(key, query, params = []) { } async function execute(query, params = []) { - return await db.run(query, ...params); + return await wrap(async () => db.run(query, ...params)); } async function executeScript(query) { - return await db.exec(query); + return await wrap(async () => db.exec(query)); } async function remove(tableName, noteId) { @@ -145,6 +145,19 @@ async function addEntitySync(entityName, entityId, sourceId) { }); } +async function wrap(func) { + const error = new Error(); + + try { + return await func(); + } + catch (e) { + log.error("Error executing transaction, executing rollback. Inner exception: " + e.stack + error.stack); + + throw e; + } +} + async function doInTransaction(func) { const error = new Error(); // to capture correct stack trace in case of exception diff --git a/services/sync.js b/services/sync.js index 52f62710d..4ec32e761 100644 --- a/services/sync.js +++ b/services/sync.js @@ -178,9 +178,9 @@ async function sync() { const cookieJar = await login(syncLog); - await pushSync(cookieJar, syncLog); - await pullSync(cookieJar, syncLog); + + await pushSync(cookieJar, syncLog); } catch (e) { logSync("sync failed: " + e.stack, syncLog); diff --git a/views/index.ejs b/views/index.ejs index eb0d33e49..36fb35c01 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -21,7 +21,7 @@ - + @@ -276,6 +276,7 @@ +