Implement proof of concept push notifications

This commit is contained in:
Hornwitser 2025-02-28 15:32:03 +01:00
parent e3210afe3a
commit 6007f4caeb
13 changed files with 407 additions and 1 deletions

View file

@ -1,8 +1,63 @@
"use server";
import webPush from "web-push";
import { Schedule } from "@/app/schedule/types";
import { readFile, writeFile } from "fs/promises";
import { broadcastUpdate } from "./streams";
webPush.setVapidDetails(
"mailto:webmaster@hornwitser.no",
process.env.VAPID_PUBLIC_KEY!,
process.env.VAPID_PRIVATE_KEY!,
)
async function sendPush(title: string, body: string) {
const payload = JSON.stringify({ title, body });
let subscriptions: PushSubscriptionJSON[];
try {
subscriptions = JSON.parse(await readFile("push-subscriptions.json", "utf-8"));
} catch (err: any) {
if (err.code !== "ENOENT") {
console.log(`Dropping "${payload}", no push subscribers`);
return;
}
subscriptions = [];
}
console.log(`Sending "${payload}" to ${subscriptions.length} subscribers`);
const removeIndexes = [];
for (let index = 0; index < subscriptions.length; index += 1) {
const subscription = subscriptions[index];
try {
await webPush.sendNotification(
subscription as webPush.PushSubscription,
payload,
{
TTL: 3600,
urgency: "high",
}
)
} 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 (removeIndexes.length) {
console.log(`Removing indexes ${removeIndexes} from subscriptions`)
removeIndexes.reverse();
for (const index of removeIndexes) {
subscriptions.splice(index, 1);
}
await writeFile(
"push-subscriptions.json",
JSON.stringify(subscriptions, undefined, "\t"),
"utf-8"
);
}
console.log("Push notices sent");
}
export async function createEvent(formData: FormData) {
const schedule: Schedule = JSON.parse(await readFile("schedule.json", "utf-8"));
const id = formData.get("id") as string;
@ -26,6 +81,7 @@ export async function createEvent(formData: FormData) {
});
broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
await sendPush("New event", `${name} will start at ${start}`);
}
export async function modifyEvent(formData: FormData) {
@ -40,6 +96,7 @@ export async function modifyEvent(formData: FormData) {
if (index === -1) {
throw Error("No such event");
}
const timeChanged = schedule.events[index].slots[0].start !== start + "Z";
schedule.events[index] = {
name,
id,
@ -55,6 +112,8 @@ export async function modifyEvent(formData: FormData) {
};
broadcastUpdate(schedule);
await writeFile("schedule.json", JSON.stringify(schedule, null, "\t"), "utf-8");
if (timeChanged)
await sendPush(`New time for ${name}`, `${name} will now start at ${start}`);
}
export async function deleteEvent(formData: FormData) {