From a30ca707137b2cf695a5604d450eb9d033cb39ba Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 17 Mar 2023 00:20:53 +0100 Subject: [PATCH] Simplify push subscription pruning logic Let's just delete on too old + any failure instead. This leaves alone old + success, which can be beneficial when a device is offline for a while. --- user.go | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/user.go b/user.go index 2c90d64..02dd6f5 100644 --- a/user.go +++ b/user.go @@ -484,23 +484,7 @@ func (net *network) broadcastWebPush(msg *irc.Message) { return } - // First, prune old subscriptions - var active []database.WebPushSubscription for _, sub := range subs { - if time.Since(sub.UpdatedAt) <= webpushPruneSubscriptionDelay { - active = append(active, sub) - continue - } - - if err := net.user.srv.db.DeleteWebPushSubscription(ctx, sub.ID); err != nil { - net.logger.Printf("failed to delete pruned Web Push subscription %q: %v", sub.Endpoint, err) - } else { - net.logger.Debugf("deleted pruned Web Push subscription %q", sub.Endpoint) - } - } - - // Then broadcast the message - for _, sub := range active { err := net.user.srv.sendWebPush(ctx, &webpush.Subscription{ Endpoint: sub.Endpoint, Keys: webpush.Keys{ @@ -516,10 +500,13 @@ func (net *network) broadcastWebPush(msg *irc.Message) { } } else if err != nil { net.logger.Printf("failed to send Web push notification to endpoint %q: %v", sub.Endpoint, err) - } else { - // On success, bump the subscription's update time - if err := net.user.srv.db.StoreWebPushSubscription(ctx, net.user.ID, net.ID, &sub); err != nil { - net.logger.Printf("failed to store Web push subscription: %v", err) + // If it failed for any reason and is old, delete it + if time.Since(sub.UpdatedAt) > webpushPruneSubscriptionDelay { + if err := net.user.srv.db.DeleteWebPushSubscription(ctx, sub.ID); err != nil { + net.logger.Printf("failed to delete pruned Web Push subscription %q: %v", sub.Endpoint, err) + } else { + net.logger.Printf("deleted pruned Web Push subscription %q", sub.Endpoint) + } } } }