fix browser notifications + make them work when browser is closed

This commit is contained in:
unknown
2026-03-23 20:24:35 +01:00
parent 17b88cb160
commit c33f6d4108
10 changed files with 480 additions and 36 deletions
+70
View File
@@ -142,6 +142,23 @@ db.run(
`CREATE INDEX IF NOT EXISTS idx_notification_screenshots_ts ON notification_screenshots(ts DESC);`,
);
db.run(`
CREATE TABLE IF NOT EXISTS push_subscriptions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
endpoint TEXT NOT NULL UNIQUE,
p256dh TEXT NOT NULL,
auth TEXT NOT NULL,
created_at INTEGER NOT NULL
);
`);
db.run(
`CREATE INDEX IF NOT EXISTS idx_push_subscriptions_user_id ON push_subscriptions(user_id);`,
);
db.run(
`CREATE UNIQUE INDEX IF NOT EXISTS idx_push_subscriptions_endpoint ON push_subscriptions(endpoint);`,
);
db.run(`
CREATE TABLE IF NOT EXISTS auto_scripts (
id TEXT PRIMARY KEY,
@@ -948,6 +965,59 @@ export function getEnrollmentStats(): {
return stats;
}
export interface PushSubscriptionRecord {
id: number;
userId: number;
endpoint: string;
p256dh: string;
auth: string;
createdAt: number;
}
export function savePushSubscription(userId: number, endpoint: string, p256dh: string, auth: string): void {
db.run(
`INSERT OR REPLACE INTO push_subscriptions (user_id, endpoint, p256dh, auth, created_at)
VALUES (?, ?, ?, ?, ?)`,
userId, endpoint, p256dh, auth, Date.now(),
);
}
export function deletePushSubscription(endpoint: string): void {
db.run(`DELETE FROM push_subscriptions WHERE endpoint=?`, endpoint);
}
export function deletePushSubscriptionsByUser(userId: number): void {
db.run(`DELETE FROM push_subscriptions WHERE user_id=?`, userId);
}
export function getPushSubscriptionsByUser(userId: number): PushSubscriptionRecord[] {
return db
.query<any>(`SELECT * FROM push_subscriptions WHERE user_id=?`)
.all(userId)
.map((r: any) => ({
id: r.id,
userId: r.user_id,
endpoint: r.endpoint,
p256dh: r.p256dh,
auth: r.auth,
createdAt: Number(r.created_at) || 0,
}));
}
export function getAllPushSubscriptions(): PushSubscriptionRecord[] {
return db
.query<any>(`SELECT * FROM push_subscriptions`)
.all()
.map((r: any) => ({
id: r.id,
userId: r.user_id,
endpoint: r.endpoint,
p256dh: r.p256dh,
auth: r.auth,
createdAt: Number(r.created_at) || 0,
}));
}
export function getPendingClients(): {
id: string;
host: string | null;