trilium/src/routes/api/options.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
const optionService = require('../../services/options');
const log = require('../../services/log');
2019-01-28 04:18:11 +08:00
const attributes = require('../../services/attributes');
// options allowed to be updated directly in options dialog
const ALLOWED_OPTIONS = ['protectedSessionTimeout', 'noteRevisionSnapshotTimeInterval',
'zoomFactor', 'theme', 'syncServerHost', 'syncServerTimeout', 'syncProxy', 'leftPaneMinWidth', 'leftPaneWidthPercent', 'hoistedNoteId', 'mainFontSize', 'treeFontSize', 'detailFontSize'];
2018-04-02 08:33:10 +08:00
async function getOptions() {
2018-09-06 17:54:04 +08:00
return await optionService.getOptionsMap(ALLOWED_OPTIONS);
}
async function updateOption(req) {
2018-04-02 08:33:10 +08:00
const {name, value} = req.params;
if (!update(name, value)) {
return [400, "not allowed option to change"];
}
}
async function updateOptions(req) {
for (const optionName in req.body) {
if (!update(optionName, req.body[optionName])) {
// this should be improved
// it should return 400 instead of current 500, but at least it now rollbacks transaction
throw new Error(`${optionName} is not allowed to change`);
}
}
}
async function update(name, value) {
2018-04-02 08:33:10 +08:00
if (!ALLOWED_OPTIONS.includes(name)) {
return false;
}
log.info(`Updating option ${name} to ${value}`);
await optionService.setOption(name, value);
return true;
}
2019-01-28 04:18:11 +08:00
async function getUserThemes() {
return (await attributes.getNotesWithLabel('appTheme'))
.map(note => {
return {
val: note.title,
title: note.title,
noteId: note.noteId
};
});
}
module.exports = {
2018-04-02 08:33:10 +08:00
getOptions,
updateOption,
2019-01-28 04:18:11 +08:00
updateOptions,
getUserThemes
};