Skip to content

Commit e3fa4ef

Browse files
committed
Remove expired subscriptions
1 parent 6884a91 commit e3fa4ef

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

backend/api/src/junk-drawer/private-messages.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,20 @@ const notifyOtherUserInChannelIfInactive = async (
180180
})
181181
console.log('Sending notification to:', subscription.endpoint, payload);
182182
await webPush.sendNotification(subscription, payload);
183-
} catch (err) {
184-
console.error('Failed to send notification', err);
185-
// optionally remove invalid subscription from DB
183+
} catch (err: any) {
184+
console.log('Failed to send notification', err);
185+
if (err.statusCode === 410 || err.statusCode === 404) {
186+
console.warn('Removing expired subscription', subscription.endpoint);
187+
await pg.none(
188+
`DELETE
189+
FROM push_subscriptions
190+
WHERE endpoint = $1
191+
AND user_id = $2`,
192+
[subscription.endpoint, otherUser.id]
193+
);
194+
} else {
195+
console.error('Push failed', err);
196+
}
186197
}
187198
}
188199

@@ -223,8 +234,9 @@ export async function getSubscriptionsFromDB(
223234
) {
224235
try {
225236
const subscriptions = await pg.manyOrNone(`
226-
select endpoint, keys from push_subscriptions
227-
where user_id = $1
237+
select endpoint, keys
238+
from push_subscriptions
239+
where user_id = $1
228240
`, [userId]
229241
);
230242

0 commit comments

Comments
 (0)