// Dashboard "Recent activity": readable labels for a log row, and Replay (play the row on the overlay again). // // Rows logged since replay_json exists carry the exact overlay message that was shown. // Older rows only have the raw Twitch event (data_json): redeems and outgoing raids are rebuilt from it, // other old rows cannot be replayed. const fs = require('fs'); const path = require('path'); const TIERS = { 1000: 'Tier 1', 2000: 'Tier 2', 3000: 'Tier 3' }; const RARITY = { common: 'Common', uncommon: 'Uncommon', rare: 'Rare', ultra_rare: 'Ultra Rare' }; const LABEL = { follow: 'Follow', sub: 'Sub', gift_sub: 'Gift sub', bits: 'Bits', watch_streak: 'Watch streak', raid: 'Raid in', raid_out: 'Raid out', hype_train: 'Hype Train', tip: 'Tip', channel_points: 'Channel points', auto_reward: 'Built-in reward', power_up: 'Power-up', custom_power_up: 'Custom Power-up', poll_begin: 'Poll started', poll_end: 'Poll ended', command: 'Command', }; const REDEEM_KIND = { channel_points: 'reward', auto_reward: 'auto_reward', power_up: 'power_up', custom_power_up: 'custom_power_up' }; function parse(json) { try { return json ? JSON.parse(json) : null; } catch { return null; } } // Twitch's own reward name from the raw event (also for rows without a stored alert) function rewardTitle(type, raw, alert) { const { AUTO_REWARDS, POWER_UPS } = require('./redeems'); if (alert?.reward) return alert.reward; if (type === 'channel_points') return raw?.reward?.title || null; if (type === 'auto_reward') return AUTO_REWARDS[raw?.reward?.type] || raw?.reward?.type || null; if (type === 'power_up') return POWER_UPS[raw?.power_up?.type] || raw?.power_up?.type || null; if (type === 'custom_power_up') return raw?.custom_power_up?.title || null; return null; } // -> { label, title, tags: [string], message } function describe(log) { const raw = parse(log.data_json); const replay = parse(log.replay_json); const a = replay?.type === 'alert' ? replay.alert : null; const t = log.event_type; const out = { label: LABEL[t] || t, title: null, tags: [], message: null }; if (REDEEM_KIND[t]) { out.title = rewardTitle(t, raw, a) || log.event_detail; if (raw?.reward?.cost) out.tags.push(`${raw.reward.cost} points`); else if (raw?.reward?.channel_points) out.tags.push(`${raw.reward.channel_points} points`); else if (raw?.bits) out.tags.push(`${raw.bits} bits`); else if (raw?.custom_power_up?.bits) out.tags.push(`${raw.custom_power_up.bits} bits`); if (a) { if (a.variation && a.variation !== a.reward) out.tags.push(`played: ${a.variation}`); if (a.rarity && a.rarity !== 'common') out.tags.push(RARITY[a.rarity] || a.rarity); if (a.tts) out.tags.push('TTS'); if (a.video) out.tags.push('video'); } out.message = a?.input || raw?.user_input || raw?.message?.text || null; } else if (a) { switch (a.type) { case 'sub': out.title = a.prime ? 'Prime' : TIERS[a.tier] || a.tier; if (a.months > 1) out.tags.push(`${a.months} months`); if (a.duration > 1) out.tags.push(`${a.duration} months at once`); out.message = a.message || null; break; case 'gift': out.title = a.total > 1 ? `${a.total} gift subs` : `gift to ${a.recipient || 'someone'}`; if (TIERS[a.tier]) out.tags.push(TIERS[a.tier]); if (!a.user) out.tags.push('anonymous'); break; case 'bits': out.title = `${a.bits} bits`; out.message = a.message || null; break; case 'streak': out.title = `${a.streak} streams in a row`; break; case 'raid': out.title = `${a.viewers} viewers`; break; case 'hype': out.title = a.phase === 'begin' ? 'started' : a.phase === 'level' ? `level ${a.level}` : `ended at level ${a.level}`; if (a.golden) out.tags.push('Golden Kappa'); if (a.treasure) out.tags.push('Treasure'); if (a.top?.length) out.message = `top: ${a.top.join(', ')}`; break; case 'tip': out.title = `${a.amount} ${a.currency}`; out.message = a.message || null; break; } } else if (t === 'raid_out') { out.title = `to ${raw?.to_broadcaster_user_name || log.username || '?'}`; if (raw?.viewers != null) out.tags.push(`${raw.viewers} viewers`); } else if (t === 'poll_begin' || t === 'poll_end') { out.title = raw?.title || null; if (t === 'poll_end' && raw?.choices?.length) { const sorted = [...raw.choices].sort((x, y) => (y.votes || 0) - (x.votes || 0)); out.message = sorted.map(c => `${c.title}: ${c.votes ?? 0}`).join(' ยท '); } } else if (log.event_detail) { out.title = log.event_detail; } if (t === 'raid_out' && replay?.raid) { out.title = `to ${replay.raid.target}`; out.tags = [`${replay.raid.viewers} viewers`]; } return out; } // Can this row be replayed? (cheap check for the table; replay() does the real work) function canReplay(log) { if (log.replay_json) return true; return !!log.data_json && (!!REDEEM_KIND[log.event_type] || log.event_type === 'raid_out'); } const PUBLIC = path.join(__dirname, '..', 'public'); const fileGone = url => !url || !fs.existsSync(path.join(PUBLIC, url.replace(/^\/+/, '').split('?')[0])); // -> the overlay message to broadcast, or null async function replayMessage(log) { const stored = parse(log.replay_json); if (stored) { if (stored.type === 'alert' && stored.alert.tts && fileGone(stored.alert.tts)) await require('./redeems').respeak(stored.alert); return stored; } const raw = parse(log.data_json); if (!raw) return null; if (REDEEM_KIND[log.event_type]) { const kind = REDEEM_KIND[log.event_type]; const key = log.event_detail; const input = raw.user_input || raw.message?.text || null; const emote = raw.reward?.emote?.id || raw.power_up?.emote?.id || null; const bits = raw.custom_power_up?.bits ?? null; const alert = await require('./redeems').rebuild(kind, key, { user: raw.user_name || raw.user_login || log.username, input, emote, bits }); return alert ? { type: 'alert', alert } : null; } if (log.event_type === 'raid_out') { const raid = await require('./alerts').raidTarget(raw); return { type: 'raid_card', phase: 'done', raid: { ...raid, viewers: raw.viewers } }; } return null; } module.exports = { describe, canReplay, replayMessage };