from __future__ import annotations from fastapi import APIRouter, Depends, HTTPException from fastapi.responses import JSONResponse from server.auth import require_auth from server.db import get_db from server.push import save_subscription from server.schemas import PushSubscribe router = APIRouter(prefix="/api/push", tags=["push"]) NO_STORE = {"Cache-Control": "no-store"} @router.get("/vapid-public-key") async def vapid_public_key() -> JSONResponse: from server.settings import settings return JSONResponse({"key": settings.vapid_public_key}, headers=NO_STORE) @router.post("/subscribe") async def push_subscribe(payload: PushSubscribe, _: dict = Depends(require_auth)) -> JSONResponse: if not payload.subscription or not payload.subscription.get("endpoint"): raise HTTPException(status_code=400, detail="Invalid subscription") db = await get_db() try: await save_subscription(db, payload.subscription) finally: await db.close() return JSONResponse({"ok": True}, headers=NO_STORE)