Add feedback form and webhook
This commit is contained in:
38
backend/app/routers/feedback.py
Normal file
38
backend/app/routers/feedback.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from typing import Any, Dict
|
||||
import httpx
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from ..auth import get_current_user
|
||||
from ..runtime import get_runtime_settings
|
||||
|
||||
router = APIRouter(prefix="/feedback", tags=["feedback"], dependencies=[Depends(get_current_user)])
|
||||
|
||||
|
||||
@router.post("")
|
||||
async def send_feedback(payload: Dict[str, Any], user: Dict[str, str] = Depends(get_current_user)) -> dict:
|
||||
runtime = get_runtime_settings()
|
||||
webhook_url = runtime.discord_webhook_url
|
||||
if not webhook_url:
|
||||
raise HTTPException(status_code=400, detail="Discord webhook not configured")
|
||||
|
||||
feedback_type = str(payload.get("type") or "").strip().lower()
|
||||
if feedback_type not in {"bug", "feature"}:
|
||||
raise HTTPException(status_code=400, detail="Invalid feedback type")
|
||||
|
||||
message = str(payload.get("message") or "").strip()
|
||||
if not message:
|
||||
raise HTTPException(status_code=400, detail="Message is required")
|
||||
if len(message) > 2000:
|
||||
raise HTTPException(status_code=400, detail="Message is too long")
|
||||
|
||||
username = user.get("username") or "unknown"
|
||||
content = f"**{feedback_type.title()}** from **{username}**\n{message}"
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=10.0) as client:
|
||||
response = await client.post(webhook_url, json={"content": content})
|
||||
response.raise_for_status()
|
||||
except httpx.HTTPStatusError as exc:
|
||||
raise HTTPException(status_code=502, detail=str(exc)) from exc
|
||||
|
||||
return {"status": "ok"}
|
||||
Reference in New Issue
Block a user