import type { Event } from 'nostr-tools' import { nostrService } from './nostr' import type { SimplePoolWithSub } from '@/types/nostr-tools-extended' import type { Review } from '@/types/nostr' import { parseReviewFromEvent } from './nostrEventParsing' const RELAY_URL = process.env.NEXT_PUBLIC_NOSTR_RELAY_URL ?? 'wss://relay.damus.io' export function getReviewsForArticle(articleId: string, timeoutMs: number = 5000): Promise { const pool = nostrService.getPool() if (!pool) { throw new Error('Pool not initialized') } const poolWithSub = pool as SimplePoolWithSub const filters = [ { kinds: [1], '#article': [articleId], '#kind_type': ['review'], }, ] return new Promise((resolve) => { const results: Review[] = [] const sub = poolWithSub.sub([RELAY_URL], filters) let finished = false const done = () => { if (finished) { return } finished = true sub.unsub() resolve(results) } sub.on('event', (event: Event) => { const parsed = parseReviewFromEvent(event) if (parsed) { results.push(parsed) } }) sub.on('eose', () => done()) setTimeout(() => done(), timeoutMs).unref?.() }) }