Refactor subscription format
Place the actual push subscription data into a push property on the subscription so that other properties can be added to it.
This commit is contained in:
parent
b4934005ae
commit
abdcc83eb9
5 changed files with 29 additions and 11 deletions
|
@ -1,13 +1,17 @@
|
|||
import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
||||
import { Subscription } from "~/shared/types/account";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
||||
const subscriptions = await readSubscriptions();
|
||||
const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint);
|
||||
const existingIndex = subscriptions.findIndex(
|
||||
sub => sub.type === "push" && sub.push.endpoint === body.subscription.endpoint
|
||||
);
|
||||
const subscription: Subscription = { type: "push", push: body.subscription };
|
||||
if (existingIndex !== -1) {
|
||||
subscriptions[existingIndex] = body.subscription;
|
||||
subscriptions[existingIndex] = subscription;
|
||||
} else {
|
||||
subscriptions.push(body.subscription);
|
||||
subscriptions.push(subscription);
|
||||
}
|
||||
await writeSubscriptions(subscriptions);
|
||||
if (existingIndex !== -1) {
|
||||
|
|
|
@ -3,7 +3,9 @@ import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
|||
export default defineEventHandler(async (event) => {
|
||||
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
||||
const subscriptions = await readSubscriptions();
|
||||
const existingIndex = subscriptions.findIndex(sub => sub.endpoint === body.subscription.endpoint);
|
||||
const existingIndex = subscriptions.findIndex(
|
||||
sub => sub.type === "push" && sub.push.endpoint === body.subscription.endpoint
|
||||
);
|
||||
if (existingIndex !== -1) {
|
||||
subscriptions.splice(existingIndex, 1);
|
||||
} else {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { Schedule } from "~/shared/types/schedule";
|
||||
import { generateDemoSchedule } from "./generate-demo-schedule";
|
||||
import { Subscription } from "~/shared/types/account";
|
||||
|
||||
// For this demo I'm just storing the runtime data in JSON files. When making
|
||||
// this into proper application this should be replaced with an actual database.
|
||||
|
@ -29,10 +30,14 @@ export async function writeSchedule(schedule: Schedule) {
|
|||
}
|
||||
|
||||
export async function readSubscriptions() {
|
||||
let subscriptions = await readJson<PushSubscriptionJSON[]>(subscriptionsPath, []);
|
||||
let subscriptions = await readJson<Subscription[]>(subscriptionsPath, []);
|
||||
if (subscriptions.length && "keys" in subscriptions[0]) {
|
||||
// Discard old format
|
||||
subscriptions = [];
|
||||
}
|
||||
return subscriptions;
|
||||
}
|
||||
|
||||
export async function writeSubscriptions(subscriptions: PushSubscriptionJSON[]) {
|
||||
export async function writeSubscriptions(subscriptions: Subscription[]) {
|
||||
await writeFile(subscriptionsPath, JSON.stringify(subscriptions, undefined, "\t") + "\n", "utf-8");
|
||||
}
|
||||
|
|
|
@ -14,9 +14,11 @@ export async function sendPush(title: string, body: string) {
|
|||
const removeIndexes = [];
|
||||
for (let index = 0; index < subscriptions.length; index += 1) {
|
||||
const subscription = subscriptions[index];
|
||||
if (subscription.type !== "push")
|
||||
continue;
|
||||
try {
|
||||
await webPush.sendNotification(
|
||||
subscription as webPush.PushSubscription,
|
||||
subscription.push as webPush.PushSubscription,
|
||||
payload,
|
||||
{
|
||||
TTL: 3600,
|
||||
|
@ -24,10 +26,11 @@ export async function sendPush(title: string, body: string) {
|
|||
}
|
||||
)
|
||||
} catch (err: any) {
|
||||
console.error("Received error sending push notice:", err.message, err?.statusCode)
|
||||
console.error(err);
|
||||
if (err?.statusCode >= 400 && err?.statusCode < 500) {
|
||||
removeIndexes.push(index)
|
||||
if (err?.statusCode === 410) {
|
||||
removeIndexes.push(index);
|
||||
} else {
|
||||
console.error("Received error sending push notice:", err.message, err?.statusCode)
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
4
shared/types/account.d.ts
vendored
Normal file
4
shared/types/account.d.ts
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
export interface Subscription {
|
||||
type: "push",
|
||||
push: PushSubscriptionJSON,
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue