nomadro/apps/api/services/ebook_svc/routers/social_ugc_admin.py
Eric b06a14b6aa Ship social admin nested menus with list/detail CRUD, soft-archive UGC, and PocketBase-ready stores.
admin.nomadro.com gets sidebar L1/L2 social ops (paginated lists + entity detail); API adds soft-delete archives/stats and shared nomadro_pb for social/coffee/notify/work.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-04 16:41:43 +08:00

244 lines
8.4 KiB
Python
Raw Permalink 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.

"""微信后台 · 游民社区 UGC 增删查改"""
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 import social_ugc as ugc
router = APIRouter(prefix="/api/admin/social/ugc", tags=["social-ugc-admin"])
class RosterPatch(BaseModel):
name: str | None = None
title: str | None = None
city: str | None = None
phone: str | None = None
linktree: str | None = None
skills: str | None = None
bio: str | None = None
restore: bool | None = None
status: str | None = None
@router.get("/overview")
async def get_overview(_: str = Depends(require_admin)) -> dict[str, Any]:
return ugc.overview()
@router.get("/stats")
async def get_stats(_: str = Depends(require_admin)) -> dict[str, Any]:
"""Dashboard visualization payload (counts, city bars, day series, soft-delete)."""
try:
return ugc.stats()
except Exception as e:
raise HTTPException(503, f"social stats unavailable: {e}") from e
@router.get("/archives")
async def get_archives(
entity: str = "",
limit: int = Query(100, ge=1, le=500),
offset: int = Query(0, ge=0),
_: str = Depends(require_admin),
) -> dict[str, Any]:
try:
return ugc.list_archives(entity=entity, limit=limit, offset=offset)
except Exception as e:
raise HTTPException(503, f"archives unavailable: {e}") from e
@router.get("/messages")
async def get_messages(
channelId: str = "",
q: str = "",
limit: int = Query(100, ge=1, le=500),
offset: int = Query(0, ge=0),
_: str = Depends(require_admin),
) -> dict[str, Any]:
try:
return ugc.list_messages(channel_id=channelId, q=q, limit=limit, offset=offset)
except Exception as e:
raise HTTPException(503, f"social db unavailable: {e}") from e
@router.delete("/messages/{message_id}")
async def del_message(message_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
if not ugc.delete_message(message_id):
raise HTTPException(404, "消息不存在")
return {"ok": True, "soft": True, "archived": True}
@router.get("/roster")
async def get_roster(
q: str = "",
limit: int = Query(100, ge=1, le=500),
offset: int = Query(0, ge=0),
_: str = Depends(require_admin),
) -> dict[str, Any]:
try:
return ugc.list_roster(q=q, limit=limit, offset=offset)
except Exception as e:
raise HTTPException(503, f"social db unavailable: {e}") from e
@router.put("/roster/{guest_id}")
async def put_roster(
guest_id: str, body: RosterPatch, _: str = Depends(require_admin)
) -> dict[str, Any]:
item = ugc.update_roster(guest_id, body.model_dump(exclude_none=True))
if not item:
raise HTTPException(404, "介绍不存在")
return {"ok": True, "item": item}
@router.delete("/roster/{guest_id}")
async def del_roster(guest_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
if not ugc.delete_roster(guest_id):
raise HTTPException(404, "介绍不存在")
return {"ok": True, "soft": True, "archived": True}
@router.get("/reviews")
async def get_reviews(
cafeId: str = "",
limit: int = Query(100, ge=1, le=500),
offset: int = Query(0, ge=0),
_: str = Depends(require_admin),
) -> dict[str, Any]:
try:
return ugc.list_reviews(cafe_id=cafeId, limit=limit, offset=offset)
except Exception as e:
raise HTTPException(503, f"social db unavailable: {e}") from e
@router.delete("/reviews/{review_id}")
async def del_review(review_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
if not ugc.delete_review(review_id):
raise HTTPException(404, "评价不存在")
return {"ok": True, "soft": True, "archived": True}
@router.get("/gig-apps")
async def get_gig_apps(
gigId: str = "",
limit: int = Query(100, ge=1, le=500),
offset: int = Query(0, ge=0),
_: str = Depends(require_admin),
) -> dict[str, Any]:
try:
return ugc.list_gig_apps(gig_id=gigId, limit=limit, offset=offset)
except Exception as e:
raise HTTPException(503, f"social db unavailable: {e}") from e
@router.delete("/gig-apps/{app_id}")
async def del_gig_app(app_id: str, _: str = Depends(require_admin)) -> dict[str, Any]:
if not ugc.delete_gig_app(app_id):
raise HTTPException(404, "申请不存在")
return {"ok": True, "soft": True, "archived": True}
@router.get("/feedback")
async def get_feedback(
limit: int = Query(100, ge=1, le=500),
offset: int = Query(0, ge=0),
_: str = Depends(require_admin),
) -> dict[str, Any]:
try:
return ugc.list_feedback(limit=limit, offset=offset)
except Exception as e:
raise HTTPException(503, f"social db unavailable: {e}") from e
@router.delete("/feedback/{fid}")
async def del_feedback(fid: str, _: str = Depends(require_admin)) -> dict[str, Any]:
if not ugc.delete_feedback(fid):
raise HTTPException(404, "反馈不存在")
return {"ok": True, "soft": True, "archived": True}
class CatalogBody(BaseModel):
meetups: list[dict[str, Any]] | None = None
gigs: list[dict[str, Any]] | None = None
cafes: list[dict[str, Any]] | None = None
roster: list[dict[str, Any]] | None = None
ebooks: list[dict[str, Any]] | None = None
videos: list[dict[str, Any]] | None = None
@router.get("/catalog")
async def get_catalog(_: str = Depends(require_admin)) -> dict[str, Any]:
from ebook_svc.services.social_ops import _read_raw
raw = _read_raw()
copy = raw.get("copy") if isinstance(raw.get("copy"), dict) else {}
catalogs = copy.get("socialCatalogs") if isinstance(copy.get("socialCatalogs"), dict) else {}
return {
"ok": True,
"version": raw.get("version"),
"catalogs": {
"meetups": catalogs.get("meetups") if isinstance(catalogs.get("meetups"), list) else [],
"gigs": catalogs.get("gigs") if isinstance(catalogs.get("gigs"), list) else [],
"cafes": catalogs.get("cafes") if isinstance(catalogs.get("cafes"), list) else [],
"roster": catalogs.get("roster") if isinstance(catalogs.get("roster"), list) else [],
"ebooks": catalogs.get("ebooks") if isinstance(catalogs.get("ebooks"), list) else [],
"videos": catalogs.get("videos") if isinstance(catalogs.get("videos"), list) else [],
},
}
@router.put("/catalog")
async def put_catalog(body: CatalogBody, _: str = Depends(require_admin)) -> dict[str, Any]:
"""深合并 socialCatalogs 数组键;不碰其它配置。"""
from ebook_svc.services.social_ops import _read_raw, _write_raw
raw = _read_raw()
out = dict(raw) if raw else {"version": 1, "modules": {}, "copy": {}}
copy = dict(out.get("copy") or {})
catalogs = dict(copy.get("socialCatalogs") or {}) if isinstance(copy.get("socialCatalogs"), dict) else {}
patch = body.model_dump(exclude_none=True)
for key, val in patch.items():
if isinstance(val, list):
catalogs[key] = val
copy["socialCatalogs"] = catalogs
out["copy"] = copy
_write_raw(out)
return {
"ok": True,
"version": out.get("version"),
"catalogs": {
"meetups": catalogs.get("meetups") if isinstance(catalogs.get("meetups"), list) else [],
"gigs": catalogs.get("gigs") if isinstance(catalogs.get("gigs"), list) else [],
"cafes": catalogs.get("cafes") if isinstance(catalogs.get("cafes"), list) else [],
"roster": catalogs.get("roster") if isinstance(catalogs.get("roster"), list) else [],
"ebooks": catalogs.get("ebooks") if isinstance(catalogs.get("ebooks"), list) else [],
"videos": catalogs.get("videos") if isinstance(catalogs.get("videos"), list) else [],
},
}
@router.get("/detail/{entity}/{entity_id}")
async def get_entity_detail(
entity: str, entity_id: str, _: str = Depends(require_admin)
) -> dict[str, Any]:
"""Detail page: live record + archive history. Soft-delete never erases payload."""
allowed = {
"roster",
"messages",
"reviews",
"gigApps",
"feedback",
"skills",
"liveness",
}
if entity not in allowed:
raise HTTPException(404, "unknown entity")
item = ugc.get_detail(entity, entity_id)
if not item:
raise HTTPException(404, "记录不存在(或仅存于存档,请查 /archives)")
return item