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 { readSubscriptions, writeSubscriptions } from "~/server/database";
|
||||||
|
import { Subscription } from "~/shared/types/account";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
||||||
const subscriptions = await readSubscriptions();
|
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) {
|
if (existingIndex !== -1) {
|
||||||
subscriptions[existingIndex] = body.subscription;
|
subscriptions[existingIndex] = subscription;
|
||||||
} else {
|
} else {
|
||||||
subscriptions.push(body.subscription);
|
subscriptions.push(subscription);
|
||||||
}
|
}
|
||||||
await writeSubscriptions(subscriptions);
|
await writeSubscriptions(subscriptions);
|
||||||
if (existingIndex !== -1) {
|
if (existingIndex !== -1) {
|
||||||
|
|
|
@ -3,7 +3,9 @@ import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
const body: { subscription: PushSubscriptionJSON } = await readBody(event);
|
||||||
const subscriptions = await readSubscriptions();
|
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) {
|
if (existingIndex !== -1) {
|
||||||
subscriptions.splice(existingIndex, 1);
|
subscriptions.splice(existingIndex, 1);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { readFile, writeFile } from "node:fs/promises";
|
import { readFile, writeFile } from "node:fs/promises";
|
||||||
import { Schedule } from "~/shared/types/schedule";
|
import { Schedule } from "~/shared/types/schedule";
|
||||||
import { generateDemoSchedule } from "./generate-demo-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
|
// 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.
|
// 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() {
|
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;
|
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");
|
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 = [];
|
const removeIndexes = [];
|
||||||
for (let index = 0; index < subscriptions.length; index += 1) {
|
for (let index = 0; index < subscriptions.length; index += 1) {
|
||||||
const subscription = subscriptions[index];
|
const subscription = subscriptions[index];
|
||||||
|
if (subscription.type !== "push")
|
||||||
|
continue;
|
||||||
try {
|
try {
|
||||||
await webPush.sendNotification(
|
await webPush.sendNotification(
|
||||||
subscription as webPush.PushSubscription,
|
subscription.push as webPush.PushSubscription,
|
||||||
payload,
|
payload,
|
||||||
{
|
{
|
||||||
TTL: 3600,
|
TTL: 3600,
|
||||||
|
@ -24,10 +26,11 @@ export async function sendPush(title: string, body: string) {
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error("Received error sending push notice:", err.message, err?.statusCode)
|
if (err?.statusCode === 410) {
|
||||||
console.error(err);
|
removeIndexes.push(index);
|
||||||
if (err?.statusCode >= 400 && err?.statusCode < 500) {
|
} else {
|
||||||
removeIndexes.push(index)
|
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