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:
2026-07-02 14:34:02 -04:00
parent 6b7dc2addd
commit a593f04d7f
3 changed files with 22 additions and 2 deletions
+8 -1
View File
@@ -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.