v4: settings are stored on the server, starting with the style, the color scheme and the digest switches — every setting lived in the browser's localStorage, so it didn't follow an account to another browser and the server couldn't read it, which the digests need (whether to send each, and in which style). user_settings (migration 0019) holds one row per user who changed anything, the defaults otherwise, and GET/PATCH /api/users/me/settings read and change it (settings-service, model/settings, validators/settings, shared/settings). AuthProvider reads the settings into the session alongside the user, on mount and on focus, so a change made elsewhere arrives; useSettings changes them at once and puts them back if the server refuses. ThemeProvider now sits inside AuthProvider: a signed-in user's choice comes from their settings and is mirrored to localStorage for index.html's first-paint script, a visitor's stays in the browser, and an account with no choice yet takes the one this browser had, once. Until anyone picks a style, the instance's GERBEUR_DEFAULT_STYLE applies, written into index.html's default-style tag along with email-enabled (src/config/instance.ts), and a browser no longer stores the default as if it had been chosen, so changing the instance's style reaches everyone who never picked one. The settings tab gets an Email section with the weekly digest and monthly recap switches, disabled with a note when the server has no SMTP. The style and scheme names moved to shared/theme.ts in the previous commit; the default feed tab and sounds stay in the browser for now. 223 backend tests (new: settings defaults, patches, isolation, refusals and cascade; index.html's new tags) and 658 frontend tests (new: useSettings, the account's theme winning and a browser's carried over, settings read into the session, the digest switches) pass, and tsc, eslint, doc:lint, deno check, deno lint and deno fmt are clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
32
api/validators/settings.ts
Normal file
32
api/validators/settings.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Checks the body of `PATCH /api/users/me/settings` before the users route
|
||||
* trusts it.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
import type { UpdateSettingsRequest } from "../../shared/settings.ts";
|
||||
import { isColorScheme, isStyleName } from "../../shared/theme.ts";
|
||||
|
||||
const VALIDATORS: Record<keyof UpdateSettingsRequest, (v: unknown) => boolean> =
|
||||
{
|
||||
style: (v) => v === null || isStyleName(v),
|
||||
colorScheme: (v) => v === null || isColorScheme(v),
|
||||
digestWeekly: (v) => typeof v === "boolean",
|
||||
digestMonthly: (v) => typeof v === "boolean",
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks whether `obj` is an {@link UpdateSettingsRequest}: an object whose
|
||||
* every key is a setting, each with a value that setting takes. An unknown
|
||||
* key fails, so a typo doesn't pass for a change.
|
||||
*/
|
||||
export function isUpdateSettingsRequest(
|
||||
obj: unknown,
|
||||
): obj is UpdateSettingsRequest {
|
||||
if (!obj || typeof obj !== "object" || Array.isArray(obj)) return false;
|
||||
return Object.entries(obj).every(([key, value]) =>
|
||||
key in VALIDATORS &&
|
||||
VALIDATORS[key as keyof UpdateSettingsRequest](value)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user