"use client"; import { loadJson, saveJson } from "./storage"; export const LIVENESS_KEY = "social-liveness"; export const FACE_OK_KEY = "social-face-ok"; const ROSTER_KEY = "roster"; export type LivenessState = { faceOk: boolean; livenessStatus: "none" | "pending" | "verified" | "rejected"; verifiedAt?: string; videoUrl?: string; }; const EMPTY: LivenessState = { faceOk: false, livenessStatus: "none", }; export function getLivenessState(): LivenessState { return loadJson(LIVENESS_KEY, EMPTY); } function bump(): void { if (typeof window === "undefined") return; window.dispatchEvent(new Event("nomadro-social-storage")); } export function markFaceOk(): void { const cur = getLivenessState(); saveJson(LIVENESS_KEY, { ...cur, faceOk: true, livenessStatus: cur.livenessStatus === "verified" ? "verified" : cur.livenessStatus || "none", }); try { localStorage.setItem(FACE_OK_KEY, "1"); } catch { /* ignore */ } bump(); } /** Avatar without a detected face — clear local face flag (dating hides). */ export function clearFaceOk(): void { const cur = getLivenessState(); if (cur.livenessStatus === "verified") { // Keep verified liveness; still mark face presence false for dating avatar gate } saveJson(LIVENESS_KEY, { ...cur, faceOk: false, }); try { localStorage.removeItem(FACE_OK_KEY); } catch { /* ignore */ } bump(); } export function markLivenessVerified(opts?: { videoUrl?: string }): void { const cur = getLivenessState(); // Live frames are audit-only — do NOT invent faceOk / album avatar saveJson(LIVENESS_KEY, { ...cur, livenessStatus: "verified", verifiedAt: new Date().toISOString(), videoUrl: opts?.videoUrl || "", }); bump(); } /** Paid + profile(avatar) + live verify — unlock chat / AV / dating / coffee. */ export function isCommunityUnlocked(): boolean { try { const paid = localStorage.getItem("social-join-paid"); if (paid !== "1" && paid !== "free") return false; if (localStorage.getItem("social-join-profile-done") !== "1") return false; } catch { return false; } if (!isLivenessVerified()) return false; const roster = loadJson<{ avatar?: string }>(ROSTER_KEY, {}); return Boolean((roster.avatar || "").trim()); } export function getCommunityUnlockSnapshot(): string { return isCommunityUnlocked() ? "1" : "0"; } export function getCommunityUnlockServerSnapshot(): string { return "0"; } export function isLivenessVerified(): boolean { return getLivenessState().livenessStatus === "verified"; } const LIVENESS_SERVER_SNAP = JSON.stringify({ faceOk: false, livenessStatus: "none", } satisfies LivenessState); /** SSR-safe: getServerSnapshot is always unverified; client snaps after hydrate. */ export function subscribeLivenessState(cb: () => void): () => void { if (typeof window === "undefined") return () => undefined; window.addEventListener("nomadro-social-storage", cb); window.addEventListener("storage", cb); return () => { window.removeEventListener("nomadro-social-storage", cb); window.removeEventListener("storage", cb); }; } export function getLivenessSnapshot(): string { return JSON.stringify(getLivenessState()); } export function getLivenessServerSnapshot(): string { return LIVENESS_SERVER_SNAP; } export function isFaceOk(): boolean { const s = getLivenessState(); if (s.faceOk) return true; try { return localStorage.getItem(FACE_OK_KEY) === "1"; } catch { return false; } } /** 换邮箱 / 退出时清空本机人脸与核验,禁止串号到下一位访客。 */ export function clearLivenessState(): void { saveJson(LIVENESS_KEY, { ...EMPTY }); try { localStorage.removeItem(FACE_OK_KEY); } catch { /* ignore */ } bump(); } /** * Locked until live selfie passes: * 群聊 / 语音视频 / 约会 / 咖啡聊 / 地图 / 「一起做事」下拉相关。 * 含远程协作:未注册无权限,提示登录 / 注册 / 补充资料。 */ export const LIVENESS_GATED_PATHS = [ "/social/chat", "/social/date", "/social/coffeechat", "/social/meetup", "/social/map", "/social/team", "/social/skills", "/social/work", "/social/ebook", "/social/feedback", ] as const; /** Nav group ids that require liveness (一起做事 + 含群聊/视频的认识人项). */ export const LIVENESS_GATED_NAV_HREFS = new Set([ "/chat", "/date", "/coffeechat", "/meetup", "/map", "/team", "/skills", "/work", "/ebook", "/feedback", ]); export function pathNeedsLiveness(pathname: string): boolean { const p = pathname.replace(/\/$/, "") || "/"; // Also match bare /chat when middleware rewrites / → /social const bare = p.startsWith("/social") ? p : `/social${p === "/" ? "" : p}`; return LIVENESS_GATED_PATHS.some( (g) => bare === g || bare.startsWith(`${g}/`) || p === g || p.startsWith(`${g}/`), ); } export function hrefNeedsLiveness(href: string): boolean { const h = (href || "").split("?")[0].replace(/\/$/, "") || "/"; const path = h.startsWith("/social") ? h.slice("/social".length) || "/" : h; return LIVENESS_GATED_NAV_HREFS.has(path) || LIVENESS_GATED_NAV_HREFS.has(h); }