// Nostr relay pool and connection management import { SimplePool, Event as NostrEvent } from 'nostr-tools' const DEFAULT_RELAYS = [ 'wss://relay.damus.io', 'wss://nos.lol', 'wss://relay.nostr.band', 'wss://relay.snort.social' ] // Kind numbers for IndeeHub content types export const NOSTR_KINDS = { VIDEO_HORIZONTAL: 34235, // NIP-71 Video horizontal VIDEO_VERTICAL: 34236, // NIP-71 Video vertical LONG_FORM: 30023, // NIP-23 Long-form content SHORT_FORM: 1, // Regular notes for shorts } class NostrService { private pool: SimplePool private relays: string[] constructor(relays: string[] = DEFAULT_RELAYS) { this.pool = new SimplePool() this.relays = relays } /** * Fetch video content from Nostr * Using NIP-71 for video events */ async fetchVideos(limit: number = 50): Promise { try { const events = await this.pool.querySync(this.relays, { kinds: [NOSTR_KINDS.VIDEO_HORIZONTAL, NOSTR_KINDS.VIDEO_VERTICAL], limit }) return events } catch (error) { console.error('Error fetching videos from Nostr:', error) return [] } } /** * Fetch content by creator (pubkey) */ async fetchByCreator(pubkey: string, limit: number = 20): Promise { try { const events = await this.pool.querySync(this.relays, { kinds: [NOSTR_KINDS.VIDEO_HORIZONTAL, NOSTR_KINDS.VIDEO_VERTICAL], authors: [pubkey], limit }) return events } catch (error) { console.error('Error fetching creator content:', error) return [] } } /** * Subscribe to new content events */ subscribeToContent( callback: (event: NostrEvent) => void, kinds: number[] = [NOSTR_KINDS.VIDEO_HORIZONTAL, NOSTR_KINDS.VIDEO_VERTICAL] ) { const sub = this.pool.subscribeMany( this.relays, { kinds, limit: 10 }, { onevent(event) { callback(event) } } ) return () => sub.close() } /** * Publish a view/watch event (NIP-XX view tracking — not yet implemented) */ async publishView(_videoEventId: string) { // View tracking via Nostr not yet implemented } /** * Close all connections */ close() { this.pool.close(this.relays) } } export const nostrService = new NostrService()