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
#98 some sync setup refactorings
azivnera4627f2ddb80
diff --git a/src/public/javascripts/dialogs/options.js b/src/public/javascripts/dialogs/options.jsindex 312557a7b1..29abfd2b63 100644--- a/src/public/javascripts/dialogs/options.js+++ b/src/public/javascripts/dialogs/options.js@@ -223,7 +223,7 @@ addTabHandler((function() {});$syncToServerButton.click(async () => {- await server.post("sync/sync-to-server");+ await server.post("setup/sync-to-server");infoService.showMessage("Sync has been established to the server instance. It will take some time to finish.");});diff --git a/src/routes/api/setup.js b/src/routes/api/setup.jsindex 54777ef3ef..e62a1afa85 100644--- a/src/routes/api/setup.js+++ b/src/routes/api/setup.js@@ -2,6 +2,10 @@const sqlInit = require('../../services/sql_init');const setupService = require('../../services/setup');+const optionService = require('../../services/options');+const syncService = require('../../services/sync');+const log = require('../../services/log');+const rp = require('request-promise');async function setupNewDocument(req) {const { username, password } = req.body;@@ -15,14 +19,51 @@ async function setupSyncFromServer(req) {return await setupService.setupSyncFromSyncServer(syncServerHost, syncProxy, username, password);}-async function setupSyncFromClient(req) {+async function setupSyncToSyncServer() {+ log.info("Initiating sync to server");++ const syncServerHost = await optionService.getOption('syncServerHost');+ const syncProxy = await optionService.getOption('syncProxy');++ const rpOpts = {+ uri: syncServerHost + '/api/setup/sync-seed',+ method: 'POST',+ json: true,+ body: {+ options: await setupService.getSyncSeedOptions()+ }+ };++ if (syncProxy) {+ rpOpts.proxy = syncProxy;+ }++ await rp(rpOpts);++ // this is completely new sync, need to reset counters. If this would not be new sync,+ // the previous request would have failed.+ await optionService.setOption('lastSyncedPush', 0);+ await optionService.setOption('lastSyncedPull', 0);++ syncService.sync();+}++async function saveSyncSeed(req) {const options = req.body.options;await sqlInit.createDatabaseForSync(options);}+async function getSyncSeed() {+ log.info("Serving sync seed.");++ return await setupService.getSyncSeedOptions();+}+module.exports = {setupNewDocument,setupSyncFromServer,- setupSyncFromClient+ setupSyncToSyncServer,+ getSyncSeed,+ saveSyncSeed};\ No newline at end of filediff --git a/src/routes/api/sync.js b/src/routes/api/sync.jsindex 3e18c22dd2..6108845435 100644--- a/src/routes/api/sync.js+++ b/src/routes/api/sync.js@@ -8,8 +8,6 @@ const sqlInit = require('../../services/sql_init');const optionService = require('../../services/options');const contentHashService = require('../../services/content_hash');const log = require('../../services/log');-const repository = require('../../services/repository');-const rp = require('request-promise');async function testSync() {try {@@ -26,6 +24,11 @@ async function testSync() {}async function getStats() {+ if (!await sqlInit.schemaExists()) {+ // fail silently but prevent errors from not existing options table+ return {};+ }+return {initialized: await optionService.getOption('initialized') === 'true',stats: syncService.stats@@ -99,48 +102,6 @@ async function update(req) {}}-async function getDocumentOptions() {- return [- await repository.getOption('documentId'),- await repository.getOption('documentSecret')- ];-}--async function getDocument() {- log.info("Serving document options.");-- return await getDocumentOptions();-}--async function syncToServer() {- log.info("Initiating sync to server");-- const syncServerHost = await optionService.getOption('syncServerHost');- const syncProxy = await optionService.getOption('syncProxy');-- const rpOpts = {- uri: syncServerHost + '/api/setup/sync-from-client',- method: 'POST',- json: true,- body: {- options: await getDocumentOptions()- }- };-- if (syncProxy) {- rpOpts.proxy = syncProxy;- }-- await rp(rpOpts);-- // this is completely new sync, need to reset counters. If this would not be new sync,- // the previous request would have failed.- await optionService.setOption('lastSyncedPush', 0);- await optionService.setOption('lastSyncedPull', 0);-- syncService.sync();-}-async function syncFinished() {// after first sync finishes, the application is ready to be used// this is meaningless but at the same time harmless (idempotent) for further syncs@@ -156,8 +117,6 @@ module.exports = {forceNoteSync,getChanged,update,- getDocument,getStats,- syncToServer,syncFinished};\ No newline at end of filediff --git a/src/routes/routes.js b/src/routes/routes.jsindex fc150c16f2..92f7308fce 100644--- a/src/routes/routes.js+++ b/src/routes/routes.js@@ -156,9 +156,7 @@ function register(app) {apiRoute(POST, '/api/sync/force-note-sync/:noteId', syncApiRoute.forceNoteSync);apiRoute(GET, '/api/sync/changed', syncApiRoute.getChanged);apiRoute(PUT, '/api/sync/update', syncApiRoute.update);- route(GET, '/api/sync/document', [auth.checkBasicAuth], syncApiRoute.getDocument, apiResultHandler);route(GET, '/api/sync/stats', [], syncApiRoute.getStats, apiResultHandler);- apiRoute(POST, '/api/sync/sync-to-server', syncApiRoute.syncToServer);apiRoute(POST, '/api/sync/finished', syncApiRoute.syncFinished);apiRoute(GET, '/api/event-log', eventLogRoute.getEventLog);@@ -169,7 +167,9 @@ function register(app) {route(POST, '/api/setup/new-document', [auth.checkAppNotInitialized], setupApiRoute.setupNewDocument, apiResultHandler);route(POST, '/api/setup/sync-from-server', [auth.checkAppNotInitialized], setupApiRoute.setupSyncFromServer, apiResultHandler, false);- route(POST, '/api/setup/sync-from-client', [auth.checkAppNotInitialized], setupApiRoute.setupSyncFromClient, apiResultHandler, false);+ apiRoute(POST, '/api/setup/sync-to-server', setupApiRoute.setupSyncToSyncServer);+ route(GET, '/api/setup/sync-seed', [auth.checkBasicAuth], setupApiRoute.getSyncSeed, apiResultHandler);+ route(POST, '/api/setup/sync-seed', [auth.checkAppNotInitialized], setupApiRoute.saveSyncSeed, apiResultHandler, false);apiRoute(POST, '/api/sql/execute', sqlRoute.execute);apiRoute(POST, '/api/anonymization/anonymize', anonymizationRoute.anonymize);diff --git a/src/services/setup.js b/src/services/setup.jsindex f4de0b61f3..aa8226d159 100644--- a/src/services/setup.js+++ b/src/services/setup.js@@ -2,6 +2,7 @@ const rp = require('request-promise');const syncService = require('./sync');const log = require('./log');const sqlInit = require('./sql_init');+const repository = require('./repository');function triggerSync() {log.info("Triggering sync.");@@ -27,7 +28,7 @@ async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, pass// response is expected to contain documentId and documentSecret optionsconst options = await rp.get({- uri: syncServerHost + '/api/sync/document',+ uri: syncServerHost + '/api/setup/sync-seed',auth: {'user': username,'pass': password@@ -55,7 +56,15 @@ async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, pass}}+async function getSyncSeedOptions() {+ return [+ await repository.getOption('documentId'),+ await repository.getOption('documentSecret')+ ];+}+module.exports = {setupSyncFromSyncServer,+ getSyncSeedOptions,triggerSync};\ No newline at end of filediff --git a/src/services/sync.js b/src/services/sync.jsindex 00dab2f055..c4331b5a20 100644--- a/src/services/sync.js+++ b/src/services/sync.js@@ -11,7 +11,7 @@ const dateUtils = require('./date_utils');const syncUpdateService = require('./sync_update');const contentHashService = require('./content_hash');const appInfo = require('./app_info');-const syncSetup = require('./sync_setup');+const syncOptions = require('./sync_options');const syncMutexService = require('./sync_mutex');const cls = require('./cls');@@ -25,7 +25,7 @@ const stats = {async function sync() {try {return await syncMutexService.doExclusively(async () => {- if (!await syncSetup.isSyncSetup()) {+ if (!await syncOptions.isSyncSetup()) {return { success: false, message: 'Sync not configured' };}@@ -196,7 +196,7 @@ async function checkContentHash(syncContext) {}async function syncRequest(syncContext, method, uri, body) {- const fullUri = await syncSetup.getSyncServer() + uri;+ const fullUri = await syncOptions.getSyncServerHost() + uri;try {const options = {@@ -205,10 +205,10 @@ async function syncRequest(syncContext, method, uri, body) {jar: syncContext.cookieJar,json: true,body: body,- timeout: await syncSetup.getSyncTimeout()+ timeout: await syncOptions.getSyncTimeout()};- const syncProxy = await syncSetup.getSyncProxy();+ const syncProxy = await syncOptions.getSyncProxy();if (syncProxy && proxyToggle) {options.proxy = syncProxy;@@ -302,10 +302,10 @@ async function updatePushStats() {}sqlInit.dbReady.then(async () => {- if (await syncSetup.isSyncSetup()) {- log.info("Setting up sync to " + await syncSetup.getSyncServer() + " with timeout " + await syncSetup.getSyncTimeout());+ if (await syncOptions.isSyncSetup()) {+ log.info("Setting up sync to " + await syncOptions.getSyncServerHost() + " with timeout " + await syncOptions.getSyncTimeout());- const syncProxy = await syncSetup.getSyncProxy();+ const syncProxy = await syncOptions.getSyncProxy();if (syncProxy) {log.info("Sync proxy: " + syncProxy);diff --git a/src/services/sync_setup.js b/src/services/sync_options.jssimilarity index 78%rename from src/services/sync_setup.jsrename to src/services/sync_options.jsindex a2dfc8099a..79c2f40501 100644--- a/src/services/sync_setup.js+++ b/src/services/sync_options.js@@ -3,7 +3,7 @@const optionService = require('./options');module.exports = {- getSyncServer: async () => await optionService.getOption('syncServerHost'),+ getSyncServerHost: async () => await optionService.getOption('syncServerHost'),isSyncSetup: async () => !!await optionService.getOption('syncServerHost'),getSyncTimeout: async () => await optionService.getOption('syncServerTimeout'),getSyncProxy: async () => await optionService.getOption('syncProxy')diff --git a/src/services/sync_table.js b/src/services/sync_table.jsindex c6ce2b72fe..e4c2a6a13c 100644--- a/src/services/sync_table.js+++ b/src/services/sync_table.js@@ -1,7 +1,7 @@const sql = require('./sql');const sourceIdService = require('./source_id');const dateUtils = require('./date_utils');-const syncSetup = require('./sync_setup');+const syncOptions = require('./sync_options');const log = require('./log');const cls = require('./cls');const eventService = require('./events');@@ -54,7 +54,7 @@ async function addEntitySync(entityName, entityId, sourceId) {sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId()});- if (!await syncSetup.isSyncSetup()) {+ if (!await syncOptions.isSyncSetup()) {// this is because the "server" instances shouldn't have outstanding pushes// useful when you fork the DB for new "client" instance, it won't try to sync the whole DBawait sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE name IN('lastSyncedPush', 'lastSyncedPull')");