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
event_log - entity and logging sync info
azivnere35c2dd4adac
diff --git a/migrations/0025__create_event_log.sql b/migrations/0025__create_event_log.sqlnew file mode 100644index 0000000000..ea22282b4b--- /dev/null+++ b/migrations/0025__create_event_log.sql@@ -0,0 +1,10 @@+CREATE TABLE `event_log` (+ `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,+ `note_id` TEXT,+ `comment` TEXT,+ `date_added` INTEGER NOT NULL+);++CREATE INDEX `IDX_event_log_date_added` ON `event_log` (+ `date_added`+);\ No newline at end of filediff --git a/public/javascripts/encryption.js b/public/javascripts/encryption.jsindex b55cd9ba19..780b3ded7c 100644--- a/public/javascripts/encryption.js+++ b/public/javascripts/encryption.js@@ -108,7 +108,7 @@ $("#encryption-password-form").submit(() => {.catch(reason => {console.log(reason);- alert(reason);+ error(reason);});return false;@@ -257,7 +257,7 @@ async function changeEncryptionOnNoteHistory(noteId, encrypt) {const result = await $.ajax({url: baseApiUrl + 'notes-history/' + noteId + "?encryption=" + (encrypt ? 0 : 1),type: 'GET',- error: () => alert("Error getting note history.")+ error: () => error("Error getting note history.")});for (const row of result) {@@ -277,7 +277,7 @@ async function changeEncryptionOnNoteHistory(noteId, encrypt) {type: 'PUT',contentType: 'application/json',data: JSON.stringify(row),- error: () => alert("Error de/encrypting note history.")+ error: () => error("Error de/encrypting note history.")});console.log('Note history ' + row.note_history_id + ' de/encrypted');@@ -335,7 +335,7 @@ async function encryptSubTree(noteId) {}});- alert("Encryption finished.");+ message("Encryption finished.");}async function decryptSubTree(noteId) {@@ -362,7 +362,7 @@ async function decryptSubTree(noteId) {}});- alert("Decryption finished.");+ message("Decryption finished.");}function updateSubTreeRecursively(noteId, updateCallback, successCallback) {diff --git a/public/javascripts/migration.js b/public/javascripts/migration.jsindex 8acf179103..9eb12c557a 100644--- a/public/javascripts/migration.js+++ b/public/javascripts/migration.js@@ -38,6 +38,6 @@ $("#run-migration").click(() => {$("#migration-table").append(row);}},- error: () => alert("Migration failed with unknown error")+ error: () => error("Migration failed with unknown error")});});\ No newline at end of filediff --git a/public/javascripts/note_history.js b/public/javascripts/note_history.jsindex b508210bab..8f0345d870 100644--- a/public/javascripts/note_history.js+++ b/public/javascripts/note_history.js@@ -37,7 +37,7 @@ function showNoteHistoryDialog(noteId, noteHistoryId) {$("#note-history-list").val(noteHistoryId).trigger('change');}},- error: () => alert("Error getting note history.")+ error: () => error("Error getting note history.")});}diff --git a/public/javascripts/recent_changes.js b/public/javascripts/recent_changes.jsindex b1552598f0..fd3362794f 100644--- a/public/javascripts/recent_changes.js+++ b/public/javascripts/recent_changes.js@@ -70,7 +70,7 @@ function showRecentChanges() {$("#recent-changes-dialog").append(dayEl);}},- error: () => alert("Error getting recent changes.")+ error: () => error("Error getting recent changes.")});}diff --git a/public/javascripts/sync.js b/public/javascripts/sync.jsindex 513da9f544..ebe03fae88 100644--- a/public/javascripts/sync.js+++ b/public/javascripts/sync.js@@ -6,17 +6,12 @@ function syncNow() {if (result.success) {checkStatus();- message("Sync finished successfully");-- for (const l of result.log)- {- console.log(l);- }+ message("Sync triggered.");}else {- alert("Sync failed");+ error("Sync failed");}},- error: () => alert("Sync failed")+ error: () => error("Sync failed")});}\ No newline at end of filediff --git a/routes/api/sync.js b/routes/api/sync.jsindex adcb2c2c41..ee2306bbaa 100644--- a/routes/api/sync.js+++ b/routes/api/sync.js@@ -8,11 +8,10 @@ const sql = require('../../services/sql');const options = require('../../services/options');router.post('/now', auth.checkApiAuth, async (req, res, next) => {- const log = await sync.sync();+ await sync.sync();res.send({- success: true,- log: log+ success: true});});diff --git a/services/event_log.js b/services/event_log.jsnew file mode 100644index 0000000000..2e1ce39713--- /dev/null+++ b/services/event_log.js@@ -0,0 +1,19 @@+const sql = require('./sql');+const utils = require('./utils');++async function addEvent(comment) {+ await addNoteEvent(null, comment);+}++async function addNoteEvent(noteId, comment) {+ await sql.insert('event_log', {+ note_id : noteId,+ comment: comment,+ date_added: utils.nowTimestamp()+ });+}++module.exports = {+ addEvent,+ addNoteEvent+};\ No newline at end of filediff --git a/services/migration.js b/services/migration.jsindex 98e7b98846..e4045ca470 100644--- a/services/migration.js+++ b/services/migration.js@@ -4,7 +4,7 @@ const options = require('./options');const fs = require('fs-extra');const log = require('./log');-const APP_DB_VERSION = 24;+const APP_DB_VERSION = 25;const MIGRATIONS_DIR = "./migrations";async function migrate() {diff --git a/services/sync.js b/services/sync.jsindex 2ad89d2517..496e6b4d5e 100644--- a/services/sync.js+++ b/services/sync.js@@ -9,6 +9,7 @@ const utils = require('./utils');const config = require('./config');const SOURCE_ID = require('./source_id');const audit_category = require('./audit_category');+const eventLog = require('./event_log');const SYNC_SERVER = config['Sync']['syncServerHost'];const isSyncSetup = !!SYNC_SERVER;@@ -16,7 +17,7 @@ const isSyncSetup = !!SYNC_SERVER;let syncInProgress = false;-async function pullSync(syncContext, syncLog) {+async function pullSync(syncContext) {const lastSyncedPull = parseInt(await options.getOption('last_synced_pull'));let syncRows;@@ -34,7 +35,7 @@ async function pullSync(syncContext, syncLog) {logSync("Pulled " + syncRows.length + " changes");}catch (e) {- logSyncError("Can't pull changes, inner exception: ", e, syncLog);+ logSyncError("Can't pull changes, inner exception: ", e);}for (const sync of syncRows) {@@ -48,26 +49,26 @@ async function pullSync(syncContext, syncLog) {});}catch (e) {- logSyncError("Can't pull " + sync.entity_name + " " + sync.entity_id, e, syncLog);+ logSyncError("Can't pull " + sync.entity_name + " " + sync.entity_id, e);}if (sync.entity_name === 'notes') {- await updateNote(resp.entity, resp.links, syncContext.sourceId, syncLog);+ await updateNote(resp.entity, resp.links, syncContext.sourceId);}else if (sync.entity_name === 'notes_tree') {- await updateNoteTree(resp, syncContext.sourceId, syncLog);+ await updateNoteTree(resp, syncContext.sourceId);}else if (sync.entity_name === 'notes_history') {- await updateNoteHistory(resp, syncContext.sourceId, syncLog);+ await updateNoteHistory(resp, syncContext.sourceId);}else if (sync.entity_name === 'notes_reordering') {- await updateNoteReordering(resp, syncContext.sourceId, syncLog);+ await updateNoteReordering(resp, syncContext.sourceId);}else if (sync.entity_name === 'options') {- await updateOptions(resp, syncContext.sourceId, syncLog);+ await updateOptions(resp, syncContext.sourceId);}else {- logSyncError("Unrecognized entity type " + sync.entity_name, e, syncLog);+ logSyncError("Unrecognized entity type " + sync.entity_name, e);}await options.setOption('last_synced_pull', sync.id);@@ -76,7 +77,7 @@ async function pullSync(syncContext, syncLog) {logSync("Finished pull");}-async function sendEntity(entity, entityName, cookieJar, syncLog) {+async function sendEntity(entity, entityName, cookieJar) {try {const payload = {sourceId: SOURCE_ID,@@ -97,11 +98,11 @@ async function sendEntity(entity, entityName, cookieJar, syncLog) {});}catch (e) {- logSyncError("Failed sending update for entity " + entityName, e, syncLog);+ logSyncError("Failed sending update for entity " + entityName, e);}}-async function readAndPushEntity(sync, syncLog, syncContext) {+async function readAndPushEntity(sync, syncContext) {let entity;if (sync.entity_name === 'notes') {@@ -123,15 +124,15 @@ async function readAndPushEntity(sync, syncLog, syncContext) {entity = await sql.getSingleResult('SELECT * FROM options WHERE opt_name = ?', [sync.entity_id]);}else {- logSyncError("Unrecognized entity type " + sync.entity_name, null, syncLog);+ logSyncError("Unrecognized entity type " + sync.entity_name, null);}logSync("Pushing changes in " + sync.entity_name + " " + sync.entity_id);- await sendEntity(entity, sync.entity_name, syncContext.cookieJar, syncLog);+ await sendEntity(entity, sync.entity_name, syncContext.cookieJar);}-async function pushSync(syncContext, syncLog) {+async function pushSync(syncContext) {let lastSyncedPush = parseInt(await options.getOption('last_synced_push'));while (true) {@@ -140,16 +141,16 @@ async function pushSync(syncContext, syncLog) {if (sync === null) {// nothing to sync- logSync("Nothing to push", syncLog);+ logSync("Nothing to push");break;}if (sync.source_id === syncContext.sourceId) {- logSync("Skipping sync " + sync.entity_name + " " + sync.entity_id + " because it originates from sync target", syncLog);+ logSync("Skipping sync " + sync.entity_name + " " + sync.entity_id + " because it originates from sync target");}else {- await readAndPushEntity(sync, syncLog, syncContext);+ await readAndPushEntity(sync, syncContext);}lastSyncedPush = sync.id;@@ -158,7 +159,7 @@ async function pushSync(syncContext, syncLog) {}}-async function login(syncLog) {+async function login() {const timestamp = utils.nowTimestamp();const documentSecret = await options.getOption('document_secret');@@ -186,55 +187,47 @@ async function login(syncLog) {};}catch (e) {- logSyncError("Can't login to API for sync, inner exception: ", e, syncLog);+ logSyncError("Can't login to API for sync, inner exception: ", e);}}async function sync() {- const syncLog = [];-if (syncInProgress) {- syncLog.push("Sync already in progress");+ logSyncError("Sync already in progress");- return syncLog;+ return;}syncInProgress = true;try {if (!await migration.isDbUpToDate()) {- syncLog.push("DB not up to date");+ logSyncError("DB not up to date");- return syncLog;+ return;}- const syncContext = await login(syncLog);+ const syncContext = await login();- await pushSync(syncContext, syncLog);+ await pushSync(syncContext);- await pullSync(syncContext, syncLog);+ await pullSync(syncContext);- await pushSync(syncContext, syncLog);+ await pushSync(syncContext);}catch (e) {- logSync("sync failed: " + e.stack, syncLog);+ logSync("sync failed: " + e.stack);}finally {syncInProgress = false;}-- return syncLog;}-function logSync(message, syncLog) {+function logSync(message) {log.info(message);-- if (syncLog) {- syncLog.push(message);- }}-function logSyncError(message, e, syncLog) {+function logSyncError(message, e) {let completeMessage = message;if (e) {@@ -243,14 +236,10 @@ function logSyncError(message, e, syncLog) {log.info(completeMessage);- if (syncLog) {- syncLog.push(completeMessage);- }-throw new Error(completeMessage);}-async function updateNote(entity, links, sourceId, syncLog) {+async function updateNote(entity, links, sourceId) {const origNote = await sql.getSingleResult("select * from notes where note_id = ?", [entity.note_id]);if (!origNote || origNote.date_modified <= entity.date_modified) {@@ -270,16 +259,18 @@ async function updateNote(entity, links, sourceId, syncLog) {// we don't distinguish between those for nowawait sql.addSyncAudit(audit_category.UPDATE_CONTENT, sourceId, entity.note_id);await sql.addSyncAudit(audit_category.UPDATE_TITLE, sourceId, entity.note_id);++ await eventLog.addNoteEvent(entity.note_id, "Synced note <note>");});- logSync("Update/sync note " + entity.note_id, syncLog);+ logSync("Update/sync note " + entity.note_id);}else {- logSync("Sync conflict in note " + entity.note_id, syncLog);+ await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note <note>");}}-async function updateNoteTree(entity, sourceId, syncLog) {+async function updateNoteTree(entity, sourceId) {const orig = await sql.getSingleResultOrNull("select * from notes_tree where note_id = ?", [entity.note_id]);if (orig === null || orig.date_modified < entity.date_modified) {@@ -291,14 +282,14 @@ async function updateNoteTree(entity, sourceId, syncLog) {await sql.addSyncAudit(audit_category.UPDATE_TITLE, sourceId, entity.note_id);});- logSync("Update/sync note tree " + entity.note_id, syncLog);+ logSync("Update/sync note tree " + entity.note_id);}else {- logSync("Sync conflict in note tree " + entity.note_id, syncLog);+ await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note tree <note>");}}-async function updateNoteHistory(entity, sourceId, syncLog) {+async function updateNoteHistory(entity, sourceId) {const orig = await sql.getSingleResultOrNull("select * from notes_history where note_history_id = ?", [entity.note_history_id]);if (orig === null || orig.date_modified_to < entity.date_modified_to) {@@ -308,14 +299,14 @@ async function updateNoteHistory(entity, sourceId, syncLog) {await sql.addNoteHistorySync(entity.note_history_id, sourceId);});- logSync("Update/sync note history " + entity.note_history_id, syncLog);+ logSync("Update/sync note history " + entity.note_history_id);}else {- logSync("Sync conflict in note history for " + entity.note_id + ", from=" + entity.date_modified_from + ", to=" + entity.date_modified_to, syncLog);+ await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note history for <note>");}}-async function updateNoteReordering(entity, sourceId, syncLog) {+async function updateNoteReordering(entity, sourceId) {await sql.doInTransaction(async () => {Object.keys(entity.ordering).forEach(async key => {await sql.execute("UPDATE notes_tree SET note_pos = ? WHERE note_id = ?", [entity.ordering[key], key]);@@ -326,7 +317,7 @@ async function updateNoteReordering(entity, sourceId, syncLog) {});}-async function updateOptions(entity, sourceId, syncLog) {+async function updateOptions(entity, sourceId) {if (!options.SYNCED_OPTIONS.includes(entity.opt_name)) {return;}@@ -340,10 +331,10 @@ async function updateOptions(entity, sourceId, syncLog) {await sql.addOptionsSync(entity.opt_name, sourceId);});- logSync("Update/sync options " + entity.opt_name, syncLog);+ await eventLog.addEvent("Synced option " + entity.opt_name);}else {- logSync("Sync conflict in options for " + entity.opt_name + ", date_modified=" + entity.date_modified, syncLog);+ await eventLog.addEvent("Sync conflict in options for " + entity.opt_name);}}