Add polaroid alerts for follows, subs, gifts, streaks, raids and bits

Overlay
- public/alerts/polaroid.js + .css: animated polaroid (photo drops in, tape
  and heart stick on, caption writes in), randomized angles, per-event art,
  caption, tape colour, badge and sound, fanned photo stack for multi-month
  subs / community gifts / raids, and a queue so alerts never overlap.
- Bits: no alert under 5; sound tiers at 5/50/75/100/150/250.
- Watch streaks are read from chat (USERNOTICE viewermilestone) and reported
  to the server over the overlay WebSocket.

Server
- Subscribe to channel.chat.notification instead of channel.subscribe /
  subscription.message / subscription.gift / channel.raid: one event with
  Prime, months bought at once, gift recipient, resub message and community
  gift totals. Per-recipient gift notices of a gift bomb are skipped, so a
  bomb is one alert (was N+1). Needs the user:read:chat scope (log in again).
- src/alerts.js maps Twitch events to alerts, broadcasts them and still runs
  them through dispatch (activity log + existing actions).
- EventSub starts on boot from the stored login (alerts survive restarts),
  validates the token hourly and refreshes it on expiry or 401, follows
  session_reconnect without dropping events, and reconnects when keepalives
  stop.
- The overlay WebSocket only accepts chat_notice messages from connections
  that pass the overlay UUID; streak notices are deduped by message id.

Dashboard
- Twitch connection status and a test button per alert (POST /alerts/test/:key).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Bashy 2026-11-20 19:57:36 +02:00
parent 88e906ff5d
commit d481fc4f69
16 changed files with 773 additions and 83 deletions

View file

@ -121,6 +121,7 @@ const flt = (v, d) => { const n = parseFloat(v); return isNaN(n) ? d : n; };
| POST | `/layout/positions` | routers/layout.js | JSON position update (legacy, kept for future drag) |
| GET/POST | `/settings` | routers/settings.js | Channel, gap audio, custom CSS/JS; broadcasts settings_update on save |
| POST | `/settings/reconnect` | routers/settings.js | Restart EventSub |
| POST | `/alerts/test/:key` | routers/alerts.js | Send a sample polaroid alert to the overlay |
## Overlay CSS variable system
@ -176,17 +177,38 @@ activity_log — append-only event log
## EventSub subscriptions
| Twitch event | Version | Condition | Scope |
|---|---|---|---|
| `channel.subscribe` | 1 | `broadcaster_user_id` | `channel:read:subscriptions` |
| `channel.subscription.message` | 1 | `broadcaster_user_id` | `channel:read:subscriptions` |
| `channel.subscription.gift` | 1 | `broadcaster_user_id` | `channel:read:subscriptions` |
| `channel.cheer` | 1 | `broadcaster_user_id` | `bits:read` |
| `channel.channel_points_custom_reward_redemption.add` | 1 | `broadcaster_user_id` | `channel:read:redemptions` |
| `channel.follow` | 2 | `broadcaster_user_id` + `moderator_user_id` | `moderator:read:followers` |
| `channel.raid` | 1 | `to_broadcaster_user_id` | none |
| Twitch event | Version | Condition | Scope | Used for |
|---|---|---|---|---|
| `channel.follow` | 2 | `broadcaster_user_id` + `moderator_user_id` | `moderator:read:followers` | follow alert |
| `channel.cheer` | 1 | `broadcaster_user_id` | `bits:read` | bits alert |
| `channel.chat.notification` | 1 | `broadcaster_user_id` + `user_id` | `user:read:chat` | subs, resubs, Prime, multi-month, gifts, community gifts, raids |
| `channel.channel_points_custom_reward_redemption.add` | 1 | `broadcaster_user_id` | `channel:read:redemptions` | actions only (no polaroid yet) |
Token refresh triggered on 401 during subscription.
`channel.chat.notification` replaced `channel.subscribe` / `subscription.message` / `subscription.gift` / `channel.raid`:
one event carries `is_prime`, `duration_months` (months bought at once), `cumulative_months`, the gift recipient,
community gift totals and the resub message. `sub_gift` notices with a `community_gift_id` are skipped (the
`community_sub_gift` notice covers the whole bomb), so a gift bomb is one alert.
**Lifecycle (src/eventsub.js, src/twitch.js):**
- Started on boot from the stored login (`db.getPrimaryUser()`), and again after OAuth login. `EVENTSUB_DISABLED=1` skips it (local UI work).
- Token: validated hourly and before subscribing; refreshed when it expires or on any 401 (`twitch.helix()`), new tokens saved to `users`.
- `session_reconnect`: connects to the given URL (subscriptions carry over), swaps sockets on its welcome.
- Keepalive watchdog: no message for `keepalive_timeout_seconds + 10s` → reconnect.
- Status (connected / subscribed / failed / last event) shown on the dashboard.
## Polaroid alerts
```
Twitch EventSub ─► eventsub.js ─► alerts.js (fromFollow / fromCheer / fromChatNotification) ─► alerts.send()
Overlay chat (tmi.js USERNOTICE viewermilestone) ─► WS up {type:'chat_notice'} ─► alerts.fromOverlayNotice() ─┘
alerts.send() ─► ws.broadcast({type:'alert', alert}) ─► overlay ─► public/alerts/polaroid.js (queue + animation + sound)
└─► dispatch.dispatchEvent() (activity log + legacy actions keep working)
```
- Alert shape and per-event look (art, caption, tape colour, badge, sound, bits tiers) live in `public/alerts/polaroid.js` (`describe()` + CONFIG block). Art in `public/alerts/art/`, sounds in `public/alerts/sounds/` (lengths in `SOUND_MS`, so the alert holds for the whole sound).
- Watch streaks only exist in chat. The overlay reports them; the server only accepts `chat_notice` from WS connections that pass `?overlay=<overlay_id>` and dedupes by message id (several overlays may report the same notice).
- Test buttons on the dashboard: `POST /alerts/test/:key` (keys = `alerts.TEST_KEYS`). Test alerts (`test:true`) replace what is showing; `queue-test` sends four that queue. Not logged, no actions fired.
- Bits under 5: no alert (`BITS_MIN`).
## Production deployment