fix(webpush): clean up stale push subscriptions for the same device

Signed-off-by: 0xsysr3ll <0xsysr3ll@pm.me>
This commit is contained in:
0xsysr3ll
2025-11-25 23:03:42 +01:00
parent 0b25210219
commit c60fe0fafb

View File

@@ -225,6 +225,26 @@ router.post<
return res.status(204).send();
}
// Clean up old subscriptions from the same device (userAgent) for this user
// iOS can silently refresh endpoints, leaving stale subscriptions in the database
if (req.body.userAgent) {
const staleSubscriptions = await userPushSubRepository.find({
relations: { user: true },
where: {
userAgent: req.body.userAgent,
user: { id: req.user?.id },
},
});
if (staleSubscriptions.length > 0) {
await userPushSubRepository.remove(staleSubscriptions);
logger.debug(
`Removed ${staleSubscriptions.length} stale push subscription(s) from same device.`,
{ label: 'API' }
);
}
}
const userPushSubscription = new UserPushSubscription({
auth: req.body.auth,
endpoint: req.body.endpoint,