"""分享頁 /c//:有 og:image,貼到 Discord、X、LINE 會出現小卡預覽。""" import sqlite3 from html import escape from fastapi import APIRouter, Depends from fastapi.responses import HTMLResponse from . import config from .card import DAY_RE from .checkin import stats from .db import get_db from .deps import fail from .i18n import norm_lang, t from .titles import fmt_uid router = APIRouter() PAGE = """ {title} · {site} {title} {open} """ @router.get("/c/{uid}/{day}", response_class=HTMLResponse) def share_page(uid: int, day: str, conn: sqlite3.Connection = Depends(get_db)): if not DAY_RE.match(day): fail(404, "not_found") user = conn.execute("SELECT * FROM users WHERE uid = ?", (uid,)).fetchone() if user is None or conn.execute( "SELECT 1 FROM checkins WHERE uid = ? AND day = ?", (uid, day)).fetchone() is None: fail(404, "not_found") lang = norm_lang(user["lang"]) total, _ = stats(conn, uid, day) image_path = f"/card/{fmt_uid(uid)}/{day}.png" return PAGE.format( lang=lang, site="My INMU (Beta)" if config.BETA else "My INMU", title=escape(t(lang, "share.title", name=user["display_name"])), desc=escape(t(lang, "share.text", n=total)), image=escape(config.PUBLIC_URL + image_path), image_path=image_path, url=escape(f"{config.PUBLIC_URL}/c/{fmt_uid(uid)}/{day}"), open=escape(t(lang, "share.open")), )