Skip to content

Commit 9b5ed09

Browse files
committed
address more comments + renaming
1 parent eb7b102 commit 9b5ed09

File tree

5 files changed

+261
-237
lines changed

5 files changed

+261
-237
lines changed

coderd/notifications.go

Lines changed: 0 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package coderd
22

33
import (
44
"bytes"
5-
"database/sql"
65
"encoding/json"
7-
"errors"
86
"net/http"
9-
"slices"
107

118
"github.com/google/uuid"
129

@@ -15,7 +12,6 @@ import (
1512
"github.com/coder/coder/v2/coderd/audit"
1613
"github.com/coder/coder/v2/coderd/database"
1714
"github.com/coder/coder/v2/coderd/database/dbauthz"
18-
"github.com/coder/coder/v2/coderd/database/dbtime"
1915
"github.com/coder/coder/v2/coderd/httpapi"
2016
"github.com/coder/coder/v2/coderd/httpmw"
2117
"github.com/coder/coder/v2/coderd/notifications"
@@ -327,150 +323,6 @@ func (api *API) putUserNotificationPreferences(rw http.ResponseWriter, r *http.R
327323
httpapi.Write(ctx, rw, http.StatusOK, out)
328324
}
329325

330-
// @Summary Create user webpush subscription
331-
// @ID create-user-webpush-subscription
332-
// @Security CoderSessionToken
333-
// @Accept json
334-
// @Tags Notifications
335-
// @Param request body codersdk.WebpushSubscription true "Webpush subscription"
336-
// @Param user path string true "User ID, name, or me"
337-
// @Router /users/{user}/webpush/subscription [post]
338-
// @Success 204
339-
// @x-apidocgen {"skip": true}
340-
func (api *API) postUserWebpushSubscription(rw http.ResponseWriter, r *http.Request) {
341-
ctx := r.Context()
342-
user := httpmw.UserParam(r)
343-
if !api.Experiments.Enabled(codersdk.ExperimentWebPush) {
344-
httpapi.ResourceNotFound(rw)
345-
return
346-
}
347-
348-
var req codersdk.WebpushSubscription
349-
if !httpapi.Read(ctx, rw, r, &req) {
350-
return
351-
}
352-
353-
if err := api.WebpushDispatcher.Test(ctx, req); err != nil {
354-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
355-
Message: "Failed to test webpush subscription",
356-
Detail: err.Error(),
357-
})
358-
return
359-
}
360-
361-
if _, err := api.Database.InsertWebpushSubscription(ctx, database.InsertWebpushSubscriptionParams{
362-
CreatedAt: dbtime.Now(),
363-
UserID: user.ID,
364-
Endpoint: req.Endpoint,
365-
EndpointAuthKey: req.AuthKey,
366-
EndpointP256dhKey: req.P256DHKey,
367-
}); err != nil {
368-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
369-
Message: "Failed to insert push notification subscription.",
370-
Detail: err.Error(),
371-
})
372-
return
373-
}
374-
375-
rw.WriteHeader(http.StatusNoContent)
376-
}
377-
378-
// @Summary Delete user webpush subscription
379-
// @ID delete-user-webpush-subscription
380-
// @Security CoderSessionToken
381-
// @Accept json
382-
// @Tags Notifications
383-
// @Param request body codersdk.DeleteWebpushSubscription true "Webpush subscription"
384-
// @Param user path string true "User ID, name, or me"
385-
// @Router /users/{user}/webpush/subscription [delete]
386-
// @Success 204
387-
// @x-apidocgen {"skip": true}
388-
func (api *API) deleteUserWebpushSubscription(rw http.ResponseWriter, r *http.Request) {
389-
ctx := r.Context()
390-
user := httpmw.UserParam(r)
391-
392-
if !api.Experiments.Enabled(codersdk.ExperimentWebPush) {
393-
httpapi.ResourceNotFound(rw)
394-
return
395-
}
396-
397-
var req codersdk.DeleteWebpushSubscription
398-
if !httpapi.Read(ctx, rw, r, &req) {
399-
return
400-
}
401-
402-
// Return NotFound if the subscription does not exist.
403-
if existing, err := api.Database.GetWebpushSubscriptionsByUserID(ctx, user.ID); err != nil && errors.Is(err, sql.ErrNoRows) {
404-
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
405-
Message: "Webpush subscription not found.",
406-
})
407-
return
408-
} else if idx := slices.IndexFunc(existing, func(s database.WebpushSubscription) bool {
409-
return s.Endpoint == req.Endpoint
410-
}); idx == -1 {
411-
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
412-
Message: "Webpush subscription not found.",
413-
})
414-
return
415-
}
416-
417-
if err := api.Database.DeleteWebpushSubscriptionByUserIDAndEndpoint(ctx, database.DeleteWebpushSubscriptionByUserIDAndEndpointParams{
418-
UserID: user.ID,
419-
Endpoint: req.Endpoint,
420-
}); err != nil {
421-
if errors.Is(err, sql.ErrNoRows) {
422-
httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{
423-
Message: "Webpush subscription not found.",
424-
})
425-
return
426-
}
427-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
428-
Message: "Failed to delete push notification subscription.",
429-
Detail: err.Error(),
430-
})
431-
return
432-
}
433-
434-
rw.WriteHeader(http.StatusNoContent)
435-
}
436-
437-
// @Summary Send a test push notification
438-
// @ID send-a-test-push-notification
439-
// @Security CoderSessionToken
440-
// @Tags Notifications
441-
// @Param user path string true "User ID, name, or me"
442-
// @Success 204
443-
// @Router /users/{user}/webpush/test [post]
444-
// @x-apidocgen {"skip": true}
445-
func (api *API) postUserPushNotificationTest(rw http.ResponseWriter, r *http.Request) {
446-
ctx := r.Context()
447-
user := httpmw.UserParam(r)
448-
449-
if !api.Experiments.Enabled(codersdk.ExperimentWebPush) {
450-
httpapi.ResourceNotFound(rw)
451-
return
452-
}
453-
454-
// We need to authorize the user to send a push notification to themselves.
455-
if !api.Authorize(r, policy.ActionCreate, rbac.ResourceNotificationMessage.WithOwner(user.ID.String())) {
456-
httpapi.Forbidden(rw)
457-
return
458-
}
459-
460-
if err := api.WebpushDispatcher.Dispatch(ctx, user.ID, codersdk.WebpushMessage{
461-
Title: "It's working!",
462-
Body: "You've subscribed to push notifications.",
463-
}); err != nil {
464-
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
465-
Message: "Failed to send test notification",
466-
Detail: err.Error(),
467-
})
468-
return
469-
}
470-
471-
rw.WriteHeader(http.StatusNoContent)
472-
}
473-
474326
func convertNotificationTemplates(in []database.NotificationTemplate) (out []codersdk.NotificationTemplate) {
475327
for _, tmpl := range in {
476328
out = append(out, codersdk.NotificationTemplate{

coderd/notifications_test.go

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package coderd_test
22

33
import (
44
"net/http"
5-
"net/http/httptest"
65
"slices"
76
"testing"
87

@@ -377,72 +376,3 @@ func TestNotificationTest(t *testing.T) {
377376
require.Len(t, sent, 0)
378377
})
379378
}
380-
381-
const (
382-
// These are valid keys for a web push subscription.
383-
// DO NOT REUSE THESE IN ANY REAL CODE.
384-
validEndpointAuthKey = "zqbxT6JKstKSY9JKibZLSQ=="
385-
validEndpointP256dhKey = "BNNL5ZaTfK81qhXOx23+wewhigUeFb632jN6LvRWCFH1ubQr77FE/9qV1FuojuRmHP42zmf34rXgW80OvUVDgTk="
386-
)
387-
388-
func TestWebpushSubscribeUnsubscribe(t *testing.T) {
389-
t.Parallel()
390-
391-
ctx := testutil.Context(t, testutil.WaitShort)
392-
393-
dv := coderdtest.DeploymentValues(t)
394-
dv.Experiments = []string{string(codersdk.ExperimentWebPush)}
395-
client := coderdtest.New(t, &coderdtest.Options{
396-
DeploymentValues: dv,
397-
})
398-
owner := coderdtest.CreateFirstUser(t, client)
399-
memberClient, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
400-
_, anotherMember := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
401-
402-
handlerCalled := make(chan bool, 1)
403-
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
404-
w.WriteHeader(http.StatusCreated)
405-
handlerCalled <- true
406-
}))
407-
defer server.Close()
408-
409-
err := memberClient.PostWebpushSubscription(ctx, "me", codersdk.WebpushSubscription{
410-
Endpoint: server.URL,
411-
AuthKey: validEndpointAuthKey,
412-
P256DHKey: validEndpointP256dhKey,
413-
})
414-
require.NoError(t, err, "create webpush subscription")
415-
require.True(t, <-handlerCalled, "handler should have been called")
416-
417-
err = memberClient.PostTestWebpushMessage(ctx)
418-
require.NoError(t, err, "test webpush message")
419-
require.True(t, <-handlerCalled, "handler should have been called again")
420-
421-
err = memberClient.DeleteWebpushSubscription(ctx, "me", codersdk.DeleteWebpushSubscription{
422-
Endpoint: server.URL,
423-
})
424-
require.NoError(t, err, "delete webpush subscription")
425-
426-
// Deleting the subscription for a non-existent endpoint should return a 404
427-
err = memberClient.DeleteWebpushSubscription(ctx, "me", codersdk.DeleteWebpushSubscription{
428-
Endpoint: server.URL,
429-
})
430-
var sdkError *codersdk.Error
431-
require.Error(t, err)
432-
require.ErrorAsf(t, err, &sdkError, "error should be of type *codersdk.Error")
433-
require.Equal(t, http.StatusNotFound, sdkError.StatusCode())
434-
435-
// Creating a subscription for another user should not be allowed.
436-
err = memberClient.PostWebpushSubscription(ctx, anotherMember.ID.String(), codersdk.WebpushSubscription{
437-
Endpoint: server.URL,
438-
AuthKey: validEndpointAuthKey,
439-
P256DHKey: validEndpointP256dhKey,
440-
})
441-
require.Error(t, err, "create webpush subscription for another user")
442-
443-
// Deleting a subscription for another user should not be allowed.
444-
err = memberClient.DeleteWebpushSubscription(ctx, anotherMember.ID.String(), codersdk.DeleteWebpushSubscription{
445-
Endpoint: server.URL,
446-
})
447-
require.Error(t, err, "delete webpush subscription for another user")
448-
}

0 commit comments

Comments
 (0)