Skip to content

Commit 0972263

Browse files
committed
fix(webpush): clean up stale push subscriptions for the same device
Signed-off-by: 0xsysr3ll <[email protected]>
1 parent a7fc15e commit 0972263

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

server/routes/user/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,26 @@ router.post<
225225
return res.status(204).send();
226226
}
227227

228+
// Clean up old subscriptions from the same device (userAgent) for this user
229+
// iOS can silently refresh endpoints, leaving stale subscriptions in the database
230+
if (req.body.userAgent) {
231+
const staleSubscriptions = await userPushSubRepository.find({
232+
relations: { user: true },
233+
where: {
234+
userAgent: req.body.userAgent,
235+
user: { id: req.user?.id },
236+
},
237+
});
238+
239+
if (staleSubscriptions.length > 0) {
240+
await userPushSubRepository.remove(staleSubscriptions);
241+
logger.debug(
242+
`Removed ${staleSubscriptions.length} stale push subscription(s) from same device.`,
243+
{ label: 'API' }
244+
);
245+
}
246+
}
247+
228248
const userPushSubscription = new UserPushSubscription({
229249
auth: req.body.auth,
230250
endpoint: req.body.endpoint,

0 commit comments

Comments
 (0)