mirror of
https://github.com/db9979/wibosim.git
synced 2026-12-06 13:34:09 +01:00
- Erneuern eines Abos ohne Anmeldung (/api/push/renew) nur mit Erneuerungs-Geheimnis: der Server gibt es beim Einschalten einmal aus und speichert nur den Hash; das Gerät legt es in IndexedDB ab, der Service Worker schickt es mit und bekommt ein neues. Neuer Endpunkt muss zum selben Push-Dienst gehören und auf der Allowlist stehen; eigene Ratenbegrenzung (10 je 10 Minuten). Ältere Abos holen das Geheimnis beim nächsten Öffnen der App. - Zustellen an Push-Dienste ohne Weiterleitungen (redirect: error) - DM-Vorschau: Vorgabe „Nur den Absender“, Textanfang auf Wunsch - Sperre durch die Spielleitung entfernt die Push-Abos, gesperrte Konten bekommen keinen Push; Passwortwechsel und Abmelden aller Sitzungen lassen sie bestehen - Routen aus webpush.js nach src/routes/push.js (installPush in index.js vor finishApp) - Start aus einer Benachrichtigung springt nur noch für Unterhaltungen selbst (Bereiche öffnet app.js) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUhQVoPUusUzxgxLWkK
96 lines
4.8 KiB
JavaScript
96 lines
4.8 KiB
JavaScript
// WiBoSim Service Worker (Team App): Push-Benachrichtigungen, Klick → App öffnen und zum Bereich springen,
|
||
// kleine Offline-Seite. Bewusst KEIN Zwischenspeichern der App-Dateien: Skripte, Stile und Daten kommen immer frisch
|
||
// vom Server (sonst Ärger mit Updates und dem Delta-Takt). Zwischengespeichert werden nur offline.html und Symbole.
|
||
const CACHE = 'wibo-offline-1';
|
||
const OFFLINE = '/offline.html';
|
||
const KEEP = [OFFLINE, '/favicon.svg', '/icon-192.png'];
|
||
|
||
self.addEventListener('install', (e) => {
|
||
e.waitUntil(caches.open(CACHE).then((c) => c.addAll(KEEP)).catch(() => {}).then(() => self.skipWaiting()));
|
||
});
|
||
|
||
self.addEventListener('activate', (e) => {
|
||
e.waitUntil(caches.keys()
|
||
.then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
|
||
.then(() => self.clients.claim()));
|
||
});
|
||
|
||
// Nur Seitenaufrufe (Navigation) abfangen: online immer vom Server, ohne Netz die Offline-Seite.
|
||
// Alle anderen Anfragen (Skripte, API, WebSocket) gehen am Service Worker vorbei.
|
||
self.addEventListener('fetch', (e) => {
|
||
const r = e.request;
|
||
if (r.mode !== 'navigate' || r.method !== 'GET') return;
|
||
e.respondWith(fetch(r).catch(() => caches.match(OFFLINE).then((x) => x || new Response('Keine Verbindung', { status: 503, headers: { 'Content-Type': 'text/plain; charset=utf-8' } }))));
|
||
});
|
||
|
||
/** Nur Adressen dieser Seite öffnen (Deep-Link wie /#depot oder /#firma/NVLT) */
|
||
function safeUrl(u) {
|
||
try { const x = new URL(u || '/', self.location.origin); return x.origin === self.location.origin ? x.href : self.location.origin + '/'; } catch { return self.location.origin + '/'; }
|
||
}
|
||
|
||
self.addEventListener('push', (e) => {
|
||
let d = {};
|
||
try { d = e.data ? e.data.json() : {}; } catch { d = { t: 'WiBoSim', b: e.data ? e.data.text() : '' }; }
|
||
const title = String(d.t || 'WiBoSim').slice(0, 120);
|
||
const jobs = [self.registration.showNotification(title, {
|
||
body: String(d.b || '').slice(0, 300),
|
||
icon: '/icon-192.png',
|
||
badge: '/icon-192.png',
|
||
tag: d.tag || 'wibosim',
|
||
renotify: true,
|
||
timestamp: Number(d.ts) || Date.now(),
|
||
data: { url: safeUrl(d.u) },
|
||
})];
|
||
// Zahl ungelesener Meldungen am App-Symbol (wo unterstützt)
|
||
if (Number.isInteger(d.n) && self.navigator && 'setAppBadge' in self.navigator) jobs.push((d.n > 0 ? self.navigator.setAppBadge(d.n) : self.navigator.clearAppBadge()).catch(() => {}));
|
||
e.waitUntil(Promise.all(jobs));
|
||
});
|
||
|
||
self.addEventListener('notificationclick', (e) => {
|
||
e.notification.close();
|
||
const url = safeUrl(e.notification.data && e.notification.data.url);
|
||
e.waitUntil(self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((list) => {
|
||
const win = list.find((c) => { try { const u = new URL(c.url); return u.origin === self.location.origin && u.pathname === '/'; } catch { return false; } });
|
||
if (win) {
|
||
// offene App: nach vorne holen und dort zum Bereich springen (ohne Neuladen)
|
||
win.postMessage({ type: 'wibo-nav', url });
|
||
return win.focus ? win.focus() : win;
|
||
}
|
||
return self.clients.openWindow(url);
|
||
}));
|
||
});
|
||
|
||
/** Erneuerungs-Geheimnis des Abos (legt js/pwa.js beim Einschalten in IndexedDB ab). op: 'get' | 'put' */
|
||
function renewSecret(op, val) {
|
||
return new Promise((done) => {
|
||
try {
|
||
const rq = indexedDB.open('wibo-push', 1);
|
||
rq.onupgradeneeded = () => rq.result.createObjectStore('kv');
|
||
rq.onerror = () => done(null);
|
||
rq.onsuccess = () => {
|
||
const db = rq.result, tx = db.transaction('kv', op === 'get' ? 'readonly' : 'readwrite'), st = tx.objectStore('kv');
|
||
const q = op === 'get' ? st.get('renew') : st.put(String(val), 'renew');
|
||
q.onsuccess = () => done(q.result || null);
|
||
q.onerror = () => done(null);
|
||
tx.oncomplete = () => db.close();
|
||
};
|
||
} catch (x) { done(null); }
|
||
});
|
||
}
|
||
|
||
// Der Push-Dienst hat das Abo erneuert: dem Server das neue melden – nur mit dem Erneuerungs-Geheimnis des alten Abos
|
||
// (ohne Anmeldung; sonst könnte jeder, der die Zustelladresse kennt, das Abo umhängen). Danach gilt ein neues Geheimnis.
|
||
self.addEventListener('pushsubscriptionchange', (e) => {
|
||
const old = e.oldSubscription;
|
||
e.waitUntil((async () => {
|
||
const secret = await renewSecret('get');
|
||
if (!secret || !old) return; // ohne Geheimnis meldet die App das Abo beim nächsten Öffnen selbst neu
|
||
const key = old.options && old.options.applicationServerKey;
|
||
const sub = e.newSubscription || (key ? await self.registration.pushManager.subscribe({ userVisibleOnly: true, applicationServerKey: key }) : null);
|
||
if (!sub) return;
|
||
const r = await fetch('/api/push/renew', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ oldEndpoint: old.endpoint, subscription: sub.toJSON(), secret }) });
|
||
const j = r.ok ? await r.json() : null;
|
||
if (j && j.renew) await renewSecret('put', j.renew);
|
||
})().catch(() => {}));
|
||
});
|