2017-11-03 08:48:02 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const utils = require('./utils');
|
2017-11-17 10:50:00 +08:00
|
|
|
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
|
|
|
|
2017-11-15 11:44:45 +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'];
|
|
|
|
}
|
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
async function setOption(optName, optValue) {
|
2017-11-03 08:48:02 +08:00
|
|
|
if (SYNCED_OPTIONS.includes(optName)) {
|
2017-11-17 10:50:00 +08:00
|
|
|
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,
|
2017-12-11 01:56:59 +08:00
|
|
|
date_modified: utils.nowDate()
|
2017-12-04 11:29:23 +08:00
|
|
|
});
|
2017-11-03 08:48:02 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 12:15:53 +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', '');
|
|
|
|
|
2017-12-07 12:15:53 +08:00
|
|
|
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);
|
2017-12-11 01:56:59 +08:00
|
|
|
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-17 10:50:00 +08:00
|
|
|
|
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
|
|
|
|
};
|