2017-12-04 11:29:23 +08:00
"use strict" ;
2018-04-03 09:25:20 +08:00
const sqlInit = require ( '../../services/sql_init' ) ;
2018-07-24 14:12:36 +08:00
const setupService = require ( '../../services/setup' ) ;
2018-07-25 15:46:57 +08:00
const log = require ( '../../services/log' ) ;
2019-06-12 02:42:06 +08:00
const appInfo = require ( '../../services/app_info' ) ;
2018-09-11 02:05:10 +08:00
2020-06-20 18:31:38 +08:00
function getStatus ( ) {
2018-09-11 02:05:10 +08:00
return {
2020-06-20 18:31:38 +08:00
isInitialized : sqlInit . isDbInitialized ( ) ,
schemaExists : sqlInit . schemaExists ( ) ,
2019-06-12 02:42:06 +08:00
syncVersion : appInfo . syncVersion
2018-09-11 02:05:10 +08:00
} ;
}
2017-12-04 11:29:23 +08:00
2020-06-20 18:31:38 +08:00
function setupNewDocument ( req ) {
2019-08-11 16:28:49 +08:00
const { username , password , theme } = req . body ;
2017-12-04 11:29:23 +08:00
2020-06-20 18:31:38 +08:00
sqlInit . createInitialDatabase ( username , password , theme ) ;
2018-03-31 05:07:41 +08:00
}
2017-12-04 11:29:23 +08:00
2020-06-20 18:31:38 +08:00
function setupSyncFromServer ( req ) {
2018-07-25 14:30:41 +08:00
const { syncServerHost , syncProxy , username , password } = req . body ;
2018-07-23 01:56:20 +08:00
2020-06-20 18:31:38 +08:00
return setupService . setupSyncFromSyncServer ( syncServerHost , syncProxy , username , password ) ;
2018-07-23 01:56:20 +08:00
}
2020-06-20 18:31:38 +08:00
function saveSyncSeed ( req ) {
2019-06-12 02:42:06 +08:00
const { options , syncVersion } = req . body ;
if ( appInfo . syncVersion !== syncVersion ) {
const message = ` Could not setup sync since local sync protocol version is ${ appInfo . syncVersion } while remote is ${ syncVersion } . To fix this issue, use same Trilium version on all instances. ` ;
log . error ( message ) ;
return [ 400 , {
error : message
} ]
}
2018-07-25 02:35:03 +08:00
2020-06-20 18:31:38 +08:00
sqlInit . createDatabaseForSync ( options ) ;
2018-07-25 02:35:03 +08:00
}
2020-06-20 18:31:38 +08:00
function getSyncSeed ( ) {
2018-07-25 15:46:57 +08:00
log . info ( "Serving sync seed." ) ;
2019-06-12 02:42:06 +08:00
return {
2020-06-20 18:31:38 +08:00
options : setupService . getSyncSeedOptions ( ) ,
2019-06-12 02:42:06 +08:00
syncVersion : appInfo . syncVersion
} ;
2018-07-25 15:46:57 +08:00
}
2018-03-31 05:07:41 +08:00
module . exports = {
2018-09-11 02:05:10 +08:00
getStatus ,
2018-07-23 01:56:20 +08:00
setupNewDocument ,
2018-07-25 02:35:03 +08:00
setupSyncFromServer ,
2018-07-25 15:46:57 +08:00
getSyncSeed ,
saveSyncSeed
2020-06-20 18:31:38 +08:00
} ;