story-research-zapwall/lib/authorQueries.ts
2026-01-14 01:08:33 +01:00

68 lines
2.4 KiB
TypeScript

/**
* Query authors by hash ID or pubkey (for backward compatibility)
* Read only from IndexedDB - no network requests
*/
import type { SimplePoolWithSub } from '@/types/nostr-tools-extended'
import type { AuthorPresentationArticle, NostrProfile } from '@/types/nostr'
import { extractAuthorNameFromTitle } from './authorPresentationParsing'
import { objectCache } from './objectCache'
/**
* Fetch author presentation by hash ID or pubkey
* Read only from IndexedDB - no network requests
* If the parameter looks like a pubkey (64 hex chars), it uses pubkey lookup
* Otherwise, it uses hash ID lookup
*/
export async function fetchAuthorByHashId(
_pool: SimplePoolWithSub,
hashIdOrPubkey: string
): Promise<import('@/types/nostr').AuthorPresentationArticle | null> {
// Check if it's a pubkey (64 hex characters) for backward compatibility
if (/^[a-f0-9]{64}$/i.test(hashIdOrPubkey)) {
// Read only from IndexedDB cache
const cached = await objectCache.getAuthorByPubkey(hashIdOrPubkey)
if (cached) {
const presentation = cached
// Calculate totalSponsoring from cache
const { getAuthorSponsoring } = await import('./sponsoring')
presentation.totalSponsoring = await getAuthorSponsoring(presentation.pubkey)
return presentation
}
// Not found in cache - return null (no network request)
return null
}
// Otherwise, treat as hash ID
const hashId = hashIdOrPubkey
const { getCachedObjectById } = await import('./helpers/queryHelpers')
const cached = await getCachedObjectById<import('@/types/nostr').AuthorPresentationArticle>('author', hashId)
if (cached) {
const presentation = cached
// Calculate totalSponsoring from cache
const { getAuthorSponsoring } = await import('./sponsoring')
presentation.totalSponsoring = await getAuthorSponsoring(presentation.pubkey)
return presentation
}
// Not found in cache - return null (no network request)
return null
}
/**
* Convert AuthorPresentationArticle to NostrProfile for display purposes
* Extracts name from title, uses bannerUrl as picture, description as about
*/
export function convertPlatformProfileToNostrProfile(
presentation: AuthorPresentationArticle
): NostrProfile {
const authorName = extractAuthorNameFromTitle(presentation.title)
const profile: NostrProfile = {
pubkey: presentation.pubkey,
name: authorName,
about: presentation.description,
picture: presentation.bannerUrl,
}
return profile
}