2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-01 21:27:46 -04:00
|
|
|
const optionService = require('../../services/options');
|
2018-06-02 13:02:20 -04:00
|
|
|
const log = require('../../services/log');
|
2021-04-17 20:52:46 +02:00
|
|
|
const searchService = require('../../services/search/services/search');
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2018-04-01 17:41:28 -04:00
|
|
|
// options allowed to be updated directly in options dialog
|
2019-11-19 20:53:04 +01:00
|
|
|
const ALLOWED_OPTIONS = new Set([
|
2020-03-28 19:55:02 +01:00
|
|
|
'username', // not exposed for update (not harmful anyway), needed for reading
|
2020-12-14 13:47:33 +01:00
|
|
|
'eraseEntitiesAfterTimeInSeconds',
|
2019-05-10 21:43:40 +02:00
|
|
|
'protectedSessionTimeout',
|
|
|
|
'noteRevisionSnapshotTimeInterval',
|
|
|
|
'zoomFactor',
|
|
|
|
'theme',
|
|
|
|
'syncServerHost',
|
|
|
|
'syncServerTimeout',
|
|
|
|
'syncProxy',
|
|
|
|
'hoistedNoteId',
|
|
|
|
'mainFontSize',
|
|
|
|
'treeFontSize',
|
|
|
|
'detailFontSize',
|
2019-05-12 21:45:30 +02:00
|
|
|
'openTabs',
|
2019-08-22 20:58:43 +02:00
|
|
|
'noteInfoWidget',
|
|
|
|
'attributesWidget',
|
|
|
|
'linkMapWidget',
|
|
|
|
'noteRevisionsWidget',
|
2019-08-29 21:08:53 +02:00
|
|
|
'whatLinksHereWidget',
|
2019-09-01 08:58:13 +02:00
|
|
|
'similarNotesWidget',
|
2019-09-07 10:11:59 +02:00
|
|
|
'editedNotesWidget',
|
2019-09-08 13:08:01 +02:00
|
|
|
'calendarWidget',
|
2019-10-06 21:35:26 +02:00
|
|
|
'codeNotesMimeTypes',
|
|
|
|
'spellCheckEnabled',
|
2019-11-03 17:59:11 +01:00
|
|
|
'spellCheckLanguageCode',
|
|
|
|
'imageMaxWidthHeight',
|
2019-12-23 20:34:29 +01:00
|
|
|
'imageJpegQuality',
|
|
|
|
'leftPaneWidth',
|
|
|
|
'rightPaneWidth',
|
2020-02-04 20:42:40 +01:00
|
|
|
'leftPaneVisible',
|
2019-12-24 12:10:32 +01:00
|
|
|
'rightPaneVisible',
|
2020-07-23 22:31:06 +02:00
|
|
|
'nativeTitleBarVisible',
|
2020-07-25 23:32:46 +02:00
|
|
|
'attributeListExpanded',
|
2020-09-07 23:35:41 +02:00
|
|
|
'promotedAttributesExpanded',
|
2021-03-09 22:06:40 +01:00
|
|
|
'similarNotesExpanded',
|
2021-03-18 20:11:58 +01:00
|
|
|
'headingStyle',
|
2021-09-20 21:08:41 +02:00
|
|
|
'autoCollapseNoteTree',
|
2021-09-20 21:12:35 +02:00
|
|
|
'autoReadonlySizeText',
|
|
|
|
'autoReadonlySizeCode'
|
2019-11-19 20:53:04 +01:00
|
|
|
]);
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getOptions() {
|
|
|
|
const optionMap = optionService.getOptionsMap();
|
2019-11-19 20:53:04 +01:00
|
|
|
const resultMap = {};
|
|
|
|
|
|
|
|
for (const optionName in optionMap) {
|
|
|
|
if (isAllowed(optionName)) {
|
|
|
|
resultMap[optionName] = optionMap[optionName];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultMap;
|
2018-03-30 13:56:46 -04:00
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function updateOption(req) {
|
2018-04-01 20:33:10 -04:00
|
|
|
const {name, value} = req.params;
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
if (!update(name, value)) {
|
2018-07-22 22:21:16 +02:00
|
|
|
return [400, "not allowed option to change"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function updateOptions(req) {
|
2018-07-22 22:21:16 +02:00
|
|
|
for (const optionName in req.body) {
|
2020-06-20 12:31:38 +02:00
|
|
|
if (!update(optionName, req.body[optionName])) {
|
2018-07-22 22:21:16 +02:00
|
|
|
// 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`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function update(name, value) {
|
2019-11-19 20:53:04 +01:00
|
|
|
if (!isAllowed(name)) {
|
2018-07-22 22:21:16 +02:00
|
|
|
return false;
|
2017-10-14 23:31:44 -04:00
|
|
|
}
|
|
|
|
|
2019-11-05 20:59:20 +01:00
|
|
|
if (name !== 'openTabs') {
|
|
|
|
log.info(`Updating option ${name} to ${value}`);
|
|
|
|
}
|
2018-06-02 13:02:20 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
optionService.setOption(name, value);
|
2018-07-22 22:21:16 +02:00
|
|
|
|
|
|
|
return true;
|
2018-03-30 13:56:46 -04:00
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getUserThemes() {
|
2021-06-06 11:01:10 +02:00
|
|
|
const notes = searchService.searchNotes("#appTheme");
|
2019-02-03 00:12:57 +01:00
|
|
|
const ret = [];
|
|
|
|
|
|
|
|
for (const note of notes) {
|
2020-06-20 12:31:38 +02:00
|
|
|
let value = 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) {
|
2020-03-01 20:49:11 +01:00
|
|
|
return ALLOWED_OPTIONS.has(name)
|
|
|
|
|| name.startsWith("keyboardShortcuts")
|
2020-05-02 00:28:40 +02:00
|
|
|
|| name.endsWith("Collapsed")
|
|
|
|
|| name.startsWith("hideArchivedNotes")
|
|
|
|
|| name.startsWith("hideIncludedImages");
|
2019-11-19 20:53:04 +01:00
|
|
|
}
|
|
|
|
|
2018-03-30 13:56:46 -04:00
|
|
|
module.exports = {
|
2018-04-01 20:33:10 -04:00
|
|
|
getOptions,
|
2018-07-22 22:21:16 +02:00
|
|
|
updateOption,
|
2019-01-27 21:18:11 +01:00
|
|
|
updateOptions,
|
|
|
|
getUserThemes
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|