nomadro/apps/api/services/ebook_svc/routers/memo_admin.py
Eric 3f7528f824 Add VIP knowledge community and device shop with native weapp UX.
Document memo/shop admin routes, deploy scripts, and cache-first feed performance in README.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-06 16:58:17 +08:00

145 lines
4.3 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
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
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
@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 = "",
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)
return {"ok": True, "items": items}
@router.post("/memos")
async def admin_post(body: AdminPostBody, _: str = Depends(require_admin)) -> dict[str, Any]:
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,
resources=body.resources or [],
)
except ValueError as e:
raise HTTPException(400, str(e)) from e
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}/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.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}