# retoor <retoor@molodetz.nl>

from .._shared import endpoint, field

GROUP = {
        "slug": "push",
        "title": "Web Push",
        "intro": """
# Web Push

Push notifications are delivered by one or more providers. `webpush` is the default and
implements the Web Push protocol: fetch the public VAPID key, then register a
`PushSubscription` obtained from the browser's `PushManager`. `apns` delivers to an Apple
Push Notification service device token and is only offered when an administrator has
configured it.

`GET /push.json` lists the providers that currently accept registrations. A registration
body without a `provider` field is a `webpush` registration, so existing clients need no
change.

There is no server-side unsubscribe endpoint: unsubscription is handled entirely in the
browser by calling `PushManager.unsubscribe()` on the subscription. The server stops delivering
to a subscription once its push endpoint reports it as gone. These mirror the in-app
[Notifications](/docs/notifications.html) feed.

Every endpoint follows the shared [Conventions & Errors](/docs/conventions.html) (auth, content
negotiation, pagination, status codes); see [Authentication](/docs/authentication.html) for the
four ways to sign requests.
""",
        "endpoints": [
            endpoint(
                id="push-key",
                method="GET",
                path="/push.json",
                title="Get the public key",
                summary="Return the VAPID public key and the providers that accept registrations.",
                auth="public",
                sample_response={
                    "publicKey": "BASE64_VAPID_KEY",
                    "providers": {"webpush": {"publicKey": "BASE64_VAPID_KEY"}},
                },
            ),
            endpoint(
                id="push-register",
                method="POST",
                path="/push.json",
                title="Register a subscription",
                summary="Register a push subscription. Sends a welcome notification.",
                auth="user",
                encoding="json",
                interactive=False,
                params=[
                    field(
                        "provider",
                        "json",
                        "string",
                        False,
                        "webpush",
                        "Provider to register with. Omit for webpush.",
                    ),
                    field(
                        "endpoint",
                        "json",
                        "string",
                        False,
                        "https://fcm.googleapis.com/...",
                        "Subscription endpoint URL. Required for webpush.",
                    ),
                    field(
                        "keys",
                        "json",
                        "string",
                        False,
                        '{"p256dh":"...","auth":"..."}',
                        "Subscription keys object. Required for webpush.",
                    ),
                    field(
                        "token",
                        "json",
                        "string",
                        False,
                        "a1b2c3...",
                        "Hexadecimal device token. Required for apns.",
                    ),
                ],
                notes=[
                    'A webpush body is JSON: `{"endpoint": "...", "keys": {"p256dh": "...", "auth": "..."}}`.',
                    'An APNs body is JSON: `{"provider": "apns", "token": "..."}`.',
                    "A provider that is unknown, disabled or unconfigured returns 400.",
                ],
                sample_response={"registered": True},
            ),
        ],
}
