diff --git a/server/constants/discover-list.ts b/server/constants/discover-list.ts new file mode 100644 index 0000000..9a02124 --- /dev/null +++ b/server/constants/discover-list.ts @@ -0,0 +1,2 @@ +/** 发现页用户列表每页条数(与公开列表一致) */ +export const DISCOVER_LIST_PAGE_SIZE = 10; diff --git a/server/utils/discover-card.test.ts b/server/utils/discover-card.test.ts new file mode 100644 index 0000000..27e8e34 --- /dev/null +++ b/server/utils/discover-card.test.ts @@ -0,0 +1,30 @@ +import { describe, expect, test } from "bun:test"; +import { discoverCardAvatarUrl, discoverCardLocationLine } from "./discover-card"; + +describe("discoverCardAvatarUrl", () => { + test("returns null when visibility is not public", () => { + expect(discoverCardAvatarUrl("https://x/a.png", "private")).toBeNull(); + expect(discoverCardAvatarUrl("https://x/a.png", "unlisted")).toBeNull(); + }); + + test("returns null when avatar empty", () => { + expect(discoverCardAvatarUrl(null, "public")).toBeNull(); + expect(discoverCardAvatarUrl(" ", "public")).toBeNull(); + }); + + test("returns trimmed url when public", () => { + expect(discoverCardAvatarUrl(" https://x/a.png ", "public")).toBe("https://x/a.png"); + }); +}); + +describe("discoverCardLocationLine", () => { + test("returns null when show flag false or text empty", () => { + expect(discoverCardLocationLine(true, false, "北京")).toBeNull(); + expect(discoverCardLocationLine(true, true, "")).toBeNull(); + expect(discoverCardLocationLine(true, true, " ")).toBeNull(); + }); + + test("returns trimmed text when allowed", () => { + expect(discoverCardLocationLine(true, true, " 上海 ")).toBe("上海"); + }); +}); diff --git a/server/utils/discover-card.ts b/server/utils/discover-card.ts new file mode 100644 index 0000000..1bd133e --- /dev/null +++ b/server/utils/discover-card.ts @@ -0,0 +1,22 @@ +export function discoverCardAvatarUrl( + avatar: string | null | undefined, + avatarVisibility: string, +): string | null { + if (avatarVisibility !== "public") { + return null; + } + const t = typeof avatar === "string" ? avatar.trim() : ""; + return t.length > 0 ? t : null; +} + +export function discoverCardLocationLine( + discoverVisible: boolean, + discoverShowLocation: boolean, + discoverLocation: string | null | undefined, +): string | null { + if (!discoverVisible || !discoverShowLocation) { + return null; + } + const t = typeof discoverLocation === "string" ? discoverLocation.trim() : ""; + return t.length > 0 ? t : null; +}