// Live streams (NIP-53). A stream is a kind 30311 event: its title, cover, // where to watch (`streaming`, almost always an HLS playlist), whether it's // on (`status`), how many watch, and who's in it (p tags with a role). Its // chat is kind 1311, addressed to the stream with an `a` tag; zaps carry // that `a` too, which is how zap.stream and the rest count them. export const LIVE_KIND = 30311; export const LIVE_CHAT_KIND = 1311; // Where streams are announced: zap.stream, Shosho and the rest publish to // the big public relays (a 2026-10-18 survey found every live one on damus, // nos.lol and ditto, most on primal; relay.zap.stream held none). export const LIVE_RELAYS = ['wss://relay.damus.io', 'wss://nos.lol', 'wss://relay.primal.net', 'wss://relay.ditto.pub']; // A stream still marked live that hasn't been refreshed in an hour is over: // plenty of publishers never say `ended` (NIP-53 allows reading it so). export const LIVE_STALE_S = 3600; // https, or a stream on this machine (a test's, a streamer's own preview) const WATCHABLE = /^(https:\/\/\S+|http:\/\/(localhost|127\.0\.0\.1)(:\d+)?\/\S*)$/; const tag = (ev, k) => ((ev && ev.tags) || []).find((x) => x[0] === k)?.[1] || ''; export const liveAddr = (ev) => LIVE_KIND + ':' + ev.pubkey + ':' + tag(ev, 'd'); export const liveTitle = (ev) => tag(ev, 'title').trim(); export const liveSummary = (ev) => tag(ev, 'summary').trim(); export const liveImage = (ev) => { const u = tag(ev, 'image').trim(); return /^https:\/\/\S+$/.test(u) ? u : ''; }; export const liveViewers = (ev) => { const n = parseInt(tag(ev, 'current_participants'), 10); return n >= 0 ? n : null; }; export const liveStarts = (ev) => +tag(ev, 'starts') || ev.created_at; // The person streaming: the host named in its p tags (zap.stream signs the // event with its own key and names the streamer as Host), else its author. export function liveHost(ev) { const p = (ev.tags || []).find((x) => x[0] === 'p' && /^[0-9a-f]{64}$/.test(x[1] || '') && /^host$/i.test(x[3] || '')); return p ? p[1] : ev.pubkey; } // Where to watch: an HLS playlist first (what browsers can play), else any https address. export function liveStream(ev) { const urls = (ev.tags || []).filter((x) => x[0] === 'streaming' && WATCHABLE.test(x[1] || '')).map((x) => x[1]); return urls.find((u) => /\.m3u8(\?|#|$)/i.test(u)) || urls[0] || ''; } export function liveRecording(ev) { const u = tag(ev, 'recording'); return WATCHABLE.test(u) ? u : ''; } export function liveStatus(ev, now = Date.now() / 1000) { const s = tag(ev, 'status').toLowerCase(); if (s === 'live') return now - ev.created_at > LIVE_STALE_S ? 'ended' : 'live'; return s === 'planned' ? 'planned' : 'ended'; } export const isLiveNow = (ev, now) => liveStatus(ev, now) === 'live' && !!liveStream(ev); // The relays the stream names for its chat, then the ones it's announced on. export function liveRelays(ev) { const named = (ev.tags || []).filter((x) => x[0] === 'relays').flatMap((x) => x.slice(1)).filter((r) => /^wss:\/\/\S+$/.test(r)); return [...new Set([...named.slice(0, 6), ...LIVE_RELAYS])]; } // Newest copy of each stream (it's replaceable: the address is the stream). export function newestByAddr(evs) { const out = new Map(); for (const ev of evs) { if (!ev || ev.kind !== LIVE_KIND) continue; const a = liveAddr(ev), cur = out.get(a); if (!cur || cur.created_at < ev.created_at) out.set(a, ev); } return out; } // The zap on a chat line: a 9735 receipt's amount from its bolt11, its // sender and words from the zap request inside it; coinos's own Ark receipt // (9737) says them in tags. `decode` is the bolt11 decoder (millisats). export function zapLine(ev, decode) { if (ev.kind === 9737) { return { pk: tag(ev, 'P') || ev.pubkey, sats: +tag(ev, 'amount') || 0, text: ev.content || '' }; } let req = null; try { req = JSON.parse(tag(ev, 'description')); } catch {} let sats = 0; try { sats = Math.floor(Number(decode(tag(ev, 'bolt11'))?.amountMsat || 0) / 1000); } catch {} if (!sats && req) sats = Math.floor((+((req.tags || []).find((x) => x[0] === 'amount') || [])[1] || 0) / 1000); return { pk: (req && /^[0-9a-f]{64}$/.test(req.pubkey || '') && req.pubkey) || tag(ev, 'P') || '', sats, text: (req && req.content) || '' }; } // Zaps aimed at a stream carry its address: the zap request (kind 9734) and // coinos's Ark receipt look the event they're zapping up here. const zapAddrs = new Map(); export const setZapAddr = (eventId, addr) => { if (eventId && addr) zapAddrs.set(eventId, addr); }; export const zapAddrOf = (eventId) => (eventId && zapAddrs.get(eventId)) || null; // One current reaction per person/message, independent of relay arrival order. // A deletion only removes reactions signed by its own author. export function liveReactions(events) { const deleted = new Set(); for (const ev of events) if (ev.kind === 5) for (const tag of ev.tags || []) if (tag[0] === 'e') deleted.add(ev.pubkey + ':' + tag[1]); const out = new Map(); for (const ev of events) { if (ev.kind !== 7 || deleted.has(ev.pubkey + ':' + ev.id)) continue; const target = (ev.tags || []).filter(t => t[0] === 'e').at(-1)?.[1]; if (!target) continue; const by = out.get(target) || new Map(); const old = by.get(ev.pubkey); if (!old || ev.created_at > old.created_at || (ev.created_at === old.created_at && ev.id > old.id)) by.set(ev.pubkey, ev); out.set(target, by); } return out; } 'n33' href='#n33'>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