Push: reminders owed within 15 minutes still arrive, the subscription heals itself on every boot, rest-timer alerts belong to the device that started the rest

- reminder tick: a due reminder is sent for up to 15 minutes after its time (once per local
  date) instead of only on the exact minute — a restart across that minute used to lose the day;
  state files are stat'ed and re-read only when they changed
- sendPush prunes 403 (stale VAPID key) like 404/410; optional device targeting
- POST /api/push/subscribe is an upsert that keeps created and stores a deviceId;
  GET /api/push/status says whether the server holds an endpoint
- rest timers keyed per user+device; cancel without a device id keeps the old account-wide
  behaviour
- sw.js: payload parsed inside waitUntil (never a silent push), pushsubscriptionchange
  re-subscribes and registers the new endpoint
- lib/push.js: syncPushSubscription() (called once per signed-in boot from App.jsx and when
  Settings opens) re-registers a lost row, replaces a subscription made against another key,
  pushSupported() is false off https; deviceId() per browser
- Settings: the notifications switch shows what the server holds
- tests: server-reminder (window, dedupe, trained day), server-push-status (upsert, status,
  per-device timers), lib/push.test.js
This commit is contained in:
Duarte Santos committed 2026-09-12 13:18:29 +02:00
1 parent c92cfc372a
commit 477fc07f8b
10 files changed
+534 -44

No files matched your search

+10 -2
View File
@@ -7,7 +7,7 @@ import { useUI } from '../store/useUI.js'
import { ACCENTS, todayISO, localTZ, weekStartOf, MONDAY, SUNDAY } from '../lib/format.js'
import { effortOf } from '../lib/history.js'
import { api, webauthnOK, passkeyLogin, passkeyRegister, IS_ANDROID } from '../lib/api.js'
import { pushSupported, enablePush, disablePush, sendTestPush } from '../lib/push.js'
import { pushSupported, enablePush, disablePush, sendTestPush, syncPushSubscription } from '../lib/push.js'
import { wakeLockSupported } from '../lib/wakelock.js'
import { t, LANGS, INSTR_LANGS } from '../lib/i18n.js'
import { DEMO, REPO } from '../lib/demo.js'
@@ -544,9 +544,17 @@ function PushCard({ S, update, toast }) {
const [busy, setBusy] = useState(false)
const supported = pushSupported()
// "On" means the server holds this browser's subscription, not merely that the browser has
// one: a row the instance dropped (dead send, rebuilt db.json) left the switch on with nothing
// ever arriving. syncPushSubscription re-registers on the way; if the server cannot be asked
// (offline), the browser's side is the best answer available.
useEffect(() => {
if (!supported) return
navigator.serviceWorker.ready.then(reg => reg.pushManager.getSubscription()).then(sub => setOn(!!sub)).catch(() => {})
let gone = false
syncPushSubscription()
.then(ok => { if (!gone) setOn(ok) })
.catch(() => navigator.serviceWorker.ready.then(reg => reg.pushManager.getSubscription()).then(sub => { if (!gone) setOn(!!sub) }).catch(() => {}))
return () => { gone = true }
}, [supported])
const toggle = async v => {