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:
parent
88e906ff5d
commit
d481fc4f69
16 changed files with 773 additions and 83 deletions
127
src/alerts.js
Normal file
127
src/alerts.js
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// Turns Twitch events into polaroid alerts and pushes them to the overlay.
|
||||
//
|
||||
// Alert shape (what public/alerts/polaroid.js renders):
|
||||
// { type:'follow', user }
|
||||
// { type:'sub', user, tier:'1000'|'2000'|'3000', prime, months, duration, message }
|
||||
// { type:'gift', user|null (anonymous), total, recipient, tier } total>1 = community gift
|
||||
// { type:'streak', user, streak }
|
||||
// { type:'raid', user, viewers }
|
||||
// { type:'bits', user|null (anonymous), bits, message }
|
||||
//
|
||||
// Every alert also goes through dispatch.dispatchEvent() so the existing actions system
|
||||
// (sounds/videos per event) and the dashboard activity log keep working.
|
||||
const ws = require('./ws');
|
||||
const dispatch = require('./dispatch');
|
||||
|
||||
// legacy action event types + detail, so existing action matching rules still apply
|
||||
function legacy(alert) {
|
||||
switch (alert.type) {
|
||||
case 'sub': return ['sub', alert.tier];
|
||||
case 'gift': return ['gift_sub', alert.tier];
|
||||
case 'bits': return ['bits', String(alert.bits)];
|
||||
case 'streak': return ['watch_streak', String(alert.streak)];
|
||||
default: return [alert.type, null];
|
||||
}
|
||||
}
|
||||
|
||||
function send(alert, raw = null) {
|
||||
if (!alert) return;
|
||||
console.log(`[alerts] ${alert.type} — ${alert.user ?? 'anonymous'} ${JSON.stringify({ ...alert, type: undefined, user: undefined })}`);
|
||||
ws.broadcast({ type: 'alert', alert });
|
||||
const [eventType, detail] = legacy(alert);
|
||||
dispatch.dispatchEvent(eventType, detail, alert.user, raw || alert);
|
||||
}
|
||||
|
||||
const text = m => (m && typeof m.text === 'string' && m.text.trim()) ? m.text.trim() : null;
|
||||
|
||||
// --- EventSub mappers ---
|
||||
|
||||
function fromFollow(e) {
|
||||
return { type: 'follow', user: e.user_name || e.user_login };
|
||||
}
|
||||
|
||||
function fromCheer(e) {
|
||||
return { type: 'bits', user: e.is_anonymous ? null : (e.user_name || e.user_login), bits: e.bits, message: e.message || null };
|
||||
}
|
||||
|
||||
// channel.chat.notification: one event type that carries all sub/gift/raid details
|
||||
// (Prime flag, months bought at once, gift recipient, resub message, community gift totals).
|
||||
function fromChatNotification(e) {
|
||||
const user = e.chatter_is_anonymous ? null : (e.chatter_user_name || e.chatter_user_login);
|
||||
switch (e.notice_type) {
|
||||
case 'sub':
|
||||
return { type: 'sub', user, tier: e.sub.sub_tier, prime: !!e.sub.is_prime, months: 1,
|
||||
duration: e.sub.duration_months || 1, message: text(e.message) };
|
||||
case 'resub':
|
||||
return { type: 'sub', user, tier: e.resub.sub_tier, prime: !!e.resub.is_prime,
|
||||
months: e.resub.cumulative_months || 1, duration: e.resub.duration_months || 1, message: text(e.message) };
|
||||
case 'gift_paid_upgrade':
|
||||
case 'prime_paid_upgrade':
|
||||
// gifted/Prime sub continued as a paid sub: show as a sub
|
||||
return { type: 'sub', user, tier: e.prime_paid_upgrade?.sub_tier || '1000', prime: false, months: 1, duration: 1, message: null };
|
||||
case 'sub_gift':
|
||||
// part of a community gift: the community_sub_gift notice already covers it (no alert per recipient)
|
||||
if (e.sub_gift.community_gift_id) return null;
|
||||
return { type: 'gift', user, total: 1, recipient: e.sub_gift.recipient_user_name, tier: e.sub_gift.sub_tier };
|
||||
case 'community_sub_gift':
|
||||
return { type: 'gift', user, total: e.community_sub_gift.total, tier: e.community_sub_gift.sub_tier };
|
||||
case 'raid':
|
||||
return { type: 'raid', user: e.raid.user_name || e.raid.user_login, viewers: e.raid.viewer_count };
|
||||
default:
|
||||
return null; // announcements, pay-it-forward, bits badge tiers, ...: no alert
|
||||
}
|
||||
}
|
||||
|
||||
// --- watch streaks, reported by the overlay (they only exist as a chat USERNOTICE) ---
|
||||
const seenNotices = new Map(); // message id -> time; several overlays may report the same notice
|
||||
function fromOverlayNotice(n) {
|
||||
if (n.kind !== 'watch_streak') return null;
|
||||
const streak = parseInt(n.streak, 10);
|
||||
if (!n.id || !n.user || !(streak > 0)) return null;
|
||||
const now = Date.now();
|
||||
for (const [id, t] of seenNotices) if (now - t > 10 * 60 * 1000) seenNotices.delete(id);
|
||||
if (seenNotices.has(n.id)) return null;
|
||||
seenNotices.set(n.id, now);
|
||||
return { type: 'streak', user: String(n.user).slice(0, 40), streak };
|
||||
}
|
||||
|
||||
// --- test alerts (dashboard buttons) ---
|
||||
const SAMPLES = {
|
||||
'follow': { type: 'follow', user: 'ChickenWhisperer' },
|
||||
'sub-new': { type: 'sub', user: 'PixelPotato', tier: '1000', prime: false, months: 1, duration: 1 },
|
||||
'sub-prime': { type: 'sub', user: 'LurkingLynx', tier: '1000', prime: true, months: 1, duration: 1 },
|
||||
'sub-resub': { type: 'sub', user: 'ChickenWhisperer', tier: '1000', prime: false, months: 14, duration: 1, message: 'still here for the chickens and the chaos' },
|
||||
'sub-3': { type: 'sub', user: 'BobTheBuilder99', tier: '1000', prime: false, months: 3, duration: 3 },
|
||||
'sub-6': { type: 'sub', user: 'xX_Lurker_Xx', tier: '1000', prime: false, months: 6, duration: 6 },
|
||||
'gift-1': { type: 'gift', user: 'PixelPotato', total: 1, recipient: 'ChickenWhisperer', tier: '1000' },
|
||||
'gift-10': { type: 'gift', user: 'BobTheBuilder99', total: 10, tier: '1000' },
|
||||
'gift-anon': { type: 'gift', user: null, total: 5, tier: '1000' },
|
||||
'streak': { type: 'streak', user: 'LurkingLynx', streak: 5 },
|
||||
'raid': { type: 'raid', user: 'CozyStreamer', viewers: 42 },
|
||||
'bits-3': { type: 'bits', user: 'PixelPotato', bits: 3 },
|
||||
'bits-20': { type: 'bits', user: 'PixelPotato', bits: 20 },
|
||||
'bits-60': { type: 'bits', user: 'ChickenWhisperer', bits: 60 },
|
||||
'bits-80': { type: 'bits', user: 'BobTheBuilder99', bits: 80, message: 'we do a little trolling' },
|
||||
'bits-120': { type: 'bits', user: 'LurkingLynx', bits: 120 },
|
||||
'bits-200': { type: 'bits', user: 'xX_Lurker_Xx', bits: 200, message: 'RUN' },
|
||||
'bits-500': { type: 'bits', user: 'BobTheBuilder99', bits: 500, message: 'Tom da Tank says hi' },
|
||||
};
|
||||
|
||||
const TEST_KEYS = [...Object.keys(SAMPLES), 'queue-test'];
|
||||
|
||||
// Test alerts go straight to the overlay; they are not logged and do not fire actions.
|
||||
// test:true makes the overlay drop what it is showing (fast checking);
|
||||
// 'queue-test' sends four alerts that queue like real ones.
|
||||
function sendTest(key) {
|
||||
if (key === 'queue-test') {
|
||||
ws.broadcast({ type: 'alert', alert: { ...SAMPLES['follow'], test: true } });
|
||||
for (const k of ['bits-60', 'gift-10', 'streak']) ws.broadcast({ type: 'alert', alert: { ...SAMPLES[k], test: 'queue' } });
|
||||
return true;
|
||||
}
|
||||
const alert = SAMPLES[key];
|
||||
if (!alert) return false;
|
||||
ws.broadcast({ type: 'alert', alert: { ...alert, test: true } });
|
||||
return true;
|
||||
}
|
||||
|
||||
module.exports = { send, sendTest, SAMPLES, TEST_KEYS, fromFollow, fromCheer, fromChatNotification, fromOverlayNotice };
|
||||
Loading…
Add table
Add a link
Reference in a new issue