feat: auto-subscribe push on login, better push logging
- Auto-subscribe to push on login, register, and session restore - Guard against nil db in SendChannelNotification - Log push send count per channel message - Bump SW cache to v5
This commit is contained in:
@@ -185,7 +185,7 @@ func (h *Handler) SendPush(ctx context.Context, userID string, payload map[strin
|
||||
// SendChannelNotification sends a push notification to all server members (except the author)
|
||||
// when a new message is posted in a channel.
|
||||
func (h *Handler) SendChannelNotification(ctx context.Context, channelID, authorID, channelName, content string) {
|
||||
if h.vapidPub == "" || h.vapidPriv == "" {
|
||||
if h.vapidPub == "" || h.vapidPriv == "" || h.db == nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -196,6 +196,8 @@ func (h *Handler) SendChannelNotification(ctx context.Context, channelID, author
|
||||
return
|
||||
}
|
||||
|
||||
h.logger.Info("sending push notifications", "channel", channelName, "author", authorID)
|
||||
|
||||
body := content
|
||||
if len(body) > 200 {
|
||||
body = body[:200] + "..."
|
||||
@@ -221,6 +223,7 @@ func (h *Handler) SendChannelNotification(ctx context.Context, channelID, author
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
sent := 0
|
||||
for rows.Next() {
|
||||
var userID, endpoint, p256dh, auth string
|
||||
if err := rows.Scan(&userID, &endpoint, &p256dh, &auth); err != nil {
|
||||
@@ -250,8 +253,12 @@ func (h *Handler) SendChannelNotification(ctx context.Context, channelID, author
|
||||
}
|
||||
if resp != nil {
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode < 300 {
|
||||
sent++
|
||||
}
|
||||
}
|
||||
}
|
||||
h.logger.Info("push notifications sent", "count", sent, "channel", channelName)
|
||||
}
|
||||
|
||||
// GenerateVAPIDKeys is in cmd/keygen. Use github.com/SherClockHolmes/webpush-go directly.
|
||||
|
||||
Reference in New Issue
Block a user