mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
webpush: replace old subscriptions in transaction
This commit is contained in:
parent
94cf3b2931
commit
b3928bac45
1 changed files with 24 additions and 14 deletions
|
|
@ -42,9 +42,9 @@ export const pushSubscribeController: AppController = async (c) => {
|
||||||
return c.json({ error: 'Unauthorized' }, 401);
|
return c.json({ error: 'Unauthorized' }, 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [_, bech32] = match;
|
const [_, accessToken] = match;
|
||||||
|
|
||||||
if (!bech32.startsWith('token1')) {
|
if (!accessToken.startsWith('token1')) {
|
||||||
return c.json({ error: 'Unauthorized' }, 401);
|
return c.json({ error: 'Unauthorized' }, 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,11 +59,20 @@ export const pushSubscribeController: AppController = async (c) => {
|
||||||
|
|
||||||
const { subscription, data } = result.data;
|
const { subscription, data } = result.data;
|
||||||
|
|
||||||
const { id } = await kysely
|
const pubkey = await signer.getPublicKey();
|
||||||
|
const tokenHash = await getTokenHash(accessToken as `token1${string}`);
|
||||||
|
|
||||||
|
const { id } = await kysely.transaction().execute(async (trx) => {
|
||||||
|
await trx
|
||||||
|
.deleteFrom('push_subscriptions')
|
||||||
|
.where('token_hash', '=', tokenHash)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
return trx
|
||||||
.insertInto('push_subscriptions')
|
.insertInto('push_subscriptions')
|
||||||
.values({
|
.values({
|
||||||
pubkey: await signer.getPublicKey(),
|
pubkey,
|
||||||
token_hash: await getTokenHash(bech32 as `token1${string}`),
|
token_hash: tokenHash,
|
||||||
endpoint: subscription.endpoint,
|
endpoint: subscription.endpoint,
|
||||||
p256dh: subscription.keys.p256dh,
|
p256dh: subscription.keys.p256dh,
|
||||||
auth: subscription.keys.auth,
|
auth: subscription.keys.auth,
|
||||||
|
|
@ -71,6 +80,7 @@ export const pushSubscribeController: AppController = async (c) => {
|
||||||
})
|
})
|
||||||
.returning('id')
|
.returning('id')
|
||||||
.executeTakeFirstOrThrow();
|
.executeTakeFirstOrThrow();
|
||||||
|
});
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
id,
|
id,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue