2017-12-03 22:29:23 -05:00
"use strict" ;
2018-04-02 21:25:20 -04:00
const sqlInit = require ( '../../services/sql_init' ) ;
2018-07-24 08:12:36 +02:00
const setupService = require ( '../../services/setup' ) ;
2018-07-25 09:46:57 +02:00
const log = require ( '../../services/log' ) ;
2019-06-11 20:42:06 +02:00
const appInfo = require ( '../../services/app_info' ) ;
2018-09-10 20:05:10 +02:00
async function getStatus ( ) {
return {
2018-09-11 10:01:40 +02:00
isInitialized : await sqlInit . isDbInitialized ( ) ,
2019-06-11 20:42:06 +02:00
schemaExists : await sqlInit . schemaExists ( ) ,
syncVersion : appInfo . syncVersion
2018-09-10 20:05:10 +02:00
} ;
}
2017-12-03 22:29:23 -05:00
2018-07-22 19:56:20 +02:00
async function setupNewDocument ( req ) {
2019-08-11 10:28:49 +02:00
const { username , password , theme } = req . body ;
2017-12-03 22:29:23 -05:00
2019-08-11 10:28:49 +02:00
await sqlInit . createInitialDatabase ( username , password , theme ) ;
2018-03-30 17:07:41 -04:00
}
2017-12-03 22:29:23 -05:00
2018-07-22 19:56:20 +02:00
async function setupSyncFromServer ( req ) {
2018-07-25 08:30:41 +02:00
const { syncServerHost , syncProxy , username , password } = req . body ;
2018-07-22 19:56:20 +02:00
2018-07-25 08:30:41 +02:00
return await setupService . setupSyncFromSyncServer ( syncServerHost , syncProxy , username , password ) ;
2018-07-22 19:56:20 +02:00
}
2018-07-25 09:46:57 +02:00
async function saveSyncSeed ( req ) {
2019-06-11 20:42:06 +02: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-24 20:35:03 +02:00
await sqlInit . createDatabaseForSync ( options ) ;
}
2018-07-25 09:46:57 +02:00
async function getSyncSeed ( ) {
log . info ( "Serving sync seed." ) ;
2019-06-11 20:42:06 +02:00
return {
options : await setupService . getSyncSeedOptions ( ) ,
syncVersion : appInfo . syncVersion
} ;
2018-07-25 09:46:57 +02:00
}
2018-03-30 17:07:41 -04:00
module . exports = {
2018-09-10 20:05:10 +02:00
getStatus ,
2018-07-22 19:56:20 +02:00
setupNewDocument ,
2018-07-24 20:35:03 +02:00
setupSyncFromServer ,
2018-07-25 09:46:57 +02:00
getSyncSeed ,
saveSyncSeed
2018-03-30 17:07:41 -04:00
} ;