Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions docs/api-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@
"wallpaperSource": "bing",
"logoUrl": "",
"columns": 0,
"searchNewTab": true
"searchNewTab": true,
"linkStyle": "classic"
}
}
```
Expand All @@ -103,6 +104,8 @@

`wallpaperSource`:首页壁纸来源,`bing`(默认,Bing 每日壁纸)| `custom`(用 `backgroundUrl` 自定义图)。存量库无此键时按 `bing` 返回。

`linkStyle`:首页链接样式,`classic`(默认)| `compact` | `wide`。存量库无此键(或存量值不在枚举内)时按 `classic` 返回。

### GET /api/wallpaper/today

同源代理 Bing 每日壁纸(无鉴权,公共页未登录也要取图)。
Expand Down Expand Up @@ -208,15 +211,17 @@ 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` 行不删不迁移,只是停产不用。

`searchNewTab`:搜索是否在新标签页打开。存储键 `search_new_tab`,值 `"1"`/`"0"`(同 `totp_enabled` 的 KV 布尔惯例);库中**无此键**(存量库)时按 `true` 返回。写入只接受布尔值,非布尔返回 400 `searchNewTab must be a boolean`。

`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 对象自动删除;外链旧值一律不删。
Expand Down
8 changes: 8 additions & 0 deletions worker/src/routes/admin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Hono } from "hono";
import {
getTablePrefix,
isLinkStyle,
isVisibility,
type HonoEnv,
type SiteSettingsDto,
Expand Down Expand Up @@ -621,6 +622,7 @@ const SETTING_KEYS = [
"columns",
"search_new_tab",
"wallpaper_source",
"link_style",
] as const;

adminRoutes.get("/settings", async (c) => {
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 17 additions & 12 deletions worker/src/services/site-data.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand Down Expand Up @@ -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",
};
}

Expand Down
13 changes: 13 additions & 0 deletions worker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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" &&
Expand Down
97 changes: 97 additions & 0 deletions worker/test/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` 行不删不迁移
Expand Down
1 change: 1 addition & 0 deletions worker/test/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ describe("admin system endpoints", () => {
expect(Object.keys(publicBody.settings).sort()).toEqual([
"backgroundUrl",
"columns",
"linkStyle",
"logoUrl",
"searchNewTab",
"siteTitle",
Expand Down
Loading