import { type Event } from 'nostr-tools' import type { AuthorPresentationDraft } from './articlePublisher' import type { SimplePoolWithSub } from '@/types/nostr-tools-extended' import { buildTags, extractTagsFromEvent, buildTagFilter } from './nostrTagSystem' import { getPrimaryRelaySync } from './config' export function buildPresentationEvent(draft: AuthorPresentationDraft, eventId: string, category: 'sciencefiction' | 'research' = 'sciencefiction') { return { kind: 1 as const, created_at: Math.floor(Date.now() / 1000), tags: buildTags({ type: 'author', category, id: eventId, paywall: false, title: draft.title, preview: draft.preview, mainnetAddress: draft.mainnetAddress, totalSponsoring: 0, ...(draft.pictureUrl ? { pictureUrl: draft.pictureUrl } : {}), }), content: draft.content, } } export function parsePresentationEvent(event: Event): import('@/types/nostr').AuthorPresentationArticle | null { const tags = extractTagsFromEvent(event) // Check if it's an author type (tag is 'author' in English) if (tags.type !== 'author') { return null } return { id: tags.id ?? event.id, pubkey: event.pubkey, title: tags.title ?? 'Présentation', preview: tags.preview ?? event.content.substring(0, 200), content: event.content, createdAt: event.created_at, zapAmount: 0, paid: true, category: 'author-presentation', isPresentation: true, mainnetAddress: tags.mainnetAddress ?? '', totalSponsoring: tags.totalSponsoring ?? 0, ...(tags.pictureUrl !== undefined && tags.pictureUrl !== null && typeof tags.pictureUrl === 'string' ? { bannerUrl: tags.pictureUrl } : {}), } } export function fetchAuthorPresentationFromPool( pool: SimplePoolWithSub, pubkey: string ): Promise { const filters = [ { ...buildTagFilter({ type: 'author', authorPubkey: pubkey, }), limit: 1, }, ] return new Promise((resolve) => { let resolved = false const relayUrl = getPrimaryRelaySync() const sub = pool.sub([relayUrl], filters) const finalize = (value: import('@/types/nostr').AuthorPresentationArticle | null) => { if (resolved) { return } resolved = true sub.unsub() resolve(value) } sub.on('event', (event: Event) => { const parsed = parsePresentationEvent(event) if (parsed) { finalize(parsed) } }) sub.on('eose', () => finalize(null)) setTimeout(() => finalize(null), 5000) }) }