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
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
async function getOptionOrNull(name) {
|
2018-01-29 09:52:05 +08:00
|
|
|
try {
|
2018-01-30 06:41:59 +08:00
|
|
|
return await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
|
2018-01-29 09:52:05 +08:00
|
|
|
}
|
|
|
|
catch (e) {
|
2018-01-30 06:41:59 +08:00
|
|
|
return await sql.getRowOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [name]);
|
2018-01-29 09:52:05 +08:00
|
|
|
}
|
2018-01-12 12:54:17 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
async function getOption(name) {
|
|
|
|
const row = await getOptionOrNull(name);
|
2017-11-03 08:48:02 +08:00
|
|
|
|
|
|
|
if (!row) {
|
2018-01-29 08:30:14 +08:00
|
|
|
throw new Error("Option " + name + " doesn't exist");
|
2017-11-03 08:48:02 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 09:52:05 +08:00
|
|
|
return row['value'] ? row['value'] : row['opt_value'];
|
2017-11-03 08:48:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
async function setOption(name, value) {
|
2018-03-31 01:56:46 +08:00
|
|
|
const opt = await sql.getRow("SELECT * FROM options WHERE name = ?", [name]);
|
2018-01-12 11:45:25 +08:00
|
|
|
|
|
|
|
if (!opt) {
|
2018-01-29 08:30:14 +08:00
|
|
|
throw new Error(`Option ${name} doesn't exist`);
|
2018-01-12 11:45:25 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (opt.isSynced) {
|
2018-03-31 07:41:54 +08:00
|
|
|
await sync_table.addOptionsSync(name);
|
2017-11-03 08:48:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 01:56:46 +08:00
|
|
|
await sql.execute("UPDATE options SET value = ?, dateModified = ? WHERE name = ?",
|
|
|
|
[value, utils.nowDate(), name]);
|
2018-01-12 11:45:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
async function createOption(name, value, isSynced) {
|
2018-01-12 11:45:25 +08:00
|
|
|
await sql.insert("options", {
|
2018-01-29 08:30:14 +08:00
|
|
|
name: name,
|
|
|
|
value: value,
|
|
|
|
isSynced: isSynced,
|
|
|
|
dateModified: utils.nowDate()
|
2017-12-04 11:29:23 +08:00
|
|
|
});
|
2018-01-12 11:45:25 +08:00
|
|
|
|
|
|
|
if (isSynced) {
|
2018-03-31 07:41:54 +08:00
|
|
|
await sync_table.addOptionsSync(name);
|
2018-01-12 11:45:25 +08:00
|
|
|
}
|
2017-11-03 08:48:02 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 12:15:53 +08:00
|
|
|
async function initOptions(startNotePath) {
|
2018-01-12 11:45:25 +08:00
|
|
|
await createOption('document_id', utils.randomSecureToken(16), false);
|
|
|
|
await createOption('document_secret', utils.randomSecureToken(16), false);
|
|
|
|
|
|
|
|
await createOption('username', '', true);
|
|
|
|
await createOption('password_verification_hash', '', true);
|
|
|
|
await createOption('password_verification_salt', '', true);
|
|
|
|
await createOption('password_derived_key_salt', '', true);
|
|
|
|
await createOption('encrypted_data_key', '', true);
|
|
|
|
await createOption('encrypted_data_key_iv', '', true);
|
|
|
|
|
2018-01-29 09:52:05 +08:00
|
|
|
await createOption('start_note_path', startNotePath, false);
|
2018-01-12 11:45:25 +08:00
|
|
|
await createOption('protected_session_timeout', 600, true);
|
2018-03-26 08:52:38 +08:00
|
|
|
await createOption('note_revision_snapshot_time_interval', 600, true);
|
2018-01-12 11:45:25 +08:00
|
|
|
await createOption('last_backup_date', utils.nowDate(), false);
|
|
|
|
await createOption('db_version', app_info.db_version, false);
|
|
|
|
|
|
|
|
await createOption('last_synced_pull', app_info.db_version, false);
|
|
|
|
await createOption('last_synced_push', 0, false);
|
2017-12-04 11:29:23 +08:00
|
|
|
}
|
2017-11-17 10:50:00 +08:00
|
|
|
|
2017-11-03 08:48:02 +08:00
|
|
|
module.exports = {
|
|
|
|
getOption,
|
2018-01-12 12:54:17 +08:00
|
|
|
getOptionOrNull,
|
2017-11-03 08:48:02 +08:00
|
|
|
setOption,
|
2018-01-12 12:54:17 +08:00
|
|
|
initOptions,
|
|
|
|
createOption
|
2017-11-03 08:48:02 +08:00
|
|
|
};
|