import { nostrService } from './nostr' import type { Article } from '@/types/nostr' import { getPrimaryRelaySync } from './config' import { buildTagFilter, extractTagsFromEvent } from './nostrTagSystem' function subscribeToPresentation(pool: import('nostr-tools').SimplePool, pubkey: string): Promise { const filters = [ { ...buildTagFilter({ type: 'author', authorPubkey: pubkey, }), limit: 1, }, ] return new Promise((resolve) => { let resolved = false const relayUrl = getPrimaryRelaySync() const { createSubscription } = require('@/types/nostr-tools-extended') const sub = createSubscription(pool, [relayUrl], filters) const finalize = (value: number) => { if (resolved) { return } resolved = true sub.unsub() resolve(value) } sub.on('event', (event: import('nostr-tools').Event) => { // Check if it's an author type using new tag system (tag is 'author' in English) const tags = extractTagsFromEvent(event) if (tags.type !== 'author') { return } const total = (tags.totalSponsoring as number | undefined) ?? 0 finalize(total) }) sub.on('eose', () => finalize(0)) setTimeout(() => finalize(0), 5000) }) } /** * Get total sponsoring for an author by their pubkey */ export function getAuthorSponsoring(pubkey: string): Promise { const pool = nostrService.getPool() if (!pool) { return Promise.resolve(0) } return subscribeToPresentation(pool, pubkey) } /** * Get sponsoring for multiple authors (for sorting) * Returns a map of pubkey -> total sponsoring */ export function getAuthorsSponsoring(pubkeys: string[]): Map { const sponsoringMap = new Map() // For now, we'll extract sponsoring from articles that are already loaded // In a real implementation, we'd query all presentation articles // For performance, we'll use the sponsoring from the article's totalSponsoring field pubkeys.forEach((pubkey) => { sponsoringMap.set(pubkey, 0) }) return sponsoringMap } /** * Get sponsoring from an article (if it's a presentation article) */ export function getSponsoringFromArticle(article: Article): number { if (article.isPresentation && article.totalSponsoring !== undefined) { return article.totalSponsoring } return 0 }