(ctx context.Context, req codersdk.WebpushSubscription)
| 427 | } |
| 428 | |
| 429 | func (n *Webpusher) Test(ctx context.Context, req codersdk.WebpushSubscription) error { |
| 430 | msgJSON, err := json.Marshal(codersdk.WebpushMessage{ |
| 431 | Title: "It's working!", |
| 432 | Body: "You've subscribed to push notifications.", |
| 433 | }) |
| 434 | if err != nil { |
| 435 | return xerrors.Errorf("marshal webpush notification: %w", err) |
| 436 | } |
| 437 | statusCode, body, err := n.webpushSend(ctx, msgJSON, req.Endpoint, webpush.Keys{ |
| 438 | Auth: req.AuthKey, |
| 439 | P256dh: req.P256DHKey, |
| 440 | }) |
| 441 | if err != nil { |
| 442 | return xerrors.Errorf("send test webpush notification: %w", err) |
| 443 | } |
| 444 | |
| 445 | // 200, 201, and 202 are common for successful delivery. |
| 446 | if statusCode > http.StatusAccepted { |
| 447 | // It's likely the subscription failed to deliver for some reason. |
| 448 | return xerrors.Errorf("web push dispatch failed with status code %d: %s", statusCode, string(body)) |
| 449 | } |
| 450 | |
| 451 | return nil |
| 452 | } |
| 453 | |
| 454 | // PublicKey returns the VAPID public key for the webpush dispatcher. |
| 455 | // Clients need this, so it's exposed via the BuildInfo endpoint. |
nothing calls this directly
no test coverage detected