2017-10-21 21:10:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
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';
|
2017-10-14 23:31:44 -04:00
|
|
|
|
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',
|
2023-06-04 23:01:40 +02:00
|
|
|
'revisionSnapshotTimeInterval',
|
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',
|
2021-12-26 12:24:18 +00:00
|
|
|
'vimKeymapEnabled',
|
2022-11-08 22:55:11 +01:00
|
|
|
'codeLineWrapEnabled',
|
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',
|
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',
|
2021-09-27 21:01:56 +02:00
|
|
|
'autoReadonlySizeCode',
|
2021-10-11 22:30:23 +02:00
|
|
|
'overrideThemeFonts',
|
|
|
|
'dailyBackupEnabled',
|
|
|
|
'weeklyBackupEnabled',
|
|
|
|
'monthlyBackupEnabled',
|
2021-11-21 15:27:13 +00:00
|
|
|
'maxContentWidth',
|
2022-05-21 14:00:53 +02:00
|
|
|
'compressImages',
|
2022-07-16 00:15:45 +02:00
|
|
|
'downloadImagesAutomatically',
|
2022-07-19 16:01:27 -04:00
|
|
|
'minTocHeadings',
|
2023-06-22 15:38:36 +08:00
|
|
|
'highlightsList',
|
2022-11-18 21:47:14 +01:00
|
|
|
'checkForUpdates',
|
2023-04-21 00:19:17 +02:00
|
|
|
'disableTray',
|
2023-05-29 13:02:25 +02:00
|
|
|
'eraseUnusedAttachmentsAfterSeconds',
|
2023-05-07 09:41:33 +02:00
|
|
|
'disableTray',
|
2023-05-04 14:57:33 +08:00
|
|
|
'customSearchEngineName',
|
2023-08-09 22:50:41 +02:00
|
|
|
'customSearchEngineUrl',
|
|
|
|
'promotedAttributesOpenInRibbon',
|
|
|
|
'editedNotesOpenInRibbon'
|
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() {
|
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';
|
2021-12-30 22:54:08 +01:00
|
|
|
|
2019-11-19 20:53:04 +01:00
|
|
|
return resultMap;
|
2018-03-30 13:56:46 -04:00
|
|
|
}
|
2017-10-14 23:31:44 -04:00
|
|
|
|
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;
|
2017-10-14 23:31:44 -04:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
if (!update(name, value)) {
|
2022-12-09 16:04:13 +01:00
|
|
|
throw new ValidationError("not allowed option to change");
|
2018-07-22 22:21:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 21:57:09 +03:00
|
|
|
function updateOptions(req: Request) {
|
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
|
2023-05-04 22:16:18 +02:00
|
|
|
throw new Error(`Option '${optionName}' is not allowed to be changed`);
|
2018-07-22 22:21:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)) {
|
2018-07-22 22:21:16 +02:00
|
|
|
return false;
|
2017-10-14 23:31:44 -04:00
|
|
|
}
|
|
|
|
|
2023-04-11 22:00:04 +02:00
|
|
|
if (name !== 'openNoteContexts') {
|
|
|
|
log.info(`Updating option '${name}' to '${value}'`);
|
2019-11-05 20:59:20 +01:00
|
|
|
}
|
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() {
|
2023-01-15 12:54:38 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-08-11 07:36:09 +03:00
|
|
|
function getSupportedLocales() {
|
|
|
|
// TODO: Currently hardcoded, needs to read the lits of available languages.
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
"id": "en",
|
|
|
|
"name": "English"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "cn",
|
|
|
|
"name": "Chinese"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-04-06 21:57:09 +03:00
|
|
|
function isAllowed(name: string) {
|
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")
|
2023-03-24 09:17:17 +01:00
|
|
|
|| name.startsWith("hideArchivedNotes");
|
2019-11-19 20:53:04 +01:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
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,
|
2024-08-11 07:36:09 +03:00
|
|
|
getUserThemes,
|
|
|
|
getSupportedLocales
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|