trilium/src/routes/api/options.js
zadam 93addac8bb Merge branch 'master' into next61
# Conflicts:
#	src/becca/entities/bnote.js
#	src/public/app/components/note_context.js
#	src/public/app/layouts/desktop_layout.js
#	src/public/app/services/note_content_renderer.js
#	src/public/app/services/utils.js
#	src/public/app/widgets/ribbon_widgets/file_properties.js
#	src/public/app/widgets/ribbon_widgets/note_info_widget.js
#	src/services/notes.js
2023-06-05 00:09:55 +02:00

153 lines
3.7 KiB
JavaScript

"use strict";
const optionService = require('../../services/options');
const log = require('../../services/log');
const searchService = require('../../services/search/services/search');
const ValidationError = require("../../errors/validation_error");
// options allowed to be updated directly in options dialog
const ALLOWED_OPTIONS = new Set([
'eraseEntitiesAfterTimeInSeconds',
'protectedSessionTimeout',
'revisionSnapshotTimeInterval',
'zoomFactor',
'theme',
'syncServerHost',
'syncServerTimeout',
'syncProxy',
'hoistedNoteId',
'mainFontSize',
'mainFontFamily',
'treeFontSize',
'treeFontFamily',
'detailFontSize',
'detailFontFamily',
'monospaceFontSize',
'monospaceFontFamily',
'openNoteContexts',
'noteInfoWidget',
'attributesWidget',
'linkMapWidget',
'revisionsWidget',
'whatLinksHereWidget',
'similarNotesWidget',
'editedNotesWidget',
'calendarWidget',
'vimKeymapEnabled',
'codeLineWrapEnabled',
'codeNotesMimeTypes',
'spellCheckEnabled',
'spellCheckLanguageCode',
'imageMaxWidthHeight',
'imageJpegQuality',
'leftPaneWidth',
'rightPaneWidth',
'leftPaneVisible',
'rightPaneVisible',
'nativeTitleBarVisible',
'attributeListExpanded',
'promotedAttributesExpanded',
'similarNotesExpanded',
'headingStyle',
'autoCollapseNoteTree',
'autoReadonlySizeText',
'autoReadonlySizeCode',
'overrideThemeFonts',
'dailyBackupEnabled',
'weeklyBackupEnabled',
'monthlyBackupEnabled',
'maxContentWidth',
'compressImages',
'downloadImagesAutomatically',
'minTocHeadings',
'highlightedText',
'checkForUpdates',
'disableTray',
'eraseUnusedAttachmentsAfterSeconds',
'disableTray',
'customSearchEngineName',
'customSearchEngineUrl',
]);
function getOptions() {
const optionMap = optionService.getOptionsMap();
const resultMap = {};
for (const optionName in optionMap) {
if (isAllowed(optionName)) {
resultMap[optionName] = optionMap[optionName];
}
}
resultMap['isPasswordSet'] = optionMap['passwordVerificationHash'] ? 'true' : 'false';
return resultMap;
}
function updateOption(req) {
const {name, value} = req.params;
if (!update(name, value)) {
throw new ValidationError("not allowed option to change");
}
}
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(`Option '${optionName}' is not allowed to be changed`);
}
}
}
function update(name, value) {
if (!isAllowed(name)) {
return false;
}
if (name !== 'openNoteContexts') {
log.info(`Updating option '${name}' to '${value}'`);
}
optionService.setOption(name, value);
return true;
}
function getUserThemes() {
const notes = searchService.searchNotes("#appTheme", {ignoreHoistedNote: true});
const ret = [];
for (const note of notes) {
let value = note.getOwnedLabelValue('appTheme');
if (!value) {
value = note.title.toLowerCase().replace(/[^a-z0-9]/gi, '-');
}
ret.push({
val: value,
title: note.title,
noteId: note.noteId
});
}
return ret;
}
function isAllowed(name) {
return ALLOWED_OPTIONS.has(name)
|| name.startsWith("keyboardShortcuts")
|| name.endsWith("Collapsed")
|| name.startsWith("hideArchivedNotes");
}
module.exports = {
getOptions,
updateOption,
updateOptions,
getUserThemes
};