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

+65 -8
View File
@@ -597,7 +597,9 @@ paths:
the two protocol keys are kept. The endpoint must be a public `https://` URL —
private/loopback/link-local addresses are refused (here and again at send
time, against DNS rebinding). At most 20 subscriptions per user; the oldest
are dropped first. Re-subscribing an existing endpoint replaces it.
are dropped first. Re-subscribing an existing endpoint replaces it and keeps
its original `created` — the client re-sends its subscription on every
signed-in boot, so a row the instance lost comes back on its own.
requestBody:
required: true
content:
@@ -607,6 +609,13 @@ paths:
required: [subscription]
properties:
subscription: { $ref: '#/components/schemas/PushSubscription' }
deviceId:
type: string
pattern: '^[A-Za-z0-9_-]{8,64}$'
description: >-
A token the browser made up for itself (stored in its localStorage),
so rest-timer pushes can be aimed at the device that started the
rest. Optional; anything that is not a short token is ignored.
responses:
'200':
description: Stored.
@@ -623,6 +632,33 @@ paths:
private: { value: { error: "endpoint must not point at a private address" } }
'401': { $ref: '#/components/responses/Unauthorized' }
/api/push/status:
get:
tags: [push]
operationId: pushStatus
summary: Does the server hold this subscription?
description: >-
Whether the caller's account has a stored subscription with exactly this
`endpoint`. The browser's `PushManager.getSubscription()` says nothing about
the server's side — a row pruned after a dead send (404/410/403) leaves the
browser subscribed to nowhere — so the client asks here before showing the
notifications switch as on, and re-registers when the answer is no.
parameters:
- in: query
name: endpoint
required: true
schema: { type: string, format: uri }
responses:
'200':
description: The answer.
content:
application/json:
schema:
type: object
properties:
subscribed: { type: boolean }
'401': { $ref: '#/components/responses/Unauthorized' }
/api/push/unsubscribe:
post:
tags: [push]
@@ -654,7 +690,8 @@ paths:
description: >-
Sends a test push to all of the caller's subscriptions (localized with the
`lang` in their synced state). Returns 200 even if delivery to individual
endpoints fails — dead endpoints (404/410) are pruned as a side effect.
endpoints fails — dead endpoints (404/410, and 403 for a subscription made
against a VAPID key the instance no longer has) are pruned as a side effect.
responses:
'200':
description: Sends attempted.
@@ -669,10 +706,14 @@ paths:
operationId: pushRestTimer
summary: Schedule a server-side rest-timer push
description: |
Schedules one push after `seconds` (clamped to 1–3600; one pending timer per
user, a new call replaces it). The client schedules this on rest start/extend
and cancels on skip or on-screen completion — the push only actually fires
when the tab was backgrounded/suspended and never got to cancel it.
Schedules one push after `seconds` (clamped to 1–3600). One pending timer per
device — `deviceId`, the same token the subscription was registered with; a
new call from the same device replaces it, and the push goes only to that
device's subscriptions. Without a `deviceId` there is one timer per account
and the push goes to every subscription, as before. The client schedules this
on rest start/extend and cancels on skip or on-screen completion — the push
only actually fires when the tab was backgrounded/suspended and never got to
cancel it. Timers live in memory: an API restart drops them.
Note: the documented `400 seconds required` exists in the code but is
unreachable — the clamp turns any missing/invalid value into 1.
@@ -688,7 +729,11 @@ paths:
type: number
minimum: 1
maximum: 3600
example: { seconds: 90 }
deviceId:
type: string
pattern: '^[A-Za-z0-9_-]{8,64}$'
description: The browser's own token, see `POST /api/push/subscribe`. Optional.
example: { seconds: 90, deviceId: "3f9c1d2e7b0a4c6d9e8f1a2b3c4d5e6f" }
responses:
'200':
description: Timer scheduled.
@@ -702,7 +747,19 @@ paths:
tags: [push]
operationId: pushRestTimerCancel
summary: Cancel the pending rest-timer push
description: Cancels the caller's scheduled rest-timer push, if any. Idempotent.
description: >-
Cancels the caller's scheduled rest-timer push, if any. With a `deviceId` only
that device's timer; without one, every pending timer of the account. Idempotent.
requestBody:
required: false
content:
application/json:
schema:
type: object
properties:
deviceId:
type: string
pattern: '^[A-Za-z0-9_-]{8,64}$'
responses:
'200':
description: Cancelled (or nothing was pending).