feat(settings): make the server settings real, and widen them
The dashboard offered seven switches that nothing read: they promised a behaviour the bot did not have, which is worse than offering nothing. Every setting is now declared once, with its type, its bounds and its explanation, and the services read it: the welcome message and its wording, the automatic role, departure notices, the private notice sent to a sanctioned member, the default mute duration, how much history a ban erases, whether a closed ticket is deleted or archived, and an optional role mention on the Gitea notifications. A test walks the sources and fails when a declared key is read nowhere, so the list cannot drift back into decoration. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
13 files changed
+576
-146
No files matched your search
+19
-77
@@ -11,7 +11,7 @@ import { env } from '../../config/env.js';
|
||||
import { createLogger } from '../../core/logger.js';
|
||||
import { getDatabase } from '../../database/index.js';
|
||||
import { logEvent, purgeEventsOlderThan } from '../../database/repositories/event-log-repository.js';
|
||||
import { getSetting, setSetting } from '../../database/repositories/settings-repository.js';
|
||||
import { GUILD_SETTINGS, SETTING_SECTIONS, allSettings, writeSetting } from '../../config/guild-settings.js';
|
||||
import {
|
||||
createApiToken,
|
||||
deleteSession,
|
||||
@@ -31,76 +31,21 @@ import { isOauthConfigured, redirectUri } from '../auth/discord-oauth.js';
|
||||
const log = createLogger('web:settings');
|
||||
const guildId = () => env.discord.guildId;
|
||||
|
||||
/** Guild settings the dashboard exposes as first-class switches. */
|
||||
const MANAGED_SETTINGS = Object.freeze([
|
||||
{
|
||||
key: 'welcome.enabled',
|
||||
label: 'Message de bienvenue',
|
||||
help: 'Publier un message dans le salon d’accueil à chaque arrivée.',
|
||||
type: 'bool',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
key: 'welcome.directMessage',
|
||||
label: 'Message privé de bienvenue',
|
||||
help: 'Envoyer en plus un message privé au nouveau membre.',
|
||||
type: 'bool',
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
key: 'departure.announce',
|
||||
label: 'Annoncer les départs',
|
||||
help: 'Publier une ligne dans les logs quand un membre quitte le serveur.',
|
||||
type: 'bool',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
key: 'moderation.dmTarget',
|
||||
label: 'Prévenir le membre sanctionné',
|
||||
help: 'Envoyer un message privé expliquant la sanction et son motif.',
|
||||
type: 'bool',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
key: 'tickets.autoClaim',
|
||||
label: 'Prise en charge automatique',
|
||||
help: 'Attribuer le ticket à la première personne du staff qui y écrit.',
|
||||
type: 'bool',
|
||||
default: false,
|
||||
},
|
||||
{
|
||||
key: 'deploy.requireConfirmation',
|
||||
label: 'Confirmation avant déploiement',
|
||||
help: 'Demander de retaper le nom de l’environnement avant de lancer un déploiement.',
|
||||
type: 'bool',
|
||||
default: true,
|
||||
},
|
||||
{
|
||||
key: 'web.defaultMode',
|
||||
label: 'Mode d’affichage par défaut',
|
||||
help: 'Interface « simple » (l’essentiel) ou « avancée » (tous les réglages).',
|
||||
type: 'enum',
|
||||
options: ['simple', 'advanced'],
|
||||
default: 'simple',
|
||||
},
|
||||
]);
|
||||
|
||||
const readManaged = () =>
|
||||
Object.fromEntries(
|
||||
MANAGED_SETTINGS.map((setting) => {
|
||||
try {
|
||||
return [setting.key, getSetting(guildId(), setting.key, setting.default)];
|
||||
} catch {
|
||||
return [setting.key, setting.default];
|
||||
}
|
||||
}),
|
||||
);
|
||||
const readManaged = () => {
|
||||
try {
|
||||
return allSettings(guildId());
|
||||
} catch {
|
||||
// The database is not ready: fall back to the declared defaults.
|
||||
return Object.fromEntries(GUILD_SETTINGS.map((setting) => [setting.key, setting.default]));
|
||||
}
|
||||
};
|
||||
|
||||
export function registerSettingsRoutes(router) {
|
||||
router.get(
|
||||
'/api/settings',
|
||||
({ request, actor }) => ({
|
||||
schema: MANAGED_SETTINGS,
|
||||
schema: GUILD_SETTINGS,
|
||||
sections: SETTING_SECTIONS,
|
||||
values: readManaged(),
|
||||
runtime: {
|
||||
nodeEnv: env.nodeEnv,
|
||||
@@ -166,25 +111,22 @@ export function registerSettingsRoutes(router) {
|
||||
router.put(
|
||||
'/api/settings/values/:key',
|
||||
({ params, body, actor }) => {
|
||||
const schema = MANAGED_SETTINGS.find((setting) => setting.key === params.key);
|
||||
if (!schema) throw notFound('Réglage inconnu.');
|
||||
if (!GUILD_SETTINGS.some((setting) => setting.key === params.key)) throw notFound('Réglage inconnu.');
|
||||
|
||||
let value = body.value;
|
||||
if (schema.type === 'bool') value = Boolean(value);
|
||||
else if (schema.type === 'enum') {
|
||||
value = String(value);
|
||||
if (!schema.options.includes(value))
|
||||
throw badRequest(`Valeurs possibles : ${schema.options.join(', ')}.`);
|
||||
let value;
|
||||
try {
|
||||
value = writeSetting(params.key, body.value, { guildId: guildId(), updatedBy: actor.userId });
|
||||
} catch (error) {
|
||||
throw badRequest(error.message);
|
||||
}
|
||||
|
||||
setSetting(guildId(), schema.key, value, actor.userId);
|
||||
logEvent({
|
||||
guildId: guildId(),
|
||||
type: 'web.settings.update',
|
||||
actorId: actor.userId,
|
||||
payload: { key: schema.key, value },
|
||||
payload: { key: params.key, value },
|
||||
});
|
||||
return { key: schema.key, value };
|
||||
return { key: params.key, value };
|
||||
},
|
||||
{ access: ACCESS_LEVELS.DEV },
|
||||
);
|
||||
|
||||
@@ -33,47 +33,91 @@ const keyValue = (rows) =>
|
||||
rows,
|
||||
);
|
||||
|
||||
function behaviourCard(data, level, reload) {
|
||||
if (level < LEVELS.DEV) {
|
||||
return card(
|
||||
'Comportement du bot',
|
||||
{ subtitle: 'Lecture seule : le niveau Dev est requis pour modifier ces réglages.' },
|
||||
keyValue(
|
||||
data.schema.map((setting) => ({
|
||||
label: setting.label,
|
||||
value: String(data.values[setting.key]),
|
||||
})),
|
||||
),
|
||||
/** One control per setting, typed after its declaration. */
|
||||
function settingControl(setting, value, apply) {
|
||||
if (setting.type === 'bool') {
|
||||
return h(
|
||||
'div',
|
||||
{},
|
||||
toggle(Boolean(value), apply, setting.label),
|
||||
h('div', { class: 'field-help', text: setting.help }),
|
||||
);
|
||||
}
|
||||
|
||||
return card(
|
||||
'Comportement du bot',
|
||||
{ subtitle: 'Appliqué immédiatement, sans redémarrage.' },
|
||||
...data.schema.map((setting) => {
|
||||
const apply = async (value) => {
|
||||
await api.put(`/api/settings/values/${setting.key}`, { value });
|
||||
toast(`« ${setting.label} » enregistré.`);
|
||||
};
|
||||
if (setting.type === 'enum') {
|
||||
return field(
|
||||
setting.label,
|
||||
select(
|
||||
setting.options.map((option) => ({ value: option, label: option })),
|
||||
{ value, onChange: (event) => apply(event.target.value) },
|
||||
),
|
||||
{ help: setting.help },
|
||||
);
|
||||
}
|
||||
|
||||
if (setting.type === 'bool') {
|
||||
return h(
|
||||
'div',
|
||||
{},
|
||||
toggle(Boolean(data.values[setting.key]), apply, setting.label),
|
||||
h('div', { class: 'field-help', text: setting.help }),
|
||||
);
|
||||
}
|
||||
return field(
|
||||
setting.label,
|
||||
select(
|
||||
setting.options.map((option) => ({ value: option, label: option })),
|
||||
{ value: data.values[setting.key], onChange: (event) => apply(event.target.value) },
|
||||
),
|
||||
{ help: setting.help },
|
||||
const control = input({
|
||||
value: value ?? '',
|
||||
type: setting.type === 'int' ? 'number' : 'text',
|
||||
min: setting.min,
|
||||
max: setting.max,
|
||||
maxlength: setting.maxLength,
|
||||
placeholder: String(setting.default ?? ''),
|
||||
});
|
||||
|
||||
// Text fields save on blur rather than on every keystroke.
|
||||
const commit = () => apply(setting.type === 'int' ? Number(control.value) : control.value);
|
||||
control.addEventListener('change', commit);
|
||||
control.addEventListener('keydown', (event) => {
|
||||
if (event.key === 'Enter') control.blur();
|
||||
});
|
||||
|
||||
return field(setting.label, control, { help: setting.help });
|
||||
}
|
||||
|
||||
function behaviourCard(data, level, reload) {
|
||||
const editable = level >= LEVELS.DEV;
|
||||
const sections = data.sections ?? [];
|
||||
|
||||
return h(
|
||||
'div',
|
||||
{ class: 'stack' },
|
||||
...sections.map((section) => {
|
||||
const settings = data.schema.filter((setting) => setting.section === section.id);
|
||||
if (settings.length === 0) return null;
|
||||
|
||||
return card(
|
||||
section.title,
|
||||
{ subtitle: section.summary },
|
||||
...settings.map((setting) => {
|
||||
const value = data.values[setting.key];
|
||||
|
||||
if (!editable) {
|
||||
return h(
|
||||
'div',
|
||||
{ class: setting.advanced ? 'advanced-only' : '' },
|
||||
h('strong', { text: `${setting.label} : ` }),
|
||||
h('span', { text: String(value) }),
|
||||
h('div', { class: 'field-help', text: setting.help }),
|
||||
);
|
||||
}
|
||||
|
||||
const apply = async (next) => {
|
||||
try {
|
||||
const saved = await api.put(`/api/settings/values/${setting.key}`, { value: next });
|
||||
toast(`« ${setting.label} » enregistré.`);
|
||||
data.values[setting.key] = saved.value;
|
||||
} catch (error) {
|
||||
toast(error.message, 'error');
|
||||
}
|
||||
};
|
||||
|
||||
const node = settingControl(setting, value, apply);
|
||||
if (setting.advanced) node.classList.add('advanced-only');
|
||||
return node;
|
||||
}),
|
||||
);
|
||||
}),
|
||||
button('Recharger', reload, { tone: 'ghost' }),
|
||||
editable && button('Recharger les réglages', reload, { tone: 'ghost' }),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user