fix: use host network and restore web chat records

This commit is contained in:
ngfchl
2026-07-06 19:44:23 +08:00
parent 251b926b6f
commit 04781f144c
7 changed files with 329 additions and 40 deletions
+23 -2
View File
@@ -62,16 +62,36 @@ async def api_stats(day: str = Query(default=None, pattern=r"^\d{4}-\d{2}-\d{2}$
return JSONResponse(await get_daily_stats(day))
def normalize_message(row: dict) -> dict:
row = dict(row)
row["time"] = row.get("created_at")
row["spam"] = row.get("is_spam", False)
return row
def normalize_ban(row: dict) -> dict:
row = dict(row)
row["time"] = row.get("created_at")
return row
@app.get("/api/messages")
async def api_messages(limit: int = Query(default=50, ge=1, le=500)):
msgs = await Message.all().order_by("-created_at").limit(limit).values()
return JSONResponse(json_safe(list(msgs)))
return JSONResponse(json_safe([normalize_message(m) for m in msgs]))
@app.get("/api/chat")
async def api_chat(limit: int = Query(default=100, ge=1, le=500)):
"""兼容旧前端:返回聊天记录。"""
msgs = await Message.all().order_by("-created_at").limit(limit).values()
return JSONResponse(json_safe([normalize_message(m) for m in msgs]))
@app.get("/api/bans")
async def api_bans(limit: int = Query(default=100, ge=1, le=500)):
bans = await Ban.all().order_by("-created_at").limit(limit).values()
return JSONResponse(json_safe(list(bans)))
return JSONResponse(json_safe([normalize_ban(b) for b in bans]))
@app.get("/api/actions")
@@ -111,6 +131,7 @@ async def api_stream(request: Request):
async def event_generator():
try:
yield {"event": "connected", "data": "ok"}
while True:
if await request.is_disconnected():
break