2018-05-22 12:22:43 +08:00
|
|
|
const repository = require('./repository');
|
2017-11-03 08:48:02 +08:00
|
|
|
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 appInfo = require('./app_info');
|
2018-05-22 12:22:43 +08:00
|
|
|
const Option = require('../entities/option');
|
2017-11-03 08:48:02 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
async function getOption(name) {
|
2018-05-22 12:22:43 +08:00
|
|
|
const option = await repository.getOption(name);
|
2017-11-03 08:48:02 +08:00
|
|
|
|
2018-05-22 12:22:43 +08:00
|
|
|
if (!option) {
|
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-05-22 12:22:43 +08:00
|
|
|
return option.value;
|
2017-11-03 08:48:02 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
async function setOption(name, value) {
|
2018-05-22 12:22:43 +08:00
|
|
|
const option = await repository.getOption(name);
|
2018-01-12 11:45:25 +08:00
|
|
|
|
2018-05-22 12:22:43 +08:00
|
|
|
if (!option) {
|
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-05-22 12:22:43 +08:00
|
|
|
option.value = value;
|
|
|
|
|
|
|
|
await option.save();
|
2018-01-12 11:45:25 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
async function createOption(name, value, isSynced) {
|
2018-05-22 12:22:43 +08:00
|
|
|
await new Option({
|
2018-01-29 08:30:14 +08:00
|
|
|
name: name,
|
|
|
|
value: value,
|
2018-05-22 12:22:43 +08:00
|
|
|
isSynced: isSynced
|
|
|
|
}).save();
|
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);
|
2018-04-03 10:33:54 +08:00
|
|
|
await createOption('encryptedDataKeyIv', '', 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);
|
2018-06-03 01:02:20 +08:00
|
|
|
|
|
|
|
await createOption('zoomFactor', 1.0, false);
|
2018-06-09 11:18:53 +08:00
|
|
|
await createOption('theme', 'white', 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,
|
|
|
|
setOption,
|
2018-04-03 10:53:01 +08:00
|
|
|
initOptions
|
2017-11-03 08:48:02 +08:00
|
|
|
};
|