2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-05-20 00:43:29 +02:00
|
|
|
import * as fs from "node:fs/promises";
|
2025-05-20 00:22:28 +02:00
|
|
|
import type { H3Event } from "h3";
|
2025-03-05 15:36:50 +01:00
|
|
|
import webPush from "web-push";
|
2025-03-05 18:41:47 +01:00
|
|
|
import { readSubscriptions, writeSubscriptions } from "~/server/database";
|
2025-03-05 15:36:50 +01:00
|
|
|
|
2025-05-20 00:22:28 +02:00
|
|
|
let cachedVapidDetails: {
|
|
|
|
subject: string,
|
|
|
|
publicKey: string,
|
|
|
|
privateKey: string,
|
|
|
|
} | undefined;
|
2025-03-05 15:36:50 +01:00
|
|
|
|
2025-05-20 00:43:29 +02:00
|
|
|
async function useVapidDetails(event: H3Event) {
|
2025-05-20 00:22:28 +02:00
|
|
|
if (cachedVapidDetails) {
|
|
|
|
return cachedVapidDetails;
|
|
|
|
}
|
|
|
|
|
|
|
|
const runtimeConfig = useRuntimeConfig(event);
|
2025-05-20 00:25:28 +02:00
|
|
|
if (!runtimeConfig.vapidSubject) throw new Error("NUXT_VAPID_SUBJECT not set.")
|
2025-05-20 00:22:28 +02:00
|
|
|
if (!runtimeConfig.public.vapidPublicKey) throw new Error("NUXT_PUBLIC_VAPID_PUBLIC_KEY not set.")
|
2025-05-20 00:43:29 +02:00
|
|
|
if (!runtimeConfig.vapidPrivateKeyFile) throw new Error("NUXT_VAPID_PRIVATE_KEY_FILE not set.")
|
2025-05-20 00:22:28 +02:00
|
|
|
|
|
|
|
return cachedVapidDetails = {
|
2025-05-20 00:25:28 +02:00
|
|
|
subject: runtimeConfig.vapidSubject,
|
2025-05-20 00:22:28 +02:00
|
|
|
publicKey: runtimeConfig.public.vapidPublicKey,
|
2025-05-20 00:43:29 +02:00
|
|
|
privateKey: await fs.readFile(runtimeConfig.vapidPrivateKeyFile, "utf-8"),
|
2025-05-20 00:22:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function sendPush(event: H3Event, title: string, body: string) {
|
2025-05-20 00:43:29 +02:00
|
|
|
const vapidDetails = await useVapidDetails(event);
|
2025-03-05 15:36:50 +01:00
|
|
|
const payload = JSON.stringify({ title, body });
|
2025-03-05 18:41:47 +01:00
|
|
|
const subscriptions = await readSubscriptions();
|
2025-03-05 15:36:50 +01:00
|
|
|
console.log(`Sending "${payload}" to ${subscriptions.length} subscribers`);
|
|
|
|
const removeIndexes = [];
|
|
|
|
for (let index = 0; index < subscriptions.length; index += 1) {
|
|
|
|
const subscription = subscriptions[index];
|
2025-03-07 12:30:39 +01:00
|
|
|
if (subscription.type !== "push")
|
|
|
|
continue;
|
2025-03-05 15:36:50 +01:00
|
|
|
try {
|
|
|
|
await webPush.sendNotification(
|
2025-03-07 12:30:39 +01:00
|
|
|
subscription.push as webPush.PushSubscription,
|
2025-03-05 15:36:50 +01:00
|
|
|
payload,
|
|
|
|
{
|
|
|
|
TTL: 3600,
|
|
|
|
urgency: "high",
|
2025-05-20 00:22:28 +02:00
|
|
|
vapidDetails,
|
2025-03-05 15:36:50 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
} catch (err: any) {
|
2025-03-07 12:30:39 +01:00
|
|
|
if (err?.statusCode === 410) {
|
|
|
|
removeIndexes.push(index);
|
|
|
|
} else {
|
|
|
|
console.error("Received error sending push notice:", err.message, err?.statusCode)
|
|
|
|
console.error(err);
|
2025-03-05 15:36:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (removeIndexes.length) {
|
|
|
|
console.log(`Removing indexes ${removeIndexes} from subscriptions`)
|
|
|
|
removeIndexes.reverse();
|
|
|
|
for (const index of removeIndexes) {
|
|
|
|
subscriptions.splice(index, 1);
|
|
|
|
}
|
2025-03-05 18:41:47 +01:00
|
|
|
await writeSubscriptions(subscriptions);
|
2025-03-05 15:36:50 +01:00
|
|
|
}
|
|
|
|
console.log("Push notices sent");
|
|
|
|
}
|