MCPcopy Index your code
hub / github.com/coder/coder / postUserWebpushSubscription

Method postUserWebpushSubscription

coderd/webpush.go:34–75  ·  view source on GitHub ↗

@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)

Source from the content-addressed store, hash-verified

32// @Success 204
33// @x-apidocgen {"skip": true}
34func (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
77func validateWebpushEndpoint(rawEndpoint string) error {
78 endpoint, err := url.Parse(rawEndpoint)

Callers

nothing calls this directly

Calls 11

UserParamFunction · 0.92
ReadFunction · 0.92
WriteFunction · 0.92
NowFunction · 0.92
validateWebpushEndpointFunction · 0.85
ContextMethod · 0.65
TestMethod · 0.65
InvalidateUserMethod · 0.65
ErrorMethod · 0.45
WriteHeaderMethod · 0.45

Tested by

no test coverage detected