nomadro/apps/api/services/social_svc/routers/api.py
Eric 4789785f11 Ship remaining business changes across social API, miniprogram audit/book, VIP track, and deploy.
Includes social realtime entrypoints, weapp audit book surfaces, VIP analytics track routes, MiroTalk/Centrifugo deploy helpers, and related admin/config wiring. Excludes local DB dumps and .tmp probe scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-01 17:10:10 +08:00

160 lines
4.3 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.

"""游民社区 API — 前缀 /api/social/v1,与 /api/v1 社群隔离。"""
from __future__ import annotations
from fastapi import APIRouter, Header, HTTPException, Query
from pydantic import BaseModel, Field
from social_svc import store
from social_svc.config import SOCIAL_ENABLED
from social_svc.realtime import issue_connection_token, publish_chat_message
router = APIRouter(tags=["social"])
def _guest(x_social_guest_id: str | None) -> str:
gid = (x_social_guest_id or "").strip()
if not gid or len(gid) > 80:
raise HTTPException(status_code=400, detail="missing X-Social-Guest-Id")
return gid
class MessageIn(BaseModel):
author: str = Field(default="游民", max_length=48)
text: str = Field(min_length=1, max_length=2000)
class ReviewIn(BaseModel):
author: str = Field(default="游民", max_length=48)
score: int = Field(default=5, ge=1, le=5)
text: str = Field(min_length=1, max_length=2000)
photoNote: str = Field(default="", max_length=200)
class RosterIn(BaseModel):
name: str = Field(default="", max_length=64)
title: str = Field(default="", max_length=80)
city: str = Field(default="", max_length=64)
phone: str = Field(default="", max_length=32)
linktree: str = Field(default="", max_length=240)
skills: str = Field(default="", max_length=200)
bio: str = Field(default="", max_length=2000)
class FeedbackIn(BaseModel):
kind: str = Field(default="建议", max_length=8)
text: str = Field(min_length=1, max_length=4000)
contact: str = Field(default="", max_length=120)
class GigAppIn(BaseModel):
name: str = Field(min_length=1, max_length=64)
contact: str = Field(min_length=1, max_length=120)
note: str = Field(default="", max_length=2000)
@router.get("/health")
async def social_health():
if not SOCIAL_ENABLED:
return {"ok": False, "module": "social", "enabled": False}
h = store.health()
from social_svc.config import CENTRIFUGO_ENABLED
h["centrifugo"] = bool(CENTRIFUGO_ENABLED)
return h
@router.get("/realtime/token")
async def realtime_token(
channel_id: str = Query(default="lobby"),
x_social_guest_id: str | None = Header(default=None),
):
guest = _guest(x_social_guest_id)
return issue_connection_token(guest, channel_id)
@router.get("/channels/{channel_id}/messages")
async def get_messages(channel_id: str, limit: int = 100):
store.init_db()
return {"items": store.list_messages(channel_id, limit)}
@router.post("/channels/{channel_id}/messages")
async def post_message(
channel_id: str,
body: MessageIn,
x_social_guest_id: str | None = Header(default=None),
):
guest = _guest(x_social_guest_id)
store.init_db()
if not body.text.strip():
raise HTTPException(status_code=400, detail="empty text")
saved = store.add_message(channel_id, body.author, body.text, guest)
# 实时推送失败不影响落库成功
try:
await publish_chat_message(channel_id, saved)
except Exception:
pass
return saved
@router.get("/cafes/{cafe_id}/reviews")
async def get_reviews(cafe_id: str, limit: int = 50):
store.init_db()
return {"items": store.list_reviews(cafe_id, limit)}
@router.post("/cafes/{cafe_id}/reviews")
async def post_review(
cafe_id: str,
body: ReviewIn,
x_social_guest_id: str | None = Header(default=None),
):
guest = _guest(x_social_guest_id)
store.init_db()
return store.add_review(
cafe_id,
body.author,
body.score,
body.text,
body.photoNote,
guest,
)
@router.get("/roster/me")
async def get_my_roster(x_social_guest_id: str | None = Header(default=None)):
guest = _guest(x_social_guest_id)
store.init_db()
return {"item": store.get_roster(guest)}
@router.put("/roster/me")
async def put_my_roster(
body: RosterIn,
x_social_guest_id: str | None = Header(default=None),
):
guest = _guest(x_social_guest_id)
store.init_db()
return store.upsert_roster(guest, body.model_dump())
@router.post("/feedback")
async def post_feedback(
body: FeedbackIn,
x_social_guest_id: str | None = Header(default=None),
):
guest = _guest(x_social_guest_id)
store.init_db()
return store.add_feedback(body.kind, body.text, body.contact, guest)
@router.post("/gigs/{gig_id}/apply")
async def post_gig_apply(
gig_id: str,
body: GigAppIn,
x_social_guest_id: str | None = Header(default=None),
):
guest = _guest(x_social_guest_id)
store.init_db()
return store.add_gig_app(gig_id, body.name, body.contact, body.note, guest)