2018-07-24 08:12:36 +02:00
const syncService = require ( './sync' ) ;
const log = require ( './log' ) ;
2018-07-24 20:35:03 +02:00
const sqlInit = require ( './sql_init' ) ;
2018-07-25 09:46:57 +02:00
const repository = require ( './repository' ) ;
2018-09-10 20:05:10 +02:00
const optionService = require ( './options' ) ;
2018-09-11 09:35:29 +02:00
const syncOptions = require ( './sync_options' ) ;
2018-12-17 22:12:26 +01:00
const request = require ( './request' ) ;
2019-06-05 22:50:03 +02:00
const appInfo = require ( './app_info' ) ;
2018-09-10 20:05:10 +02:00
2018-09-11 10:01:40 +02:00
async function hasSyncServerSchemaAndSeed ( ) {
2018-09-10 20:05:10 +02:00
const response = await requestToSyncServer ( 'GET' , '/api/setup/status' ) ;
2019-06-11 20:42:06 +02:00
if ( response . syncVersion !== appInfo . syncVersion ) {
throw new Error ( ` Could not setup sync since local sync protocol version is ${ appInfo . syncVersion } while remote is ${ response . syncVersion } . To fix this issue, use same Trilium version on all instances. ` ) ;
}
2018-09-11 10:01:40 +02:00
return response . schemaExists ;
2018-09-10 20:05:10 +02:00
}
2018-07-24 08:12:36 +02:00
function triggerSync ( ) {
2018-07-24 20:35:03 +02:00
log . info ( "Triggering sync." ) ;
// it's ok to not wait for it here
syncService . sync ( ) . then ( async res => {
if ( res . success ) {
await sqlInit . dbInitialized ( ) ;
}
2018-07-24 08:12:36 +02:00
} ) ;
}
2018-09-11 10:01:40 +02:00
async function sendSeedToSyncServer ( ) {
2018-09-10 20:05:10 +02:00
log . info ( "Initiating sync to server" ) ;
await requestToSyncServer ( 'POST' , '/api/setup/sync-seed' , {
2019-06-05 22:50:03 +02:00
options : await getSyncSeedOptions ( ) ,
syncVersion : appInfo . syncVersion
2018-09-10 20:05:10 +02:00
} ) ;
// 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 ) ;
}
async function requestToSyncServer ( method , path , body = null ) {
2018-12-17 22:12:26 +01:00
return await request . exec ( {
method ,
url : await syncOptions . getSyncServerHost ( ) + path ,
body ,
proxy : await syncOptions . getSyncProxy ( ) ,
timeout : await syncOptions . getSyncTimeout ( )
} ) ;
2018-09-10 20:05:10 +02:00
}
2018-07-25 08:30:41 +02:00
async function setupSyncFromSyncServer ( syncServerHost , syncProxy , username , password ) {
2018-07-24 08:12:36 +02:00
if ( await sqlInit . isDbInitialized ( ) ) {
return {
result : 'failure' ,
error : 'DB is already initialized.'
} ;
}
try {
log . info ( "Getting document options from sync server." ) ;
// response is expected to contain documentId and documentSecret options
2019-06-11 20:42:06 +02:00
const resp = await request . exec ( {
2018-12-17 22:12:26 +01:00
method : 'get' ,
url : syncServerHost + '/api/setup/sync-seed' ,
2018-07-24 08:12:36 +02:00
auth : {
'user' : username ,
'pass' : password
} ,
2018-12-17 22:12:26 +01:00
proxy : syncProxy
2018-07-24 08:12:36 +02:00
} ) ;
2019-06-11 20:42:06 +02:00
if ( resp . syncVersion !== appInfo . syncVersion ) {
const message = ` Could not setup sync since local sync protocol version is ${ appInfo . syncVersion } while remote is ${ resp . syncVersion } . To fix this issue, use same Trilium version on all instances. ` ;
log . error ( message ) ;
return {
result : 'failure' ,
error : message
}
}
await sqlInit . createDatabaseForSync ( resp . options , syncServerHost , syncProxy ) ;
2018-07-24 08:12:36 +02:00
triggerSync ( ) ;
return { result : 'success' } ;
}
catch ( e ) {
log . error ( "Sync failed: " + e . message ) ;
return {
result : 'failure' ,
error : e . message
} ;
}
}
2018-07-25 09:46:57 +02:00
async function getSyncSeedOptions ( ) {
return [
await repository . getOption ( 'documentId' ) ,
await repository . getOption ( 'documentSecret' )
] ;
}
2018-07-24 08:12:36 +02:00
module . exports = {
2018-09-11 10:01:40 +02:00
hasSyncServerSchemaAndSeed ,
2018-09-10 20:05:10 +02:00
triggerSync ,
2018-09-11 10:01:40 +02:00
sendSeedToSyncServer ,
2018-07-24 08:12:36 +02:00
setupSyncFromSyncServer ,
2018-09-10 20:05:10 +02:00
getSyncSeedOptions
2018-07-24 08:12:36 +02:00
} ;