@Summary Create user webpush subscription @ID create-user-webpush-subscription @Security CoderSessionToken @Accept json @Tags Notifications @Param request body codersdk.WebpushSubscription true "Webpush subscription" @Param user path string true "User ID, name, or me" @Router /api/v2/users/{user}/we
(rw http.ResponseWriter, r *http.Request)
| 32 | // @Success 204 |
| 33 | // @x-apidocgen {"skip": true} |
| 34 | func (api *API) postUserWebpushSubscription(rw http.ResponseWriter, r *http.Request) { |
| 35 | ctx := r.Context() |
| 36 | user := httpmw.UserParam(r) |
| 37 | var req codersdk.WebpushSubscription |
| 38 | if !httpapi.Read(ctx, rw, r, &req) { |
| 39 | return |
| 40 | } |
| 41 | if err := validateWebpushEndpoint(req.Endpoint); err != nil { |
| 42 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 43 | Message: "Invalid webpush endpoint.", |
| 44 | Detail: err.Error(), |
| 45 | }) |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | if err := api.WebpushDispatcher.Test(ctx, req); err != nil { |
| 50 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 51 | Message: "Failed to test webpush subscription", |
| 52 | Detail: err.Error(), |
| 53 | }) |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | if _, err := api.Database.InsertWebpushSubscription(ctx, database.InsertWebpushSubscriptionParams{ |
| 58 | CreatedAt: dbtime.Now(), |
| 59 | UserID: user.ID, |
| 60 | Endpoint: req.Endpoint, |
| 61 | EndpointAuthKey: req.AuthKey, |
| 62 | EndpointP256dhKey: req.P256DHKey, |
| 63 | }); err != nil { |
| 64 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 65 | Message: "Failed to insert push notification subscription.", |
| 66 | Detail: err.Error(), |
| 67 | }) |
| 68 | return |
| 69 | } |
| 70 | if invalidator, ok := api.WebpushDispatcher.(webpush.SubscriptionCacheInvalidator); ok { |
| 71 | invalidator.InvalidateUser(user.ID) |
| 72 | } |
| 73 | |
| 74 | rw.WriteHeader(http.StatusNoContent) |
| 75 | } |
| 76 | |
| 77 | func validateWebpushEndpoint(rawEndpoint string) error { |
| 78 | endpoint, err := url.Parse(rawEndpoint) |
nothing calls this directly
no test coverage detected