#!/usr/bin/env node // Saca los últimos posts de un feed SSB en JSON, para tools/directo.py. // Se ejecuta en el servidor del pub, con el sbot de OASIS arrancado. // Uso: OASIS_DIR=~/oasis node tools/ultimos.js @feed.ed25519 [N] const path = require('path'); const dir = process.env.OASIS_DIR || path.join(process.env.HOME, 'oasis'); const ssbConfig = require(path.join(dir, 'src', 'server', 'ssb_config')); const ssbClient = require(path.join(dir, 'src', 'server', 'node_modules', 'ssb-client')); const id = process.argv[2]; const n = Math.min(50, Math.max(1, parseInt(process.argv[3] || '5', 10))); if (!id || !id.startsWith('@')) { console.error('uso: node tools/ultimos.js @feed.ed25519 [N]'); process.exit(1); } const pub = (ssbConfig.keys.public || '').replace('.ed25519', ''); const remote = `unix:${path.join(ssbConfig.path, 'socket')}~noauth:${pub}`; ssbClient(ssbConfig.keys, { remote, caps: ssbConfig.caps }).then((ssb) => { const out = []; const src = ssb.createUserStream({ id, reverse: true, limit: 200 }); const next = () => src(null, (end, msg) => { if (end || out.length >= n) { src(true, () => {}); ssb.close(() => console.log(JSON.stringify(out))); return; } const c = msg.value && msg.value.content; if (c && c.type === 'post' && typeof c.text === 'string') { const img = (Array.isArray(c.mentions) ? c.mentions : []) .find((m) => m && typeof m.link === 'string' && m.link.startsWith('&') && typeof m.type === 'string' && m.type.startsWith('image/')); // OASIS no siempre pone type en mentions: se mira también el markdown const md = c.text.match(/!\[[^\]]*\]\((&[A-Za-z0-9+/=]+\.sha256)\)/); out.push({ key: msg.key, ts: msg.value.timestamp, text: c.text, img: img ? img.link : (md ? md[1] : null), }); } next(); }); next(); }).catch((e) => { console.error(e.message); process.exit(1); });