mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	merged "Sync local document" into "Test sync"
This commit is contained in:
		
							parent
							
								
									b4a566df9e
								
							
						
					
					
						commit
						e4381d10e8
					
				@ -223,7 +223,6 @@ addTabHandler((function() {
 | 
				
			|||||||
    const $syncServerTimeout = $("#sync-server-timeout");
 | 
					    const $syncServerTimeout = $("#sync-server-timeout");
 | 
				
			||||||
    const $syncProxy = $("#sync-proxy");
 | 
					    const $syncProxy = $("#sync-proxy");
 | 
				
			||||||
    const $testSyncButton = $("#test-sync-button");
 | 
					    const $testSyncButton = $("#test-sync-button");
 | 
				
			||||||
    const $syncToServerButton = $("#sync-to-server-button");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function optionsLoaded(options) {
 | 
					    function optionsLoaded(options) {
 | 
				
			||||||
        $syncServerHost.val(options['syncServerHost']);
 | 
					        $syncServerHost.val(options['syncServerHost']);
 | 
				
			||||||
@ -244,25 +243,14 @@ addTabHandler((function() {
 | 
				
			|||||||
    $testSyncButton.click(async () => {
 | 
					    $testSyncButton.click(async () => {
 | 
				
			||||||
        const result = await server.post('sync/test');
 | 
					        const result = await server.post('sync/test');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (result.connection === "Success") {
 | 
					        if (result.success) {
 | 
				
			||||||
            infoService.showMessage("Sync server handshake has been successful");
 | 
					            infoService.showMessage(result.message);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else {
 | 
					        else {
 | 
				
			||||||
            infoService.showError("Sync server handshake failed, error: " + result.error);
 | 
					            infoService.showError("Sync server handshake failed, error: " + result.error);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $syncToServerButton.click(async () => {
 | 
					 | 
				
			||||||
        const resp = await server.post("setup/sync-to-server");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (resp.success) {
 | 
					 | 
				
			||||||
            infoService.showMessage("Sync has been established to the server instance. It will take some time to finish.");
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        else {
 | 
					 | 
				
			||||||
            infoService.showError('Sync setup failed: ' + resp.error);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
        optionsLoaded
 | 
					        optionsLoaded
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
				
			|||||||
@ -38,7 +38,7 @@ async function loginSync(req) {
 | 
				
			|||||||
    const givenHash = req.body.hash;
 | 
					    const givenHash = req.body.hash;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (expectedHash !== givenHash) {
 | 
					    if (expectedHash !== givenHash) {
 | 
				
			||||||
        return [400, { message: "Sync login hash doesn't match" }];
 | 
					        return [400, { message: "Sync login credentials are incorrect." }];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    req.session.loggedIn = true;
 | 
					    req.session.loggedIn = true;
 | 
				
			||||||
 | 
				
			|||||||
@ -2,10 +2,13 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const sqlInit = require('../../services/sql_init');
 | 
					const sqlInit = require('../../services/sql_init');
 | 
				
			||||||
const setupService = require('../../services/setup');
 | 
					const setupService = require('../../services/setup');
 | 
				
			||||||
const optionService = require('../../services/options');
 | 
					 | 
				
			||||||
const syncService = require('../../services/sync');
 | 
					 | 
				
			||||||
const log = require('../../services/log');
 | 
					const log = require('../../services/log');
 | 
				
			||||||
const rp = require('request-promise');
 | 
					
 | 
				
			||||||
 | 
					async function getStatus() {
 | 
				
			||||||
 | 
					    return {
 | 
				
			||||||
 | 
					        isInitialized: await sqlInit.isDbInitialized()
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function setupNewDocument(req) {
 | 
					async function setupNewDocument(req) {
 | 
				
			||||||
    const { username, password } = req.body;
 | 
					    const { username, password } = req.body;
 | 
				
			||||||
@ -19,42 +22,6 @@ async function setupSyncFromServer(req) {
 | 
				
			|||||||
    return await setupService.setupSyncFromSyncServer(syncServerHost, syncProxy, username, password);
 | 
					    return await setupService.setupSyncFromSyncServer(syncServerHost, syncProxy, username, password);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    try {
 | 
					 | 
				
			||||||
        await rp(rpOpts);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    catch (e) {
 | 
					 | 
				
			||||||
        return { success: false, error: e.message };
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // 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();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return { success: true };
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function saveSyncSeed(req) {
 | 
					async function saveSyncSeed(req) {
 | 
				
			||||||
    const options = req.body.options;
 | 
					    const options = req.body.options;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -68,9 +35,9 @@ async function getSyncSeed() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
 | 
					    getStatus,
 | 
				
			||||||
    setupNewDocument,
 | 
					    setupNewDocument,
 | 
				
			||||||
    setupSyncFromServer,
 | 
					    setupSyncFromServer,
 | 
				
			||||||
    setupSyncToSyncServer,
 | 
					 | 
				
			||||||
    getSyncSeed,
 | 
					    getSyncSeed,
 | 
				
			||||||
    saveSyncSeed
 | 
					    saveSyncSeed
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@ -1,6 +1,7 @@
 | 
				
			|||||||
"use strict";
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const syncService = require('../../services/sync');
 | 
					const syncService = require('../../services/sync');
 | 
				
			||||||
 | 
					const setupService = require('../../services/setup');
 | 
				
			||||||
const syncUpdateService = require('../../services/sync_update');
 | 
					const syncUpdateService = require('../../services/sync_update');
 | 
				
			||||||
const syncTableService = require('../../services/sync_table');
 | 
					const syncTableService = require('../../services/sync_table');
 | 
				
			||||||
const sql = require('../../services/sql');
 | 
					const sql = require('../../services/sql');
 | 
				
			||||||
@ -11,13 +12,20 @@ const log = require('../../services/log');
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
async function testSync() {
 | 
					async function testSync() {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
        await syncService.login();
 | 
					        if (await setupService.isSyncServerInitialized()) {
 | 
				
			||||||
 | 
					            await syncService.login();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return { connection: "Success" };
 | 
					            return { success: true, message: "Sync server handshake has been successful" };
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else {
 | 
				
			||||||
 | 
					            await setupService.setupSyncToSyncServer();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return { success: true, message: "Sync has been established to the server instance. It will take some time to finish." };
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    catch (e) {
 | 
					    catch (e) {
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            connection: "Failure",
 | 
					            success: false,
 | 
				
			||||||
            error: e.message
 | 
					            error: e.message
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -166,9 +166,9 @@ function register(app) {
 | 
				
			|||||||
    apiRoute(PUT, '/api/recent-notes/:branchId/:notePath', recentNotesRoute.addRecentNote);
 | 
					    apiRoute(PUT, '/api/recent-notes/:branchId/:notePath', recentNotesRoute.addRecentNote);
 | 
				
			||||||
    apiRoute(GET, '/api/app-info', appInfoRoute.getAppInfo);
 | 
					    apiRoute(GET, '/api/app-info', appInfoRoute.getAppInfo);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    route(GET, '/api/setup/status', [], setupApiRoute.getStatus, apiResultHandler);
 | 
				
			||||||
    route(POST, '/api/setup/new-document', [auth.checkAppNotInitialized], setupApiRoute.setupNewDocument, apiResultHandler);
 | 
					    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-server', [auth.checkAppNotInitialized], setupApiRoute.setupSyncFromServer, apiResultHandler, false);
 | 
				
			||||||
    apiRoute(POST, '/api/setup/sync-to-server', setupApiRoute.setupSyncToSyncServer);
 | 
					 | 
				
			||||||
    route(GET, '/api/setup/sync-seed', [auth.checkBasicAuth], setupApiRoute.getSyncSeed, apiResultHandler);
 | 
					    route(GET, '/api/setup/sync-seed', [auth.checkBasicAuth], setupApiRoute.getSyncSeed, apiResultHandler);
 | 
				
			||||||
    route(POST, '/api/setup/sync-seed', [auth.checkAppNotInitialized], setupApiRoute.saveSyncSeed, apiResultHandler, false);
 | 
					    route(POST, '/api/setup/sync-seed', [auth.checkAppNotInitialized], setupApiRoute.saveSyncSeed, apiResultHandler, false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -3,6 +3,13 @@ const syncService = require('./sync');
 | 
				
			|||||||
const log = require('./log');
 | 
					const log = require('./log');
 | 
				
			||||||
const sqlInit = require('./sql_init');
 | 
					const sqlInit = require('./sql_init');
 | 
				
			||||||
const repository = require('./repository');
 | 
					const repository = require('./repository');
 | 
				
			||||||
 | 
					const optionService = require('./options');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function isSyncServerInitialized() {
 | 
				
			||||||
 | 
					    const response = await requestToSyncServer('GET', '/api/setup/status');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return response.isInitialized;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function triggerSync() {
 | 
					function triggerSync() {
 | 
				
			||||||
    log.info("Triggering sync.");
 | 
					    log.info("Triggering sync.");
 | 
				
			||||||
@ -15,6 +22,42 @@ function triggerSync() {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					async function setupSyncToSyncServer() {
 | 
				
			||||||
 | 
					    log.info("Initiating sync to server");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await requestToSyncServer('POST', '/api/setup/sync-seed', {
 | 
				
			||||||
 | 
					        options: await getSyncSeedOptions()
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 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 requestToSyncServer(method, path, body = null) {
 | 
				
			||||||
 | 
					    const syncServerHost = await optionService.getOption('syncServerHost');
 | 
				
			||||||
 | 
					    const syncProxy = await optionService.getOption('syncProxy');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const rpOpts = {
 | 
				
			||||||
 | 
					        uri: syncServerHost + path,
 | 
				
			||||||
 | 
					        method: method,
 | 
				
			||||||
 | 
					        json: true
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (body) {
 | 
				
			||||||
 | 
					        rpOpts.body = body;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (syncProxy) {
 | 
				
			||||||
 | 
					        rpOpts.proxy = syncProxy;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return await rp(rpOpts);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, password) {
 | 
					async function setupSyncFromSyncServer(syncServerHost, syncProxy, username, password) {
 | 
				
			||||||
    if (await sqlInit.isDbInitialized()) {
 | 
					    if (await sqlInit.isDbInitialized()) {
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
@ -64,7 +107,9 @@ async function getSyncSeedOptions() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
 | 
					    isSyncServerInitialized,
 | 
				
			||||||
 | 
					    triggerSync,
 | 
				
			||||||
 | 
					    setupSyncToSyncServer,
 | 
				
			||||||
    setupSyncFromSyncServer,
 | 
					    setupSyncFromSyncServer,
 | 
				
			||||||
    getSyncSeedOptions,
 | 
					    getSyncSeedOptions
 | 
				
			||||||
    triggerSync
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@ -483,15 +483,9 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            <h4>Sync test</h4>
 | 
					            <h4>Sync test</h4>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <p>This will test connection and handshake to the sync server.</p>
 | 
					            <p>This will test connection and handshake to the sync server. If sync server isn't initialized, this will set it up to sync with local document.</p>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            <button id="test-sync-button" class="btn btn-sm">Test sync</button>
 | 
					            <button id="test-sync-button" class="btn btn-sm">Test sync</button>
 | 
				
			||||||
 | 
					 | 
				
			||||||
            <h4>Sync document to the server instance</h4>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            <p>This is used when you want to sync your local document to the server instance configured above. This is a one time action after which the documents are synced automatically and transparently.</p>
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            <button id="sync-to-server-button" class="btn btn-sm">Sync local document to the server instance</button>
 | 
					 | 
				
			||||||
          </div>
 | 
					          </div>
 | 
				
			||||||
          <div id="advanced">
 | 
					          <div id="advanced">
 | 
				
			||||||
            <h4 style="margin-top: 0px;">Sync</h4>
 | 
					            <h4 style="margin-top: 0px;">Sync</h4>
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user