trilium/services/options.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-11-03 08:48:02 +08:00
const sql = require('./sql');
const utils = require('./utils');
const sync_table = require('./sync_table');
2017-12-04 11:29:23 +08:00
const app_info = require('./app_info');
2017-11-03 08:48:02 +08:00
const SYNCED_OPTIONS = [ 'username', 'password_verification_hash', 'encrypted_data_key', 'protected_session_timeout',
2017-11-03 08:48:02 +08:00
'history_snapshot_time_interval' ];
async function getOption(optName) {
const row = await sql.getSingleResultOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [optName]);
if (!row) {
2017-12-07 09:58:59 +08:00
throw new Error("Option " + optName + " doesn't exist");
2017-11-03 08:48:02 +08:00
}
return row['opt_value'];
}
async function setOption(optName, optValue) {
2017-11-03 08:48:02 +08:00
if (SYNCED_OPTIONS.includes(optName)) {
await sync_table.addOptionsSync(optName);
2017-11-03 08:48:02 +08:00
}
2017-12-04 11:29:23 +08:00
await sql.replace("options", {
opt_name: optName,
opt_value: optValue,
date_modified: utils.nowDate()
2017-12-04 11:29:23 +08:00
});
2017-11-03 08:48:02 +08:00
}
async function initOptions(startNotePath) {
2017-12-04 11:29:23 +08:00
await setOption('document_id', utils.randomSecureToken(16));
await setOption('document_secret', utils.randomSecureToken(16));
await setOption('username', '');
await setOption('password_verification_hash', '');
await setOption('password_verification_salt', '');
await setOption('password_derived_key_salt', '');
await setOption('encrypted_data_key', '');
await setOption('encrypted_data_key_iv', '');
await setOption('start_note_path', startNotePath);
2017-12-04 11:29:23 +08:00
await setOption('protected_session_timeout', 600);
await setOption('history_snapshot_time_interval', 600);
await setOption('last_backup_date', utils.nowDate());
2017-12-04 11:29:23 +08:00
await setOption('db_version', app_info.db_version);
await setOption('last_synced_pull', app_info.db_version);
await setOption('last_synced_push', 0);
await setOption('last_synced_push', 0);
}
2017-11-03 08:48:02 +08:00
module.exports = {
getOption,
setOption,
2017-12-04 11:29:23 +08:00
initOptions,
2017-11-03 08:48:02 +08:00
SYNCED_OPTIONS
};