fix: push notification

This commit is contained in:
Krispire Admin
2026-06-08 17:52:21 +05:30
parent 0c8b2a54b3
commit 05d906aee9
4 changed files with 159 additions and 9 deletions
+22
View File
@@ -52,6 +52,28 @@ func (r *PushRepo) ListByUser(ctx context.Context, userID string) ([]model.PushS
return out, rows.Err()
}
// PruneToMostRecent keeps only the `keep` most-recently-used subscriptions for
// a user, deleting older ones — a soft per-user device cap.
func (r *PushRepo) PruneToMostRecent(ctx context.Context, userID string, keep int) error {
if keep < 1 {
keep = 1
}
_, err := r.pg.Pool.Exec(ctx, `
DELETE FROM push_subscriptions
WHERE user_id = $1 AND id NOT IN (
SELECT id FROM push_subscriptions
WHERE user_id = $1
ORDER BY last_used_at DESC NULLS LAST
LIMIT $2
)`,
userID, keep,
)
if err != nil {
return fmt.Errorf("prune push subscriptions: %w", err)
}
return nil
}
// DeleteByEndpoint removes a subscription (on unsubscribe or when the push
// service reports it gone).
func (r *PushRepo) DeleteByEndpoint(ctx context.Context, endpoint string) error {