From cd37565c8d6f1708d8ba7e25b9d1677d8b23760a Mon Sep 17 00:00:00 2001 From: Todos Date: Fri, 6 Nov 2026 23:37:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(settings):=20=E6=96=B0=E5=A2=9E=20linkStyl?= =?UTF-8?q?e=20=E9=93=BE=E6=8E=A5=E6=A0=B7=E5=BC=8F=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit settings 白名单加 link_style,PUT 校验 classic|compact|wide(非法值 400), SiteSettingsDto 带出 linkStyle(缺省/脏值归一 classic,无 schema 迁移)。 同步 api-contract.md 与站点设置测试。 --- docs/api-contract.md | 11 +++- worker/src/routes/admin.ts | 8 +++ worker/src/services/site-data.ts | 29 ++++++---- worker/src/types.ts | 13 +++++ worker/test/settings.test.ts | 97 ++++++++++++++++++++++++++++++++ worker/test/setup.test.ts | 1 + 6 files changed, 144 insertions(+), 15 deletions(-) diff --git a/docs/api-contract.md b/docs/api-contract.md index 2983147..1dc4ab8 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -94,7 +94,8 @@ "wallpaperSource": "bing", "logoUrl": "", "columns": 0, - "searchNewTab": true + "searchNewTab": true, + "linkStyle": "classic" } } ``` @@ -103,6 +104,8 @@ `wallpaperSource`:首页壁纸来源,`bing`(默认,Bing 每日壁纸)| `custom`(用 `backgroundUrl` 自定义图)。存量库无此键时按 `bing` 返回。 +`linkStyle`:首页链接样式,`classic`(默认)| `compact` | `wide`。存量库无此键(或存量值不在枚举内)时按 `classic` 返回。 + ### GET /api/wallpaper/today 同源代理 Bing 每日壁纸(无鉴权,公共页未登录也要取图)。 @@ -208,8 +211,8 @@ Passkey(WebAuthn,/api/auth/passkey/*,@simplewebauthn/server v14): ### 站点设置 -- `GET /api/admin/settings` → `{siteTitle, backgroundUrl, wallpaperSource, logoUrl, columns, searchNewTab}` -- `PUT /api/admin/settings` body 可含 `{siteTitle, backgroundUrl, wallpaperSource(bing|custom), logoUrl, columns(0–12), searchNewTab(boolean)}` → 更新后对象 +- `GET /api/admin/settings` → `{siteTitle, backgroundUrl, wallpaperSource, logoUrl, columns, searchNewTab, linkStyle}` +- `PUT /api/admin/settings` body 可含 `{siteTitle, backgroundUrl, wallpaperSource(bing|custom), logoUrl, columns(0–12), searchNewTab(boolean), linkStyle(classic|compact|wide)}` → 更新后对象 主题(设深色/浅色)**不在**站点设置里:深浅色只由访客本机偏好(顶栏主题按钮,`localStorage` 键 `nav.visitor-theme`)决定,没有偏好时跟随系统外观。`PUT` 传 `theme` 不生效(白名单外的键一律忽略);存量的 `settings.theme` 行不删不迁移,只是停产不用。 @@ -217,6 +220,8 @@ Passkey(WebAuthn,/api/auth/passkey/*,@simplewebauthn/server v14): `wallpaperSource`:首页壁纸来源。存储键 `wallpaper_source`,值 `"bing"`/`"custom"`;库中**无此键**(存量库)时按 `bing` 返回。写入只接受 `bing`/`custom`,其他值返回 400 `wallpaperSource must be 'bing' or 'custom'`。 +`linkStyle`:首页链接样式。存储键 `link_style`,值 `"classic"`/`"compact"`/`"wide"`;库中**无此键**(存量库)时按 `classic` 返回,存量脏值同样归一为 `classic`(不改写库行)。写入只接受这三个字符串值(非字符串一律拒绝),其他值返回 400 `linkStyle must be 'classic', 'compact' or 'wide'`,且一次被拒的请求不会写入同包的任何设置键。 + `columns` 允许 `0–12`,其中 `0` 表示**未配置**(用默认值):`0` 原样存入并原样返回,不会被替换成默认列数。`GET /api/admin/settings` 对未配置的库返回 `columns: 0`,所以 `0–12` 都能往返提交;超出范围返回 400 `columns must be between 0 and 12`。 `backgroundUrl` / `logoUrl` 的值可以是外链 URL,也可以是本地上传路径(`/assets/…`,见下节)。`/assets/…` 被替换成**任何**其他值时,旧 R2 对象自动删除;外链旧值一律不删。 diff --git a/worker/src/routes/admin.ts b/worker/src/routes/admin.ts index c5016f5..b759e9a 100644 --- a/worker/src/routes/admin.ts +++ b/worker/src/routes/admin.ts @@ -1,6 +1,7 @@ import { Hono } from "hono"; import { getTablePrefix, + isLinkStyle, isVisibility, type HonoEnv, type SiteSettingsDto, @@ -621,6 +622,7 @@ const SETTING_KEYS = [ "columns", "search_new_tab", "wallpaper_source", + "link_style", ] as const; adminRoutes.get("/settings", async (c) => { @@ -673,6 +675,12 @@ adminRoutes.put("/settings", async (c) => { return badRequest("wallpaperSource must be 'bing' or 'custom'"); updates.wallpaper_source = wallpaperSource; } + if (body.linkStyle !== undefined) { + // 枚举值直接落库;非法/非字符串一律 400,不落任何键(含同包其他设置)。 + if (!isLinkStyle(body.linkStyle)) + return badRequest("linkStyle must be 'classic', 'compact' or 'wide'"); + updates.link_style = body.linkStyle; + } for (const [key, value] of Object.entries(updates)) { await setSetting(c.env.DB, prefix, key, value); diff --git a/worker/src/services/site-data.ts b/worker/src/services/site-data.ts index 8a4fa7b..f5f56fe 100644 --- a/worker/src/services/site-data.ts +++ b/worker/src/services/site-data.ts @@ -1,15 +1,16 @@ -import type { - AdminSceneDto, - Category, - Link, - PublicCategoryDto, - PublicLinkDto, - Scene, - SceneDto, - SearchEngine, - SearchEngineDto, - SiteDataDto, - SiteSettingsDto, +import { + isLinkStyle, + type AdminSceneDto, + type Category, + type Link, + type PublicCategoryDto, + type PublicLinkDto, + type Scene, + type SceneDto, + type SearchEngine, + type SearchEngineDto, + type SiteDataDto, + type SiteSettingsDto, } from "../types"; /** @@ -52,6 +53,10 @@ export function toSiteSettingsDto( columns: Number.isFinite(columns) && columns > 0 ? columns : 0, // 存量库没有该键时按默认「开」:搜索在新标签页打开,起始页不被顶掉。 searchNewTab: (settings.search_new_tab ?? "1") === "1", + // 缺省/脏值一律归一 `classic`:存量库没有该键时用经典链接样式。 + linkStyle: isLinkStyle(settings.link_style) + ? settings.link_style + : "classic", }; } diff --git a/worker/src/types.ts b/worker/src/types.ts index 357d4ca..939cbd6 100644 --- a/worker/src/types.ts +++ b/worker/src/types.ts @@ -201,6 +201,8 @@ export interface SiteSettingsDto { columns: number; /** Open search results in a new tab (settings key `search_new_tab`); defaults to `true`. */ searchNewTab: boolean; + /** 链接样式(settings key `link_style`):缺省 `classic`,脏值同样归一。 */ + linkStyle: LinkStyle; } export interface SiteDataDto { @@ -287,6 +289,17 @@ export function getTablePrefix(env: Env): string { export const VALID_VISIBILITY = ["public", "private"] as const; export type Visibility = (typeof VALID_VISIBILITY)[number]; +/** settings key `link_style` 的合法值;缺省与脏值一律按 `classic`。 */ +export const LINK_STYLES = ["classic", "compact", "wide"] as const; +export type LinkStyle = (typeof LINK_STYLES)[number]; + +export function isLinkStyle(value: unknown): value is LinkStyle { + return ( + typeof value === "string" && + (LINK_STYLES as readonly string[]).includes(value) + ); +} + export function isVisibility(value: unknown): value is Visibility { return ( typeof value === "string" && diff --git a/worker/test/settings.test.ts b/worker/test/settings.test.ts index 7101fd6..a7b803d 100644 --- a/worker/test/settings.test.ts +++ b/worker/test/settings.test.ts @@ -295,6 +295,103 @@ describe("settings wallpaperSource (wallpaper_source)", () => { }); }); +/** + * `linkStyle`(链接样式):KV 键 `link_style`,值 `classic`/`compact`/`wide`。 + * 存量库无此键时 DTO 按 `classic` 返回;公开端点同样带出该字段给前端。 + */ +describe("settings linkStyle (link_style)", () => { + let env: Env; + let app: Hono; + let cookie: string; + + beforeEach(async () => { + env = makeEnv(); + app = makeApp(); + await runMigrations(env); + cookie = await signIn(app, env); + }); + + it("defaults to classic when the key is absent (admin + public DTO)", async () => { + expect(storedSetting(env, "link_style")).toBeNull(); + + const admin = await readJson(await getSettings(app, env, cookie)); + expect(admin.linkStyle).toBe("classic"); + + const pub = await readJson(await app.request("/api/public", {}, env)); + expect(pub.settings.linkStyle).toBe("classic"); + + // Reading the default never writes a row. + expect(storedSetting(env, "link_style")).toBeNull(); + }); + + it("round-trips classic/compact/wide and mirrors into /api/public", async () => { + for (const linkStyle of ["compact", "wide", "classic"] as const) { + const res = await putSettings(app, env, cookie, { linkStyle }); + expect(res.status).toBe(200); + expect((await readJson(res)).linkStyle).toBe(linkStyle); + expect(storedSetting(env, "link_style")).toBe(linkStyle); + expect( + (await readJson(await app.request("/api/public", {}, env))).settings + .linkStyle, + ).toBe(linkStyle); + } + }); + + it("keeps the stored value when other settings are written", async () => { + await putSettings(app, env, cookie, { linkStyle: "wide" }); + const res = await putSettings(app, env, cookie, { siteTitle: "nav2" }); + const body = await readJson(res); + expect(body.siteTitle).toBe("nav2"); + expect(body.linkStyle).toBe("wide"); + }); + + it("rejects invalid values with 400 and keeps the stored value", async () => { + await putSettings(app, env, cookie, { linkStyle: "compact" }); + for (const linkStyle of ["grid", "Classic", "wide ", "", null, 1, true]) { + const res = await putSettings(app, env, cookie, { linkStyle }); + expect(res.status).toBe(400); + expect(await readJson(res)).toEqual({ + error: "linkStyle must be 'classic', 'compact' or 'wide'", + }); + } + expect(storedSetting(env, "link_style")).toBe("compact"); + }); + + it("rejects the whole write when linkStyle is invalid (no partial update)", async () => { + const res = await putSettings(app, env, cookie, { + siteTitle: "nav2", + linkStyle: "grid", + }); + expect(res.status).toBe(400); + expect(storedSetting(env, "site_title")).toBeNull(); + expect(storedSetting(env, "link_style")).toBeNull(); + }); + + it("normalizes a dirty stored value to classic (admin + public DTO)", async () => { + (env.DB as unknown as SqliteD1).db + .prepare("INSERT INTO settings (key, value, updated_at) VALUES (?, ?, ?)") + .run("link_style", "grid", "2026-11-06T00:00:00.000Z"); + + const admin = await readJson(await getSettings(app, env, cookie)); + expect(admin.linkStyle).toBe("classic"); + + const pub = await readJson(await app.request("/api/public", {}, env)); + expect(pub.settings.linkStyle).toBe("classic"); + + // The dirty row itself is left untouched (no migration, no rewrite). + expect(storedSetting(env, "link_style")).toBe("grid"); + }); + + it("lists link_style in the settings whitelist", async () => { + const res = await app.request( + "/api/admin/settings/keys", + { headers: { cookie } }, + env, + ); + expect((await readJson(res)).keys).toContain("link_style"); + }); +}); + /** * 站点主题已删除(2026-11-05 定版):`theme` 不再是站点设置 —— 前后台/公开端点 * 的 settings DTO 都不含它,写入白名单也不再接受它;存量的 `theme` 行不删不迁移 diff --git a/worker/test/setup.test.ts b/worker/test/setup.test.ts index 788635b..c2950c5 100644 --- a/worker/test/setup.test.ts +++ b/worker/test/setup.test.ts @@ -422,6 +422,7 @@ describe("admin system endpoints", () => { expect(Object.keys(publicBody.settings).sort()).toEqual([ "backgroundUrl", "columns", + "linkStyle", "logoUrl", "searchNewTab", "siteTitle",