server: Implement color theme migration based on existing theme

This commit is contained in:
Elian Doran 2024-11-02 00:39:22 +02:00
parent 5c66e3fd04
commit 92c588dc98
No known key found for this signature in database
2 changed files with 31 additions and 6 deletions

View File

@ -3,6 +3,11 @@ import BOption from "../becca/entities/boption.js";
import { OptionRow } from '../becca/entities/rows.js';
import sql from "./sql.js";
/**
* A dictionary where the keys are the option keys (e.g. `theme`) and their corresponding values.
*/
export type OptionMap = Record<string | number, string>;
function getOptionOrNull(name: string): string | null {
let option;
@ -82,7 +87,7 @@ function getOptions() {
}
function getOptionMap() {
const map: Record<string | number, string> = {};
const map: OptionMap = {};
for (const option of Object.values(becca.options)) {
map[option.name] = option.value;

View File

@ -1,4 +1,4 @@
import optionService from "./options.js";
import optionService, { OptionMap } from "./options.js";
import appInfo from "./app_info.js";
import utils from "./utils.js";
import log from "./log.js";
@ -19,9 +19,17 @@ interface NotSyncedOpts {
syncProxy?: string;
}
/**
* Represents a correspondence between an option and its default value, to be initialized when the database is missing that particular option (after a migration from an older version, or when creating a new database).
*/
interface DefaultOption {
name: string;
value: string;
/**
* The value to initialize the option with, if the option is not already present in the database.
*
* If a function is passed in instead, the function is called if the option does not exist (with access to the current options) and the return value is used instead. Useful to migrate a new option with a value depending on some other option that might be initialized.
*/
value: string | ((options: OptionMap) => string);
isSynced: boolean;
}
@ -111,7 +119,13 @@ const defaultOptions: DefaultOption[] = [
{ name: 'locale', value: 'en', isSynced: true },
{ name: 'firstDayOfWeek', value: '1', isSynced: true },
{ name: "codeBlockTheme", value: "default:atom-one-dark", isSynced: false },
{ name: "codeBlockTheme", value: (optionsMap) => {
if (optionsMap.theme === "light") {
return "default:stackoverflow-light";
} else {
return "default:stackoverflow-dark";
}
}, isSynced: false },
{ name: "codeBlockWordWrap", value: "false", isSynced: true }
];
@ -122,9 +136,15 @@ function initStartupOptions() {
for (const {name, value, isSynced} of allDefaultOptions) {
if (!(name in optionsMap)) {
optionService.createOption(name, value, isSynced);
let resolvedValue;
if (typeof value === "function") {
resolvedValue = value(optionsMap);
} else {
resolvedValue = value;
}
log.info(`Created option "${name}" with default value "${value}"`);
optionService.createOption(name, resolvedValue, isSynced);
log.info(`Created option "${name}" with default value "${resolvedValue}"`);
}
}