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
|
|
@ -11,6 +11,38 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<h3>Twitch connection</h3>
|
||||
<p>
|
||||
<% if (eventsubStatus.connected) { %>
|
||||
<span class="badge badge-green">connected</span>
|
||||
<% } else if (eventsubStatus.running) { %>
|
||||
<span class="badge badge-grey">connecting…</span>
|
||||
<% } else { %>
|
||||
<span class="badge badge-grey">not running</span> (log in again to start)
|
||||
<% } %>
|
||||
<% if (eventsubStatus.subscribed.length) { %><br><small>Listening to: <%= eventsubStatus.subscribed.join(', ') %></small><% } %>
|
||||
<% if (eventsubStatus.failed.length) { %><br><small style="color:#c0392b">Failed: <%= eventsubStatus.failed.join(', ') %> (log out and in again if a permission is missing)</small><% } %>
|
||||
<% if (eventsubStatus.lastEventAt) { %><br><small>Last event: <%= eventsubStatus.lastEventAt %></small><% } %>
|
||||
</p>
|
||||
|
||||
<h3>Test alerts</h3>
|
||||
<p><small>Sends a sample alert to the overlay (OBS). Not logged, no actions fired.</small></p>
|
||||
<div id="test-alerts" style="display:flex; flex-wrap:wrap; gap:.4rem; margin-bottom:.5rem;">
|
||||
<% for (const key of testKeys) { %>
|
||||
<button type="button" class="outline" data-key="<%= key %>" style="width:auto; padding:.3rem .8rem; margin:0;"><%= key %></button>
|
||||
<% } %>
|
||||
</div>
|
||||
<p><small id="test-status"></small></p>
|
||||
<script>
|
||||
document.querySelectorAll('#test-alerts button').forEach(b => b.addEventListener('click', async () => {
|
||||
const out = document.getElementById('test-status');
|
||||
try {
|
||||
const r = await fetch('/alerts/test/' + encodeURIComponent(b.dataset.key), { method: 'POST' });
|
||||
out.textContent = r.ok ? `Sent: ${b.dataset.key}` : `Failed (${r.status})`;
|
||||
} catch (e) { out.textContent = 'Failed: ' + e.message; }
|
||||
}));
|
||||
</script>
|
||||
|
||||
<h3>Recent activity</h3>
|
||||
<% if (logs.length === 0) { %>
|
||||
<p><em>No activity yet. Events will appear here once your overlay is connected and Twitch fires events.</em></p>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,9 @@
|
|||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Caveat:wght@700&display=swap">
|
||||
<link rel="stylesheet" href="/alerts/polaroid.css">
|
||||
<style>
|
||||
:root {
|
||||
--chat-left: <%= overlayLayout.chat_left %>%;
|
||||
|
|
@ -85,6 +88,7 @@
|
|||
<video id="alert-video" autoplay></video>
|
||||
|
||||
<script src="/vendor/tmi.min.js"></script>
|
||||
<script src="/alerts/polaroid.js"></script>
|
||||
<script>
|
||||
const S = <%- JSON.stringify(settings) %>;
|
||||
const chat = document.getElementById('chat');
|
||||
|
|
@ -98,6 +102,15 @@
|
|||
const client = new tmi.Client({ channels: [S.channel] });
|
||||
client.connect().catch(console.error);
|
||||
|
||||
// Watch streaks only exist as a chat USERNOTICE (msg-id=viewermilestone).
|
||||
// Report them to the server, which logs them and sends the alert back to every overlay.
|
||||
client.on('raw_message', (_copy, m) => {
|
||||
const t = m.tags || {};
|
||||
if (m.command !== 'USERNOTICE' || t['msg-id'] !== 'viewermilestone' || t['msg-param-category'] !== 'watch-streak') return;
|
||||
sendUp({ type: 'chat_notice', kind: 'watch_streak', id: t.id,
|
||||
user: t['display-name'] || t.login, streak: t['msg-param-value'] });
|
||||
});
|
||||
|
||||
client.on('message', (channel, tags, message) => {
|
||||
const now = Date.now();
|
||||
const silence = (now - lastMsgTime) / 1000;
|
||||
|
|
@ -135,9 +148,17 @@
|
|||
}
|
||||
|
||||
// --- Alert WebSocket ---
|
||||
// ?overlay=<id> marks this page as the real overlay, so the server accepts chat_notice messages from it
|
||||
let ws = null;
|
||||
const pending = [];
|
||||
function sendUp(obj) {
|
||||
if (ws && ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(obj));
|
||||
else pending.push(obj);
|
||||
}
|
||||
function connectWS() {
|
||||
const proto = location.protocol === 'https:' ? 'wss' : 'ws';
|
||||
const ws = new WebSocket(`${proto}://${location.host}/ws/overlay`);
|
||||
ws = new WebSocket(`${proto}://${location.host}/ws/overlay?overlay=${encodeURIComponent(S.overlay_id)}`);
|
||||
ws.onopen = () => { while (pending.length) ws.send(JSON.stringify(pending.shift())); };
|
||||
|
||||
ws.onmessage = (e) => {
|
||||
const msg = JSON.parse(e.data);
|
||||
|
|
@ -145,6 +166,12 @@
|
|||
Object.assign(S, msg.settings);
|
||||
return;
|
||||
}
|
||||
if (msg.type === 'alert') {
|
||||
// test alerts replace whatever is showing (fast checking); real ones queue
|
||||
if (msg.alert.test === true) PolaroidAlerts.reset();
|
||||
PolaroidAlerts.play(msg.alert);
|
||||
return;
|
||||
}
|
||||
document.dispatchEvent(new CustomEvent(`heystreamer:${msg.event_type}`, { detail: msg }));
|
||||
handleAction(msg);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue