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-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) {
|
|
|
|
throw new Error("Option " + optName + " doesn't exist");
|
|
|
|
}
|
|
|
|
|
|
|
|
return row['opt_value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setOptionInTransaction(optName, optValue) {
|
|
|
|
await sql.doInTransaction(async () => setOption(optName, optValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setOption(optName, optValue) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
await setOptionNoSync(optName, optValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setOptionNoSync(optName, optValue) {
|
|
|
|
const now = utils.nowTimestamp();
|
|
|
|
|
|
|
|
await sql.execute("UPDATE options SET opt_value = ?, date_modified = ? WHERE opt_name = ?", [optValue, now, optName]);
|
|
|
|
}
|
|
|
|
|
2017-11-17 10:50:00 +08:00
|
|
|
sql.dbReady.then(async () => {
|
|
|
|
if (!await getOption('document_id')) {
|
|
|
|
await setOption('document_id', utils.randomSecureToken(16));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await getOption('document_secret')) {
|
|
|
|
await setOption('document_secret', utils.randomSecureToken(16));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-11-03 08:48:02 +08:00
|
|
|
module.exports = {
|
|
|
|
getOption,
|
|
|
|
setOption,
|
|
|
|
setOptionNoSync,
|
|
|
|
setOptionInTransaction,
|
|
|
|
SYNCED_OPTIONS
|
|
|
|
};
|