36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import type { Sponsoring } from '@/types/nostr'
|
|
import { objectCache } from './objectCache'
|
|
import { parseObjectId } from './urlGenerator'
|
|
|
|
export async function getSponsoringById(sponsoringId: string, _timeoutMs: number = 5000): Promise<Sponsoring | null> {
|
|
const parsed = parseObjectId(sponsoringId)
|
|
const hash = parsed.hash ?? sponsoringId
|
|
|
|
// Read only from IndexedDB cache
|
|
const cached = await objectCache.get('sponsoring', hash)
|
|
if (cached) {
|
|
return cached as Sponsoring
|
|
}
|
|
|
|
// Also try by ID if hash lookup failed
|
|
const cachedById = await objectCache.getById('sponsoring', sponsoringId)
|
|
if (cachedById) {
|
|
return cachedById as Sponsoring
|
|
}
|
|
|
|
// Not found in cache - return null (no network request)
|
|
return null
|
|
}
|
|
|
|
export async function getSponsoringByAuthor(authorPubkey: string, _timeoutMs: number = 5000): Promise<Sponsoring[]> {
|
|
// Read only from IndexedDB cache
|
|
const allSponsoring = await objectCache.getAll('sponsoring')
|
|
const sponsoring = allSponsoring as Sponsoring[]
|
|
|
|
// Filter by authorPubkey
|
|
const authorSponsoring = sponsoring.filter((s) => s.authorPubkey === authorPubkey)
|
|
|
|
// Sort by creation date descending
|
|
return authorSponsoring.sort((a, b) => b.createdAt - a.createdAt)
|
|
}
|