2017-12-03 22:29:23 -05:00
"use strict" ;
2024-07-18 21:35:17 +03:00
import sqlInit from "../../services/sql_init.js" ;
import setupService from "../../services/setup.js" ;
import log from "../../services/log.js" ;
import appInfo from "../../services/app_info.js" ;
2025-01-09 18:36:24 +02:00
import type { Request } from "express" ;
2018-09-10 20:05:10 +02:00
2020-06-20 12:31:38 +02:00
function getStatus() {
2018-09-10 20:05:10 +02:00
return {
2020-06-20 12:31:38 +02:00
isInitialized : sqlInit.isDbInitialized ( ) ,
schemaExists : sqlInit.schemaExists ( ) ,
2019-06-11 20:42:06 +02:00
syncVersion : appInfo.syncVersion
2018-09-10 20:05:10 +02:00
} ;
}
2017-12-03 22:29:23 -05:00
2021-12-28 22:59:38 +01:00
async function setupNewDocument() {
await sqlInit . createInitialDatabase ( ) ;
2018-03-30 17:07:41 -04:00
}
2017-12-03 22:29:23 -05:00
2024-04-06 23:08:41 +03:00
function setupSyncFromServer ( req : Request ) {
2021-12-29 23:37:12 +01:00
const { syncServerHost , syncProxy , password } = req . body ;
2018-07-22 19:56:20 +02:00
2021-12-29 23:37:12 +01:00
return setupService . setupSyncFromSyncServer ( syncServerHost , syncProxy , password ) ;
2018-07-22 19:56:20 +02:00
}
2024-04-06 23:08:41 +03:00
function saveSyncSeed ( req : Request ) {
2024-04-03 23:28:26 +03:00
const { options , syncVersion } = req . body ;
2019-06-11 20:42:06 +02:00
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 ) ;
2025-01-09 18:07:02 +02:00
return [
400 ,
{
error : message
}
] ;
2019-06-11 20:42:06 +02:00
}
2018-07-24 20:35:03 +02:00
2020-12-06 22:11:49 +01:00
log . info ( "Saved sync seed." ) ;
2020-06-20 12:31:38 +02:00
sqlInit . createDatabaseForSync ( options ) ;
2018-07-24 20:35:03 +02:00
}
2025-02-13 17:02:03 +01:00
/ * *
* @swagger
* / a p i / s e t u p / s y n c - s e e d :
* get :
* tags :
* - auth
* summary : Sync documentSecret value
* description : First step to logging in .
* operationId : setup - sync - seed
* responses :
* '200' :
* description : Successful operation
* content :
* application / json :
* schema :
* type : object
* properties :
* syncVersion :
* type : integer
* example : 34
* options :
* type : object
* properties :
* documentSecret :
* type : string
* security :
* - user - password : [ ]
* /
2020-06-20 12:31:38 +02:00
function getSyncSeed() {
2018-07-25 09:46:57 +02:00
log . info ( "Serving sync seed." ) ;
2019-06-11 20:42:06 +02:00
return {
2020-06-20 12:31:38 +02:00
options : setupService.getSyncSeedOptions ( ) ,
2019-06-11 20:42:06 +02:00
syncVersion : appInfo.syncVersion
} ;
2018-07-25 09:46:57 +02:00
}
2024-07-18 21:47:30 +03:00
export default {
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
2020-06-20 12:31:38 +02:00
} ;