|
| 1 | +"""Push target registration — APNs devices (native app) and Web Push |
| 2 | +subscriptions (installed PWA). Both are per-user and idempotent.""" |
| 3 | + |
| 4 | +from fastapi import APIRouter |
| 5 | +from pydantic import BaseModel, Field |
| 6 | +from sqlalchemy import delete, select |
| 7 | + |
| 8 | +from app.core.deps import DB, CurrentUser |
| 9 | +from app.models import Device, PushSubscription |
| 10 | +from app.services.push import get_vapid |
| 11 | + |
| 12 | +router = APIRouter(prefix="/push", tags=["push"]) |
| 13 | + |
| 14 | + |
| 15 | +@router.get("/vapid-public-key") |
| 16 | +async def vapid_public_key(user: CurrentUser) -> dict: |
| 17 | + """The applicationServerKey the PWA uses to subscribe.""" |
| 18 | + return {"public_key": get_vapid()["public_key"]} |
| 19 | + |
| 20 | + |
| 21 | +class DeviceIn(BaseModel): |
| 22 | + token: str = Field(min_length=1, max_length=200) |
| 23 | + # Xcode debug builds get sandbox APNs tokens; TestFlight/App Store don't. |
| 24 | + sandbox: bool = False |
| 25 | + |
| 26 | + |
| 27 | +@router.post("/devices", status_code=204) |
| 28 | +async def register_device(payload: DeviceIn, user: CurrentUser, db: DB) -> None: |
| 29 | + existing = ( |
| 30 | + await db.execute(select(Device).where(Device.token == payload.token)) |
| 31 | + ).scalar_one_or_none() |
| 32 | + if existing is None: |
| 33 | + db.add(Device(user_id=user.id, token=payload.token, sandbox=payload.sandbox)) |
| 34 | + else: |
| 35 | + # Token moved to another account (device signed in as someone else). |
| 36 | + existing.user_id = user.id |
| 37 | + existing.sandbox = payload.sandbox |
| 38 | + |
| 39 | + |
| 40 | +@router.delete("/devices/{token}", status_code=204) |
| 41 | +async def unregister_device(token: str, user: CurrentUser, db: DB) -> None: |
| 42 | + await db.execute( |
| 43 | + delete(Device).where(Device.token == token, Device.user_id == user.id) |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +class SubscriptionKeys(BaseModel): |
| 48 | + p256dh: str = Field(min_length=1, max_length=200) |
| 49 | + auth: str = Field(min_length=1, max_length=100) |
| 50 | + |
| 51 | + |
| 52 | +class SubscriptionIn(BaseModel): |
| 53 | + endpoint: str = Field(min_length=1, max_length=1024) |
| 54 | + keys: SubscriptionKeys |
| 55 | + |
| 56 | + |
| 57 | +@router.post("/subscriptions", status_code=204) |
| 58 | +async def register_subscription( |
| 59 | + payload: SubscriptionIn, user: CurrentUser, db: DB |
| 60 | +) -> None: |
| 61 | + existing = ( |
| 62 | + await db.execute( |
| 63 | + select(PushSubscription).where( |
| 64 | + PushSubscription.endpoint == payload.endpoint |
| 65 | + ) |
| 66 | + ) |
| 67 | + ).scalar_one_or_none() |
| 68 | + if existing is None: |
| 69 | + db.add( |
| 70 | + PushSubscription( |
| 71 | + user_id=user.id, |
| 72 | + endpoint=payload.endpoint, |
| 73 | + p256dh=payload.keys.p256dh, |
| 74 | + auth=payload.keys.auth, |
| 75 | + ) |
| 76 | + ) |
| 77 | + else: |
| 78 | + existing.user_id = user.id |
| 79 | + existing.p256dh = payload.keys.p256dh |
| 80 | + existing.auth = payload.keys.auth |
| 81 | + |
| 82 | + |
| 83 | +class SubscriptionDelete(BaseModel): |
| 84 | + endpoint: str = Field(min_length=1, max_length=1024) |
| 85 | + |
| 86 | + |
| 87 | +@router.post("/subscriptions/delete", status_code=204) |
| 88 | +async def unregister_subscription( |
| 89 | + payload: SubscriptionDelete, user: CurrentUser, db: DB |
| 90 | +) -> None: |
| 91 | + await db.execute( |
| 92 | + delete(PushSubscription).where( |
| 93 | + PushSubscription.endpoint == payload.endpoint, |
| 94 | + PushSubscription.user_id == user.id, |
| 95 | + ) |
| 96 | + ) |
0 commit comments