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:
khannurien
2026-10-04 21:20:57 +00:00
parent f20ca83f0c
commit 89014639cb
38 changed files with 923 additions and 128 deletions

View File

@@ -28,14 +28,14 @@ Deno.test("runMigrations: a fresh database is left as schema.sql made it", () =>
.get() as {
n: number;
};
assertEquals(applied.n, 18);
assertEquals(applied.n, 19);
// Running again applies nothing.
runMigrations(db);
assertEquals(
(db.prepare(`SELECT COUNT(*) as n FROM schema_migrations;`).get() as {
n: number;
}).n,
18,
19,
);
db.close();
});

View File

@@ -34,6 +34,7 @@ const TABLES = [
"invites",
"attachments",
"chat_messages",
"user_settings",
"categories",
"playlists",
"dumps",

View File

@@ -8,11 +8,11 @@ Deno.test("loadIndexHtml: fills the placeholders, and rereads a rebuilt file", a
const path = `${root}/index.html`;
await Deno.writeTextFile(
path,
"<title>__SITE_NAME__</title><body>__BANDCAMP_PLAYER__</body>",
"<title>__SITE_NAME__</title><body>__BANDCAMP_PLAYER__ __DEFAULT_STYLE__ __EMAIL_ENABLED__</body>",
);
assertEquals(
await loadIndexHtml(root),
"<title>gerbeur</title><body>embed</body>",
"<title>gerbeur</title><body>embed smooth off</body>",
);
await Deno.writeTextFile(path, "<title>rebuilt __SITE_NAME__</title>");

View File

@@ -29,6 +29,10 @@ import {
import { authenticateMedia } from "../../lib/auth.ts";
import type { Dump } from "../../../shared/dump.ts";
import type { PaginatedData } from "../../../shared/api.ts";
import {
DEFAULT_SETTINGS,
type UserSettings,
} from "../../../shared/settings.ts";
function invite(inviterId: string, createdAt = new Date()): string {
const token = crypto.randomUUID();
@@ -541,3 +545,62 @@ test("GET /api/users/me/media-token issues a media token for the caller", async
assertEquals(res.status, 200);
assertEquals(await authenticateMedia(res.data.mediaToken), user.id);
});
// ── Settings ─────────────────────────────────────────────────────────────────
test("settings: defaults until changed, a patch keeps the rest, each user their own", async () => {
const ann = await createUser();
const bob = await createUser();
const initial = await get<UserSettings>("/api/users/me/settings", {
auth: ann.auth,
});
assertEquals(initial.data, DEFAULT_SETTINGS);
const changed = await patch<UserSettings>("/api/users/me/settings", {
auth: ann.auth,
json: { style: "kanagawa", digestWeekly: false },
});
assertEquals(changed.data, {
...DEFAULT_SETTINGS,
style: "kanagawa",
digestWeekly: false,
});
const scheme = await patch<UserSettings>("/api/users/me/settings", {
auth: ann.auth,
json: { colorScheme: "dark" },
});
assertEquals(scheme.data.style, "kanagawa");
assertEquals(scheme.data.colorScheme, "dark");
assertEquals(scheme.data.digestWeekly, false);
const bobs = await get<UserSettings>("/api/users/me/settings", {
auth: bob.auth,
});
assertEquals(bobs.data, DEFAULT_SETTINGS);
// Deleting the account takes its settings along.
db.prepare(`DELETE FROM users WHERE id = ?;`).run(ann.id);
assertEquals(count(`SELECT COUNT(*) as n FROM user_settings;`), 0);
});
test("settings: signed in only, and a bad value or unknown key is refused", async () => {
const ann = await createUser();
assertEquals((await get("/api/users/me/settings")).status, 401);
for (
const json of [
{ style: "comic" },
{ colorScheme: "sepia" },
{ digestMonthly: "no" },
{ theme: "smooth" },
[],
]
) {
const res = await patch("/api/users/me/settings", { auth: ann.auth, json });
assertEquals(res.status, 400, JSON.stringify(json));
}
// Null hands the style back to the instance's.
const reset = await patch<UserSettings>("/api/users/me/settings", {
auth: ann.auth,
json: { style: null },
});
assertEquals(reset.data.style, null);
});