story-research-zapwall/lib/authorQueries.ts
2026-01-07 01:51:26 +01:00

59 lines
2.2 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 { 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 as import('@/types/nostr').AuthorPresentationArticle
// 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
// Read only from IndexedDB cache
const cached = await objectCache.get('author', hashId)
if (cached) {
const presentation = cached as import('@/types/nostr').AuthorPresentationArticle
// Calculate totalSponsoring from cache
const { getAuthorSponsoring } = await import('./sponsoring')
presentation.totalSponsoring = await getAuthorSponsoring(presentation.pubkey)
return presentation
}
// Also try by ID if hash lookup failed
const cachedById = await objectCache.getById('author', hashId)
if (cachedById) {
const presentation = cachedById as import('@/types/nostr').AuthorPresentationArticle
// 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
}