heatsync-tui 0.2.0

heat-sorted live multichat in the terminal — twitch, kick + youtube (read-only), real emotes, vim keys
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
//! blocking HTTP against the public HeatSync API (no auth). emote sets + image
//! bytes for the TUI, and the archive/corpus reads for the CLI subcommands.

use std::io::Read;
use std::sync::OnceLock;
use std::time::Duration;

use heatsync_core::emote::{EmoteSet, EmoteSetResponse};
use heatsync_core::Platform;
use serde::Deserialize;

pub const BASE: &str = "https://heatsync.org";
/// staged for the emote image-render layer (next phase).
#[allow(dead_code)]
const MAX_IMAGE_BYTES: u64 = 8 * 1024 * 1024;

fn platform_q(p: Platform) -> &'static str {
    match p {
        Platform::Twitch => "twitch",
        Platform::Kick => "kick",
        Platform::Youtube => "youtube",
    }
}

/// one shared agent for the whole process — its connection pool reuses TLS
/// sessions, so a burst of 30 emote fetches from one CDN pays one handshake,
/// not thirty. thread-safe; the loader pool and emote-set threads all share it.
fn agent() -> &'static ureq::Agent {
    static AGENT: OnceLock<ureq::Agent> = OnceLock::new();
    AGENT.get_or_init(|| {
        ureq::AgentBuilder::new()
            .timeout(Duration::from_secs(12))
            // the archive search endpoint gates non-browser UAs (anti-scrape); we're
            // first-party, so identify as heatsync-tui behind a Mozilla token so the
            // guard passes. proper fix later: server-side allowlist this UA.
            .user_agent("Mozilla/5.0 (heatsync-tui/0.1; +https://heatsync.org)")
            .build()
    })
}

/// the channel's current pin (`/api/pin`), if it has one. twitch + kick only.
pub fn pin(platform: Platform, channel: &str) -> Option<heatsync_core::Pin> {
    if platform == Platform::Youtube {
        return None;
    }
    let v: serde_json::Value = agent()
        .get(&format!("{BASE}/api/pin"))
        .query("platform", platform_q(platform))
        .query("channel", &channel.to_lowercase())
        .call()
        .ok()?
        .into_json()
        .ok()?;
    heatsync_core::proto::pin_from(v.get("pin")?)
}

/// the pooled global emote set (/api/emotes): twitch globals + event-unlocked
/// emotes, and the bttv/ffz/7tv global sets — merged and deduped server-side.
pub fn global_emotes() -> Option<EmoteSet> {
    let resp: EmoteSetResponse = agent()
        .get(&format!("{BASE}/api/emotes"))
        .call()
        .ok()?
        .into_json()
        .ok()?;
    let mut set = EmoteSet::from_list(resp.emotes);
    set.mark_global();
    Some(set)
}

/// `/api/paints` for a batch of paint-space ids (<=50): heatsync paint specs
/// and picked name colors. `None` on any failure so the caller retries.
pub fn paints(ids: &[String]) -> Option<serde_json::Value> {
    let url = format!("{BASE}/api/paints?ids={}", ids.join(","));
    agent().get(&url).call().ok()?.into_json().ok()
}

/// `/api/cosmetics/batch`: 7tv paints for twitch ids and `kick_<id>`s (<=50).
pub fn cosmetics(ids: &[String]) -> Option<serde_json::Value> {
    let url = format!("{BASE}/api/cosmetics/batch?ids={}", ids.join(","));
    agent().get(&url).call().ok()?.into_json().ok()
}

/// channel emote set (7tv/bttv/ffz precedence, deduped server-side).
pub fn emote_set(channel: &str, platform: Platform) -> Option<EmoteSet> {
    let url = format!(
        "{BASE}/api/channel/{channel}/emotes?platform={}",
        platform_q(platform)
    );
    let resp: EmoteSetResponse = agent().get(&url).call().ok()?.into_json().ok()?;
    Some(EmoteSet::from_list(resp.emotes))
}

/// raw image bytes for an emote, capped so a hostile CDN can't balloon memory.
/// staged for the emote image-render layer (next phase).
#[allow(dead_code)]
pub fn image_bytes(url: &str) -> Option<Vec<u8>> {
    let mut buf = Vec::new();
    agent()
        .get(url)
        .call()
        .ok()?
        .into_reader()
        .take(MAX_IMAGE_BYTES)
        .read_to_end(&mut buf)
        .ok()?;
    Some(buf)
}

// ---- corpus / archive reads (CLI) ----------------------------------------

/// one archived chat row (subset of the archive API shape).
#[derive(Debug, Deserialize)]
pub struct ArchiveMsg {
    #[serde(default)]
    pub platform: String,
    #[serde(default)]
    pub channel: String,
    #[serde(default)]
    pub username: String,
    #[serde(default)]
    pub display_name: Option<String>,
    #[serde(default)]
    pub message: String,
    #[serde(default)]
    pub timestamp: String,
    /// `[{name, version}]` — same shape the kick ws frames use.
    #[serde(default)]
    pub badges: Option<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
pub struct ArchivePage {
    #[serde(default)]
    pub results: Vec<ArchiveMsg>,
    /// staged for CLI pagination (--page / follow).
    #[serde(default)]
    #[allow(dead_code)]
    pub next_cursor: Option<String>,
}

/// full-text search the archive.
pub fn search(q: &str, channel: Option<&str>, limit: u32) -> Option<ArchivePage> {
    let mut url = format!("{BASE}/api/archive/search?limit={limit}&q={}", urlencode(q));
    if let Some(c) = channel {
        url.push_str(&format!("&channel={}", urlencode(c)));
    }
    agent().get(&url).call().ok()?.into_json().ok()
}

/// the site feed's latest `want` rows, oldest first — the feed tab's backlog.
/// anonymous works; rows pass through the same parser the live socket uses, so
/// they are sanitized at the same trust boundary.
pub fn feed_recent(want: usize) -> Vec<heatsync_core::proto::SpecialLine> {
    let url = format!("{BASE}/api/messages?sort=time&limit={want}");
    let Some(v) = agent()
        .get(&url)
        .call()
        .ok()
        .and_then(|r| r.into_json::<serde_json::Value>().ok())
    else {
        return Vec::new();
    };
    let mut rows: Vec<_> = v
        .get("messages")
        .and_then(serde_json::Value::as_array)
        .map(|a| {
            a.iter()
                .filter_map(heatsync_core::proto::feed_row)
                .collect()
        })
        .unwrap_or_default();
    // the api answers newest first; the tab reads oldest first.
    rows.reverse();
    rows
}

/// a channel's log for a UTC date (YYYY-MM-DD).
pub fn channel_log(channel: &str, from: &str, to: &str, limit: u32) -> Option<ArchivePage> {
    let url =
        format!("{BASE}/api/archive/channel/{channel}/messages?from={from}&to={to}&limit={limit}");
    agent().get(&url).call().ok()?.into_json().ok()
}

/// the last messages a channel saw, oldest first — scrollback seed for a fresh
/// tab. the archive reads ascending, so we ask for a recent window and keep the
/// tail, widening the window when a quiet channel comes back thin. rows are
/// sanitized here — archive content crosses the same trust boundary live chat
/// does.
pub fn recent(channel: &str, platform: Platform, want: usize) -> Vec<heatsync_core::Message> {
    use heatsync_core::{proto::badges_from, sanitize};
    let mut best: Vec<ArchiveMsg> = Vec::new();
    for mins in [10u64, 60, 480] {
        let from = iso_from_unix(unix_now().saturating_sub(mins * 60));
        let to = iso_from_unix(unix_now());
        let Some(page) = channel_log(channel, &from, &to, 200) else {
            continue;
        };
        let rows: Vec<ArchiveMsg> = page
            .results
            .into_iter()
            .filter(|r| r.platform == platform_q(platform))
            .collect();
        let enough = rows.len() >= want;
        best = rows;
        if enough {
            break;
        }
    }
    let skip = best.len().saturating_sub(want);
    best.drain(..skip);
    best.into_iter()
        .map(|r| {
            let user = sanitize::clean(r.display_name.as_deref().unwrap_or(&r.username));
            heatsync_core::Message {
                platform,
                user: if user.is_empty() {
                    sanitize::clean(&r.username)
                } else {
                    user
                },
                text: sanitize::clean(&r.message),
                color: None,
                badges: badges_from(r.badges.as_ref()),
                id: None,
                gone: None,
                reply_to: None,
                note: None,
                // its own time: stamps show, and a stream-start row can tell
                // whether this history reaches back that far.
                extra: heatsync_core::Extra {
                    ts: heatsync_core::iso::parse(&r.timestamp).map_or(0, |t| t.max(0) as u64),
                    ..Default::default()
                },
                emotes: None,
                tags: Vec::new(),
                heat: 0.0,
                uid: None,
            }
        })
        .filter(|m| !m.user.is_empty() || !m.text.is_empty())
        .collect()
}

/// twitch's public web gql, the same one the heatsync desktop app's /status
/// asks: no login, 3s at most, answer capped.
const GQL: &str = "https://gql.twitch.tv/gql";
const GQL_CLIENT_ID: &str = "kimne78kx3ncx6brgo4mv6wki5h1ko";
const MAX_GQL: u64 = 1 << 20;

/// (stream start in unix seconds, game) if `login` is live on twitch right now.
/// `None` when offline, unknown, or anything at all goes wrong.
pub fn twitch_live(login: &str) -> Option<(u64, String)> {
    let ok = !login.is_empty()
        && login.len() <= 25
        && login
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b == b'_');
    if !ok {
        return None;
    }
    let body = serde_json::json!({
        "query": "query($login:String!){user(login:$login){displayName stream{type viewersCount createdAt game{name}} broadcastSettings{title language}}}",
        "variables": { "login": login.to_lowercase() },
    });
    let resp = agent()
        .post(GQL)
        .set("Client-Id", GQL_CLIENT_ID)
        .timeout(Duration::from_secs(3))
        .send_json(body)
        .ok()?;
    let v: serde_json::Value = serde_json::from_reader(resp.into_reader().take(MAX_GQL)).ok()?;
    parse_live(&v)
}

/// gql answer → (start, game) of a live stream.
fn parse_live(v: &serde_json::Value) -> Option<(u64, String)> {
    let s = v.pointer("/data/user/stream").filter(|s| s.is_object())?;
    if s.get("type").and_then(|t| t.as_str()) != Some("live") {
        return None;
    }
    let at = s.get("createdAt").and_then(|t| t.as_str())?;
    let start = heatsync_core::iso::parse(at).filter(|t| *t > 0)? as u64;
    let game = s
        .pointer("/game/name")
        .and_then(|g| g.as_str())
        .map(|g| heatsync_core::sanitize::clean(g).chars().take(80).collect())
        .unwrap_or_default();
    Some((start, game))
}

fn unix_now() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

/// unix seconds → `YYYY-MM-DDThh:mm:ssZ`. hand-rolled civil-date conversion
/// (Hinnant's algorithm) — fifteen tested lines beat a date dependency.
fn iso_from_unix(t: u64) -> String {
    let (h, mi, s) = ((t % 86400) / 3600, (t % 3600) / 60, t % 60);
    let z = (t / 86400) as i64 + 719_468;
    let era = z.div_euclid(146_097);
    let doe = z.rem_euclid(146_097);
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let d = doy - (153 * mp + 2) / 5 + 1;
    let m = if mp < 10 { mp + 3 } else { mp - 9 };
    let y = yoe + era * 400 + i64::from(m <= 2);
    format!("{y:04}-{m:02}-{d:02}T{h:02}:{mi:02}:{s:02}Z")
}

/// hottest posts across the platform (these carry real heat).
#[derive(Debug, Deserialize)]
pub struct HotMsg {
    #[serde(default)]
    pub content: String,
    #[serde(default)]
    pub heat: f64,
    #[serde(default)]
    pub max_heat: f64,
    #[serde(default)]
    pub display_name: Option<String>,
    #[serde(default)]
    pub username: String,
    #[serde(default)]
    pub subject: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct HotPage {
    #[serde(default)]
    pub messages: Vec<HotMsg>,
}

pub fn hot(limit: u32, hours: u32) -> Option<HotPage> {
    let url = format!("{BASE}/api/messages/hot?limit={limit}&hours={hours}");
    agent().get(&url).call().ok()?.into_json().ok()
}

// ---- live lists (the channel picker) ---------------------------------------

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LiveStream {
    #[serde(default)]
    pub username: String,
    #[serde(default)]
    pub platform: String,
    #[serde(default)]
    pub title: String,
    #[serde(default)]
    pub category: String,
    #[serde(default)]
    pub viewer_count: u64,
    #[serde(default)]
    pub video_id: Option<String>,
    #[serde(default)]
    pub platform_usernames: std::collections::HashMap<String, String>,
}

#[derive(Debug, Deserialize)]
struct LivePage {
    #[serde(default)]
    streams: Vec<LiveStream>,
}

/// `/api/live/top` — the busiest live streams across twitch/kick/youtube.
pub fn live_top(limit: u32) -> Option<Vec<LiveStream>> {
    let url = format!("{BASE}/api/live/top?limit={limit}&sort=viewers");
    let page: LivePage = agent().get(&url).call().ok()?.into_json().ok()?;
    Some(page.streams)
}

#[derive(Debug, Deserialize)]
struct LiveStatus {
    #[serde(default)]
    live: Vec<String>,
    #[serde(default, rename = "kickLive")]
    kick_live: Vec<String>,
}

/// `/api/platform/live-status` — which of these twitch / kick channels are live
/// right now (the server caps each list at 20). names only, no viewer counts.
pub fn live_status(twitch: &[String], kick: &[String]) -> Option<(Vec<String>, Vec<String>)> {
    if twitch.is_empty() && kick.is_empty() {
        return Some((Vec::new(), Vec::new()));
    }
    let url = format!(
        "{BASE}/api/platform/live-status?channels={}&kick_channels={}",
        urlencode(&twitch.join(",")),
        urlencode(&kick.join(","))
    );
    let st: LiveStatus = agent().get(&url).call().ok()?.into_json().ok()?;
    Some((st.live, st.kick_live))
}

// ---- admin status (CLI, mellen-only) --------------------------------------

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HealthData {
    #[serde(default)]
    pub messages_per_minute: f64,
    #[serde(default)]
    pub postgres_status: String,
    #[serde(default)]
    pub redis_status: String,
    #[serde(default)]
    pub ws_connections: i64,
    #[serde(default)]
    pub cpu_usage: f64,
    #[serde(default)]
    pub ram_usage: f64,
    #[serde(default)]
    pub disk_usage: f64,
    #[serde(default)]
    pub error_rate: f64,
}

#[derive(Debug, Deserialize)]
pub struct CountPage {
    #[serde(default)]
    pub total: i64,
}

#[derive(Debug, Deserialize)]
pub struct ReportsPage {
    #[serde(default)]
    pub reports: Vec<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
pub struct NcmecPage {
    #[serde(default)]
    pub count: i64,
}

fn admin_get<T: for<'de> Deserialize<'de>>(path: &str, token: &str) -> Option<T> {
    agent()
        .get(&format!("{BASE}{path}"))
        .set("Authorization", &format!("Bearer {token}"))
        .call()
        .ok()?
        .into_json()
        .ok()
}

pub fn admin_health(token: &str) -> Option<HealthData> {
    admin_get("/api/admin/health", token)
}

/// is the admin token itself refused (401/403)? false for a down server or a
/// good token — only a clear "no" from heatsync counts.
pub fn admin_token_rejected(base: &str, token: &str) -> bool {
    matches!(
        agent()
            .get(&format!("{base}/api/admin/health"))
            .set("Authorization", &format!("Bearer {token}"))
            .call(),
        Err(ureq::Error::Status(401 | 403, _))
    )
}

/// moderation-queue depth (items awaiting review).
pub fn admin_mod_queue_total(token: &str) -> Option<i64> {
    admin_get::<CountPage>("/api/admin/moderation-queue?limit=1", token).map(|p| p.total)
}

/// pending user reports.
pub fn admin_pending_reports(token: &str) -> Option<i64> {
    admin_get::<ReportsPage>("/api/admin/reports?status=pending", token)
        .map(|p| p.reports.len() as i64)
}

/// NCMEC reports stuck in a failed/manual state — never routine, always worth surfacing.
pub fn admin_ncmec_backlog(token: &str) -> Option<i64> {
    admin_get::<NcmecPage>("/api/admin/ncmec-reports", token).map(|p| p.count)
}

/// minimal percent-encoding for query values (alnum + a few safe chars pass).
fn urlencode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(b as char)
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

#[cfg(test)]
mod tests {
    #[test]
    fn the_gql_answer_gives_a_live_streams_start_and_game() {
        let live = serde_json::json!({"data": {"user": {"stream": {"type": "live",
            "createdAt": "2026-11-03T10:00:00Z", "game": {"name": "Just \u{7}Chatting"}}}}});
        assert_eq!(
            parse_live(&live),
            Some((1_793_700_000, "Just Chatting".into()))
        );
        let no_game = serde_json::json!({"data": {"user": {"stream": {"type": "live",
            "createdAt": "2026-11-03T10:00:00Z", "game": null}}}});
        assert_eq!(parse_live(&no_game).map(|l| l.1), Some(String::new()));
        for bad in [
            serde_json::json!({"data": {"user": {"stream": null}}}),
            serde_json::json!({"data": {"user": null}}),
            serde_json::json!({"data": {"user": {"stream": {"type": "rerun", "createdAt": "2026-11-03T10:00:00Z"}}}}),
            serde_json::json!({"data": {"user": {"stream": {"type": "live", "createdAt": "junk"}}}}),
            serde_json::json!("junk"),
        ] {
            assert_eq!(parse_live(&bad), None);
        }
        assert_eq!(
            twitch_live("a b"),
            None,
            "a bad login never leaves the machine"
        );
    }

    use super::{iso_from_unix, parse_live, twitch_live};

    #[test]
    fn iso_conversion_hits_known_instants() {
        assert_eq!(iso_from_unix(0), "1970-01-01T00:00:00Z");
        assert_eq!(iso_from_unix(1_735_689_600), "2025-01-01T00:00:00Z");
        assert_eq!(iso_from_unix(951_782_400), "2000-02-29T00:00:00Z"); // leap day
        assert_eq!(iso_from_unix(1_787_697_045), "2026-08-25T22:30:45Z");
    }
}