Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
number of sync changes to push next to "sync now" button
azivner962c078bbcd7
diff --git a/app.js b/app.jsindex 944c6432e5..2e11942211 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.jsnew file mode 100644index 0000000000..b9cab1c26c--- /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 filediff --git a/public/javascripts/sync.js b/public/javascripts/sync.jsindex 38515a6142..513da9f544 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.jsindex cf04b3abdd..8bcc59c46d 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.jssimilarity index 51%rename from routes/api/audit.jsrename to routes/api/status.jsindex 4e376d680f..58d32304cf 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.jsindex 34a2f705a1..5851ac8293 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 exceptiondiff --git a/services/sync.js b/services/sync.jsindex 52f62710d8..4ec32e761d 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.ejsindex eb0d33e494..36fb35c01c 100644--- a/views/index.ejs+++ b/views/index.ejs@@ -21,7 +21,7 @@<span id="top-message"></span><span id="error-message"></span>- <button class="btn btn-xs" onclick="syncNow();">Sync now</button>+ <button class="btn btn-xs" onclick="syncNow();">Sync now (<span id="changesToPushCount">0</span>)</button><button class="btn btn-xs" onclick="displaySettings();">Settings</button>@@ -276,6 +276,7 @@<script src="javascripts/note_history.js"></script><script src="javascripts/recent_changes.js"></script>+ <script src="javascripts/status.js"></script><script src="javascripts/sync.js"></script><script src="javascripts/utils.js"></script></body>