cosmo_backend/app/api/danmaku.py

48 lines
1.4 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession
from pydantic import BaseModel, constr
from typing import List
from app.database import get_db
from app.models.db import User
from app.services.auth_deps import get_current_user
from app.services.danmaku_service import danmaku_service
router = APIRouter(prefix="/danmaku", tags=["danmaku"])
class DanmakuCreate(BaseModel):
text: constr(max_length=20, min_length=1)
class DanmakuResponse(BaseModel):
id: str
uid: str
username: str
text: str
ts: float
@router.post("/send", response_model=DanmakuResponse)
async def send_danmaku(
data: DanmakuCreate,
current_user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db)
):
"""Send a short danmaku message (max 20 chars)"""
try:
result = await danmaku_service.add_danmaku(
user_id=current_user.id,
username=current_user.username,
text=data.text,
db=db
)
return result
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/list", response_model=List[DanmakuResponse])
async def get_danmaku_list(
db: AsyncSession = Depends(get_db)
):
"""Get all active danmaku messages"""
# This endpoint is public (or could be protected if needed)
return await danmaku_service.get_active_danmaku(db)