Notes/src/routes/api/options.js

123 lines
2.9 KiB
JavaScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
const optionService = require('../../services/options');
const log = require('../../services/log');
2019-01-27 21:18:11 +01:00
const attributes = require('../../services/attributes');
// options allowed to be updated directly in options dialog
2019-11-19 20:53:04 +01:00
const ALLOWED_OPTIONS = new Set([
'username', // not exposed for update (not harmful anyway), needed for reading
'eraseNotesAfterTimeInSeconds',
2019-05-10 21:43:40 +02:00
'protectedSessionTimeout',
'noteRevisionSnapshotTimeInterval',
'zoomFactor',
'theme',
'syncServerHost',
'syncServerTimeout',
'syncProxy',
'hoistedNoteId',
'mainFontSize',
'treeFontSize',
'detailFontSize',
'openTabs',
2019-08-22 20:58:43 +02:00
'noteInfoWidget',
'attributesWidget',
'linkMapWidget',
'noteRevisionsWidget',
'whatLinksHereWidget',
'similarNotesWidget',
2019-09-07 10:11:59 +02:00
'editedNotesWidget',
2019-09-08 13:08:01 +02:00
'calendarWidget',
'codeNotesMimeTypes',
'spellCheckEnabled',
2019-11-03 17:59:11 +01:00
'spellCheckLanguageCode',
'imageMaxWidthHeight',
'imageJpegQuality',
'leftPaneWidth',
'rightPaneWidth',
'leftPaneVisible',
'rightPaneVisible',
'nativeTitleBarVisible'
2019-11-19 20:53:04 +01:00
]);
2018-04-01 20:33:10 -04:00
async function getOptions() {
2019-11-19 20:53:04 +01:00
const optionMap = await optionService.getOptionsMap();
const resultMap = {};
for (const optionName in optionMap) {
if (isAllowed(optionName)) {
resultMap[optionName] = optionMap[optionName];
}
}
return resultMap;
}
async function updateOption(req) {
2018-04-01 20:33:10 -04:00
const {name, value} = req.params;
2019-11-19 20:53:04 +01:00
if (!await update(name, value)) {
return [400, "not allowed option to change"];
}
}
async function updateOptions(req) {
for (const optionName in req.body) {
2019-11-19 20:53:04 +01:00
if (!await 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) {
2019-11-19 20:53:04 +01:00
if (!isAllowed(name)) {
return false;
}
if (name !== 'openTabs') {
log.info(`Updating option ${name} to ${value}`);
}
await optionService.setOption(name, value);
return true;
}
2019-01-27 21:18:11 +01:00
async function getUserThemes() {
2019-02-03 00:12:57 +01:00
const notes = await attributes.getNotesWithLabel('appTheme');
const ret = [];
for (const note of notes) {
let value = await note.getOwnedLabelValue('appTheme');
2019-02-03 00:12:57 +01:00
2019-02-03 15:35:37 +01:00
if (!value) {
2019-02-03 00:12:57 +01:00
value = note.title.toLowerCase().replace(/[^a-z0-9]/gi, '-');
}
ret.push({
val: value,
title: note.title,
noteId: note.noteId
2019-01-27 21:18:11 +01:00
});
2019-02-03 00:12:57 +01:00
}
return ret;
2019-01-27 21:18:11 +01:00
}
2019-11-19 20:53:04 +01:00
function isAllowed(name) {
return ALLOWED_OPTIONS.has(name)
|| name.startsWith("keyboardShortcuts")
|| name.endsWith("Collapsed")
|| name.startsWith("hideArchivedNotes")
|| name.startsWith("hideIncludedImages");
2019-11-19 20:53:04 +01:00
}
module.exports = {
2018-04-01 20:33:10 -04:00
getOptions,
updateOption,
2019-01-27 21:18:11 +01:00
updateOptions,
getUserThemes
};