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

@ -2,10 +2,25 @@ const { WebSocket } = require('ws');
const clients = new Set();
// Overlay -> server messages. Only accepted from connections that present the overlay UUID
// (?overlay=<uuid>, the same secret as the OBS URL), so strangers cannot inject alerts.
function handleIncoming(raw) {
let msg;
try { msg = JSON.parse(raw); } catch { return; }
if (msg?.type === 'chat_notice') {
const alerts = require('./alerts'); // lazy: alerts.js requires this module
alerts.send(alerts.fromOverlayNotice(msg));
}
}
function init(wss) {
wss.on('connection', (ws) => {
wss.on('connection', (ws, req) => {
clients.add(ws);
console.log(`[ws] Client connected — total: ${clients.size}`);
const db = require('./db');
const overlayId = new URL(req.url, 'http://x').searchParams.get('overlay');
const trusted = !!overlayId && overlayId === db.getSettings().overlay_id;
console.log(`[ws] Client connected${trusted ? ' (overlay)' : ''} — total: ${clients.size}`);
if (trusted) ws.on('message', data => handleIncoming(String(data).slice(0, 2000)));
ws.on('close', () => {
clients.delete(ws);
console.log(`[ws] Client disconnected — total: ${clients.size}`);