trilium/src/services/options.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

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');
const appInfo = require('./app_info');
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) {
const option = await repository.getOption(name);
2017-11-03 08:48:02 +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
}
return option.value;
2017-11-03 08:48:02 +08:00
}
async function setOption(name, value) {
const option = await repository.getOption(name);
if (!option) {
2018-01-29 08:30:14 +08:00
throw new Error(`Option ${name} doesn't exist`);
}
option.value = value;
await option.save();
}
async function createOption(name, value, isSynced) {
await new Option({
2018-01-29 08:30:14 +08:00
name: name,
value: value,
isSynced: isSynced
}).save();
2017-11-03 08:48:02 +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);
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-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-04-03 09:47:46 +08:00
await createOption('lastSyncedPull', appInfo.dbVersion, false);
await createOption('lastSyncedPush', 0, false);
await createOption('zoomFactor', 1.0, false);
await createOption('theme', 'white', false);
2017-12-04 11:29:23 +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
};