Private
Public Access
Backend: PushSubscription model/migration, VAPID config + `cli.py generate-vapid-keys`, push_service.send_push_to_user (upsert-by-endpoint subscribe/unsubscribe, auto-cleanup of expired 404/410 subscriptions), /api/push/* router, and ConnectionManager now tracks connected user IDs per room so chat.py can push only to offline members after broadcasting to online ones. Two test-infra bugs found and fixed along the way: send_push_to_user takes the caller's AsyncSession and is awaited inline rather than fired via asyncio.create_task with its own session (background tasks were outliving the test event loop); and the ws_client fixture now uses NullPool to eliminate a connection-pool checkout race that was failing WS tests intermittently. Frontend: service worker rebuilt with vite-plugin-pwa's injectManifest strategy (custom src/sw.ts) so it can add push/notificationclick handlers alongside the existing precaching and StaleWhileRevalidate routes ported over from generateSW. New subscribe/unsubscribe flow (lib/push.ts, api/push.ts) with a toggle in the account menu. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
from fastapi import APIRouter, Depends, Response
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.config import settings
|
|
from app.database import get_db
|
|
from app.dependencies import get_current_user
|
|
from app.models import User
|
|
from app.schemas.push import (
|
|
PushSubscriptionCreate,
|
|
PushUnsubscribeRequest,
|
|
VapidPublicKeyRead,
|
|
)
|
|
from app.services.push_service import subscribe, unsubscribe
|
|
|
|
router = APIRouter(prefix="/api/push", tags=["push"])
|
|
|
|
|
|
@router.get("/vapid-public-key", response_model=VapidPublicKeyRead)
|
|
async def get_vapid_public_key(current_user: User = Depends(get_current_user)):
|
|
return VapidPublicKeyRead(public_key=settings.vapid_public_key)
|
|
|
|
|
|
@router.post("/subscribe", status_code=204)
|
|
async def subscribe_endpoint(
|
|
data: PushSubscriptionCreate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> Response:
|
|
await subscribe(db, current_user.id, data)
|
|
return Response(status_code=204)
|
|
|
|
|
|
@router.delete("/subscribe", status_code=204)
|
|
async def unsubscribe_endpoint(
|
|
data: PushUnsubscribeRequest,
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
) -> Response:
|
|
await unsubscribe(db, current_user.id, data.endpoint)
|
|
return Response(status_code=204)
|