@@ -41,13 +41,14 @@ type Dispatcher interface {
41
41
// for updates inside of a workspace, which we want to be immediate.
42
42
//
43
43
// See: https://github.com/coder/internal/issues/528
44
- func New (ctx context.Context , log * slog.Logger , db database.Store ) (Dispatcher , error ) {
44
+ func New (ctx context.Context , log slog.Logger , db database.Store , vapidSub string ) (Dispatcher , error ) {
45
45
keys , err := db .GetWebpushVAPIDKeys (ctx )
46
46
if err != nil {
47
47
if ! errors .Is (err , sql .ErrNoRows ) {
48
48
return nil , xerrors .Errorf ("get notification vapid keys: %w" , err )
49
49
}
50
50
}
51
+
51
52
if keys .VapidPublicKey == "" || keys .VapidPrivateKey == "" {
52
53
// Generate new VAPID keys. This also deletes all existing push
53
54
// subscriptions as part of the transaction, as they are no longer
@@ -62,6 +63,7 @@ func New(ctx context.Context, log *slog.Logger, db database.Store) (Dispatcher,
62
63
}
63
64
64
65
return & Webpusher {
66
+ vapidSub : vapidSub ,
65
67
store : db ,
66
68
log : log ,
67
69
VAPIDPublicKey : keys .VapidPublicKey ,
@@ -71,8 +73,14 @@ func New(ctx context.Context, log *slog.Logger, db database.Store) (Dispatcher,
71
73
72
74
type Webpusher struct {
73
75
store database.Store
74
- log * slog.Logger
75
-
76
+ log slog.Logger
77
+ // VAPID allows us to identify the sender of the message.
78
+ // This must be a https:// URL or an email address.
79
+ // Some push services (such as Apple's) require this to be set.
80
+ vapidSub string
81
+
82
+ // public and private keys for VAPID. These are used to sign and encrypt
83
+ // the message payload.
76
84
VAPIDPublicKey string
77
85
VAPIDPrivateKey string
78
86
}
@@ -148,10 +156,13 @@ func (n *Webpusher) webpushSend(ctx context.Context, msg []byte, endpoint string
148
156
Endpoint : endpoint ,
149
157
Keys : keys ,
150
158
}, & webpush.Options {
159
+ Subscriber : n .vapidSub ,
151
160
VAPIDPublicKey : n .VAPIDPublicKey ,
152
161
VAPIDPrivateKey : n .VAPIDPrivateKey ,
153
162
})
154
163
if err != nil {
164
+ n .log .Error (ctx , "failed to send webpush notification" , slog .Error (err ), slog .F ("endpoint" , endpoint ))
165
+ n .log .Debug (ctx , "webpush notification payload" , slog .F ("payload" , string (cpy )))
155
166
return - 1 , nil , xerrors .Errorf ("send webpush notification: %w" , err )
156
167
}
157
168
defer resp .Body .Close ()
0 commit comments