2017-11-03 08:48:02 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const utils = require('./utils');
|
2018-04-03 08:46:46 +08:00
|
|
|
const dateUtils = require('./date_utils');
|
2018-04-02 09:27:46 +08:00
|
|
|
const syncTableService = require('./sync_table');
|
|
|
|
const appInfo = require('./app_info');
|
2017-11-03 08:48:02 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
async function getOptionOrNull(name) {
|
2018-04-01 11:11:43 +08:00
|
|
|
return await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);
|
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-04-01 11:11:43 +08:00
|
|
|
return row.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-04-02 09:27:46 +08:00
|
|
|
await syncTableService.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 = ?",
|
2018-04-03 08:46:46 +08:00
|
|
|
[value, dateUtils.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,
|
2018-04-03 08:46:46 +08:00
|
|
|
dateModified: dateUtils.nowDate()
|
2017-12-04 11:29:23 +08:00
|
|
|
});
|
2018-01-12 11:45:25 +08:00
|
|
|
|
|
|
|
if (isSynced) {
|
2018-04-02 09:27:46 +08:00
|
|
|
await syncTableService.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-04-03 09:47:46 +08:00
|
|
|
await createOption('documentId', utils.randomSecureToken(16), false);
|
|
|
|
await createOption('documentSecret', utils.randomSecureToken(16), false);
|
2018-01-12 11:45:25 +08:00
|
|
|
|
|
|
|
await createOption('username', '', true);
|
2018-04-03 09:47:46 +08:00
|
|
|
await createOption('passwordVerificationHash', '', true);
|
|
|
|
await createOption('passwordVerificationSalt', '', true);
|
|
|
|
await createOption('passwordDerivedKeySalt', '', true);
|
|
|
|
await createOption('encryptedDataKey', '', true);
|
|
|
|
await createOption('encryptedDataKey_iv', '', true);
|
2018-01-12 11:45:25 +08:00
|
|
|
|
2018-04-03 09:47:46 +08:00
|
|
|
await createOption('startNotePath', startNotePath, false);
|
|
|
|
await createOption('protectedSessionTimeout', 600, true);
|
|
|
|
await createOption('noteRevisionSnapshotTimeInterval', 600, true);
|
|
|
|
await createOption('lastBackupDate', dateUtils.nowDate(), false);
|
|
|
|
await createOption('dbVersion', appInfo.dbVersion, false);
|
2018-01-12 11:45:25 +08:00
|
|
|
|
2018-04-03 09:47:46 +08:00
|
|
|
await createOption('lastSyncedPull', appInfo.dbVersion, false);
|
|
|
|
await createOption('lastSyncedPush', 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
|
|
|
};
|