story-research-zapwall/lib/sponsoring.ts
Nicolas Cantu 2a191f35f4 Fix all TypeScript errors and warnings
- Fix unused function warnings by renaming to _unusedExtractTags
- Fix type errors in nostrTagSystem.ts for includes() calls
- Fix type errors in reviews.ts for filter kinds array
- Fix ArrayBuffer type errors in articleEncryption.ts
- Remove unused imports (DecryptionKey, decryptArticleContent, extractTagsFromEvent)
- All TypeScript checks now pass without disabling any controls
2025-12-27 22:26:13 +01:00

86 lines
2.4 KiB
TypeScript

import { nostrService } from './nostr'
import { SimplePoolWithSub } from '@/types/nostr-tools-extended'
import type { Article } from '@/types/nostr'
const RELAY = process.env.NEXT_PUBLIC_NOSTR_RELAY_URL ?? 'wss://relay.damus.io'
function subscribeToPresentation(pool: SimplePoolWithSub, pubkey: string): Promise<number> {
const { buildTagFilter } = require('./nostrTagSystem')
const filters = [
{
...buildTagFilter({
type: 'author',
authorPubkey: pubkey,
}),
limit: 1,
},
]
return new Promise((resolve) => {
let resolved = false
const sub = pool.sub([RELAY], 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 { extractTagsFromEvent } = require('./nostrTagSystem')
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<number> {
const pool = nostrService.getPool()
if (!pool) {
return Promise.resolve(0)
}
return subscribeToPresentation(pool as SimplePoolWithSub, pubkey)
}
/**
* Get sponsoring for multiple authors (for sorting)
* Returns a map of pubkey -> total sponsoring
*/
export function getAuthorsSponsoring(pubkeys: string[]): Map<string, number> {
const sponsoringMap = new Map<string, number>()
// 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
}