Notes/src/routes/api/options.ts

171 lines
4.1 KiB
TypeScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
import optionService from "../../services/options.js";
import log from "../../services/log.js";
import searchService from "../../services/search/services/search.js";
import ValidationError from "../../errors/validation_error.js";
2024-04-06 21:57:09 +03:00
import { Request } from 'express';
2023-06-30 11:18:34 +02:00
// options allowed to be updated directly in the Options dialog
2019-11-19 20:53:04 +01:00
const ALLOWED_OPTIONS = new Set([
2020-12-14 13:47:33 +01:00
'eraseEntitiesAfterTimeInSeconds',
2019-05-10 21:43:40 +02:00
'protectedSessionTimeout',
'revisionSnapshotTimeInterval',
2024-09-04 08:41:17 +00:00
'revisionSnapshotNumberLimit',
2019-05-10 21:43:40 +02:00
'zoomFactor',
'theme',
'syncServerHost',
'syncServerTimeout',
'syncProxy',
'hoistedNoteId',
'mainFontSize',
2021-09-27 21:01:56 +02:00
'mainFontFamily',
2019-05-10 21:43:40 +02:00
'treeFontSize',
2021-09-27 21:01:56 +02:00
'treeFontFamily',
2019-05-10 21:43:40 +02:00
'detailFontSize',
2021-09-27 21:01:56 +02:00
'detailFontFamily',
'monospaceFontSize',
'monospaceFontFamily',
2023-04-11 22:00:04 +02:00
'openNoteContexts',
'vimKeymapEnabled',
2022-11-08 22:55:11 +01:00
'codeLineWrapEnabled',
'codeNotesMimeTypes',
'spellCheckEnabled',
2019-11-03 17:59:11 +01:00
'spellCheckLanguageCode',
'imageMaxWidthHeight',
'imageJpegQuality',
'leftPaneWidth',
'rightPaneWidth',
'leftPaneVisible',
'rightPaneVisible',
'nativeTitleBarVisible',
'headingStyle',
'autoCollapseNoteTree',
'autoReadonlySizeText',
2021-09-27 21:01:56 +02:00
'autoReadonlySizeCode',
2021-10-11 22:30:23 +02:00
'overrideThemeFonts',
'dailyBackupEnabled',
'weeklyBackupEnabled',
'monthlyBackupEnabled',
'maxContentWidth',
'compressImages',
'downloadImagesAutomatically',
2022-07-19 16:01:27 -04:00
'minTocHeadings',
'highlightsList',
'checkForUpdates',
2023-04-21 00:19:17 +02:00
'disableTray',
2023-05-29 13:02:25 +02:00
'eraseUnusedAttachmentsAfterSeconds',
'disableTray',
2023-05-04 14:57:33 +08:00
'customSearchEngineName',
'customSearchEngineUrl',
'promotedAttributesOpenInRibbon',
'editedNotesOpenInRibbon',
'locale',
'firstDayOfWeek'
2019-11-19 20:53:04 +01:00
]);
2020-06-20 12:31:38 +02:00
function getOptions() {
2023-06-29 22:10:13 +02:00
const optionMap = optionService.getOptionMap();
2024-04-06 21:57:09 +03:00
const resultMap: Record<string, string> = {};
2019-11-19 20:53:04 +01:00
for (const optionName in optionMap) {
if (isAllowed(optionName)) {
resultMap[optionName] = optionMap[optionName];
}
}
2023-04-09 23:08:24 +02:00
resultMap['isPasswordSet'] = optionMap['passwordVerificationHash'] ? 'true' : 'false';
2019-11-19 20:53:04 +01:00
return resultMap;
}
2024-04-06 21:57:09 +03:00
function updateOption(req: Request) {
2018-04-01 20:33:10 -04:00
const {name, value} = req.params;
2020-06-20 12:31:38 +02:00
if (!update(name, value)) {
throw new ValidationError("not allowed option to change");
}
}
2024-04-06 21:57:09 +03:00
function updateOptions(req: Request) {
for (const optionName in req.body) {
2020-06-20 12:31:38 +02:00
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
2023-05-04 22:16:18 +02:00
throw new Error(`Option '${optionName}' is not allowed to be changed`);
}
}
}
2024-04-06 21:57:09 +03:00
function update(name: string, value: string) {
2019-11-19 20:53:04 +01:00
if (!isAllowed(name)) {
return false;
}
2023-04-11 22:00:04 +02:00
if (name !== 'openNoteContexts') {
log.info(`Updating option '${name}' to '${value}'`);
}
2020-06-20 12:31:38 +02:00
optionService.setOption(name, value);
return true;
}
2020-06-20 12:31:38 +02:00
function getUserThemes() {
const notes = searchService.searchNotes("#appTheme", {ignoreHoistedNote: true});
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
}
function getSupportedLocales() {
2024-08-11 08:29:02 +03:00
// TODO: Currently hardcoded, needs to read the list of available languages.
return [
{
"id": "en",
"name": "English"
},
2024-08-15 10:30:32 +03:00
{
"id": "es",
"name": "Español"
},
{
"id": "cn",
2024-08-15 16:23:37 +08:00
"name": "简体中文"
},
{
"id": "ro",
"name": "Română"
}
];
}
2024-04-06 21:57:09 +03:00
function isAllowed(name: string) {
return ALLOWED_OPTIONS.has(name)
|| name.startsWith("keyboardShortcuts")
|| name.endsWith("Collapsed")
|| name.startsWith("hideArchivedNotes");
2019-11-19 20:53:04 +01:00
}
export default {
2018-04-01 20:33:10 -04:00
getOptions,
updateOption,
2019-01-27 21:18:11 +01:00
updateOptions,
getUserThemes,
getSupportedLocales
2020-06-20 12:31:38 +02:00
};