nomadro/apps/api/services/ebook_svc/routers/memo_admin.py
Eric 4389ebe6cb Ship WeChat audit release 2026-11-02: realtime config, audit vip-ebook shell, admin ops.
Document test9 tag 2026-11-02 for upload; POST config pull + WS, fixed dark home, no debug bar.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-02 16:12:57 +08:00

230 lines
6.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""知识社群管理:admin.nomadro.cn/wechat/memo — 含试读配置。"""
from __future__ import annotations
from typing import Any
from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel, Field
from ebook_svc.core.security import require_admin
from ebook_svc.services.memo_store import memo_store
router = APIRouter(prefix="/api/admin/memo", tags=["memo-admin"])
class AdminPostBody(BaseModel):
content: str = ""
title: str = ""
nickname: str = "博主"
pinned: bool = False
featured: bool = False
# None → 跟随 settings.defaultTrial
trial: bool | None = None
visibility: str | None = None
resources: list[dict[str, Any]] = Field(default_factory=list)
class SettingsBody(BaseModel):
enabled: bool | None = None
canPost: bool | None = None
canComment: bool | None = None
canLike: bool | None = None
canFavorite: bool | None = None
canUpload: bool | None = None
adminOnlyPost: bool | None = None
requireVip: bool | None = None
allowTrialBrowse: bool | None = None
defaultTrial: bool | None = None
trialComments: bool | None = None
maxContentLen: int | None = Field(default=None, ge=100, le=50000)
maxImages: int | None = Field(default=None, ge=1, le=18)
planetName: str | None = None
planetDesc: str | None = None
notice: str | None = None
class PinBody(BaseModel):
pinned: bool = True
class FeaturedBody(BaseModel):
featured: bool = True
class VisibilityBody(BaseModel):
"""trial=试读(未付费可读全文);vip=会员专享。"""
trial: bool | None = None
visibility: str | None = None
@router.get("")
async def overview(_: str = Depends(require_admin)) -> dict[str, Any]:
settings = memo_store.get_settings()
stats = memo_store.stats()
recent = memo_store.list_memos(limit=30, include_hidden=True)
return {"ok": True, "settings": settings, "stats": stats, "recent": recent}
@router.get("/settings")
async def get_settings(_: str = Depends(require_admin)) -> dict[str, Any]:
return {"ok": True, **memo_store.get_settings()}
@router.put("/settings")
async def put_settings(body: SettingsBody, _: str = Depends(require_admin)) -> dict[str, Any]:
saved = memo_store.save_settings(body.model_dump(exclude_none=True))
return {"ok": True, **saved}
@router.get("/memos")
async def list_memos(
q: str = "",
visibility: str = "",
limit: int = Query(100, ge=1, le=200),
_: str = Depends(require_admin),
) -> dict[str, Any]:
items = memo_store.list_memos(
limit=limit,
include_hidden=True,
q=q,
visibility=visibility,
)
return {"ok": True, "items": items}
@router.post("/memos")
async def admin_post(body: AdminPostBody, _: str = Depends(require_admin)) -> dict[str, Any]:
s = memo_store.get_settings()
if body.trial is True:
vis = "trial"
elif body.trial is False:
vis = "vip"
elif body.visibility:
vis = body.visibility
else:
vis = "trial" if s.get("defaultTrial", True) else "vip"
try:
memo = memo_store.create_memo(
openid="__admin__",
content=body.content or "",
title=body.title or "",
nickname=body.nickname or "博主",
pinned=body.pinned,
featured=body.featured,
visibility=vis,
resources=body.resources or [],
)
except ValueError as e:
raise HTTPException(400, str(e)) from e
return {"ok": True, "memo": memo}
@router.put("/memos/{memo_id}")
async def admin_update_memo(
memo_id: str, body: AdminPostBody, _: str = Depends(require_admin)
) -> dict[str, Any]:
vis = body.visibility
if body.trial is True:
vis = "trial"
elif body.trial is False:
vis = "vip"
try:
memo = memo_store.update_memo(
memo_id,
content=body.content,
title=body.title,
nickname=body.nickname,
visibility=vis,
resources=body.resources,
)
except ValueError as e:
raise HTTPException(400, str(e)) from e
if not memo:
raise HTTPException(404, "不存在")
return {"ok": True, "memo": memo}
@router.delete("/memos/{memo_id}")
async def admin_delete(memo_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
ok = memo_store.delete_memo(memo_id, admin=True)
if not ok:
raise HTTPException(404, "不存在")
return {"ok": True}
@router.post("/memos/{memo_id}/hide")
async def admin_hide(memo_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
memo = memo_store.hide_memo(memo_id)
if not memo:
raise HTTPException(404, "不存在")
return {"ok": True, "memo": memo}
@router.post("/memos/{memo_id}/unhide")
async def admin_unhide(memo_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
try:
memo = memo_store.unhide_memo(memo_id)
except ValueError as e:
raise HTTPException(400, str(e)) from e
if not memo:
raise HTTPException(404, "不存在")
return {"ok": True, "memo": memo}
@router.post("/memos/{memo_id}/pin")
async def admin_pin(
memo_id: str,
body: PinBody,
_: str = Depends(require_admin),
) -> dict[str, Any]:
memo = memo_store.set_pin(memo_id, body.pinned)
if not memo:
raise HTTPException(404, "不存在")
return {"ok": True, "memo": memo}
@router.post("/memos/{memo_id}/featured")
async def admin_featured(
memo_id: str,
body: FeaturedBody,
_: str = Depends(require_admin),
) -> dict[str, Any]:
memo = memo_store.set_featured(memo_id, body.featured)
if not memo:
raise HTTPException(404, "不存在")
return {"ok": True, "memo": memo}
@router.post("/memos/{memo_id}/visibility")
async def admin_visibility(
memo_id: str,
body: VisibilityBody,
_: str = Depends(require_admin),
) -> dict[str, Any]:
if body.trial is True:
vis = "trial"
elif body.trial is False:
vis = "vip"
else:
vis = (body.visibility or "vip").strip().lower()
try:
memo = memo_store.set_visibility(memo_id, vis)
except ValueError as e:
raise HTTPException(400, str(e)) from e
if not memo:
raise HTTPException(404, "不存在")
return {"ok": True, "memo": memo}
@router.get("/memos/{memo_id}/comments")
async def admin_comments(memo_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
return {"ok": True, "items": memo_store.list_comments(memo_id)}
@router.delete("/comments/{comment_id}")
async def admin_del_comment(comment_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
ok = memo_store.delete_comment(comment_id, admin=True)
if not ok:
raise HTTPException(404, "不存在")
return {"ok": True}