@@ -2,11 +2,8 @@ package coderd
2
2
3
3
import (
4
4
"bytes"
5
- "database/sql"
6
5
"encoding/json"
7
- "errors"
8
6
"net/http"
9
- "slices"
10
7
11
8
"github.com/google/uuid"
12
9
@@ -15,7 +12,6 @@ import (
15
12
"github.com/coder/coder/v2/coderd/audit"
16
13
"github.com/coder/coder/v2/coderd/database"
17
14
"github.com/coder/coder/v2/coderd/database/dbauthz"
18
- "github.com/coder/coder/v2/coderd/database/dbtime"
19
15
"github.com/coder/coder/v2/coderd/httpapi"
20
16
"github.com/coder/coder/v2/coderd/httpmw"
21
17
"github.com/coder/coder/v2/coderd/notifications"
@@ -327,150 +323,6 @@ func (api *API) putUserNotificationPreferences(rw http.ResponseWriter, r *http.R
327
323
httpapi .Write (ctx , rw , http .StatusOK , out )
328
324
}
329
325
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
-
474
326
func convertNotificationTemplates (in []database.NotificationTemplate ) (out []codersdk.NotificationTemplate ) {
475
327
for _ , tmpl := range in {
476
328
out = append (out , codersdk.NotificationTemplate {
0 commit comments