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

44 lines
1.5 KiB
TypeScript

import type { Series } from '@/types/nostr'
import { objectCache } from './objectCache'
import { parseObjectId } from './urlGenerator'
export async function getSeriesByAuthor(authorPubkey: string, _timeoutMs: number = 5000): Promise<Series[]> {
// Read only from IndexedDB cache
const allSeries = await objectCache.getAll('series')
const series = allSeries as Series[]
// Filter by author pubkey
const authorSeries = series.filter((s) => s.pubkey === authorPubkey)
// Sort by hash (newest first - hash contains timestamp information)
// Since Series doesn't have createdAt, we sort by id which contains version/index
return authorSeries.sort((a, b) => {
// Sort by version descending, then by index descending
if (b.version !== a.version) {
return b.version - a.version
}
return b.index - a.index
})
}
export async function getSeriesById(seriesId: string, _timeoutMs: number = 2000): Promise<Series | null> {
// Try to parse seriesId as id format (<hash>_<index>_<version>) or use it as hash
const parsed = parseObjectId(seriesId)
const hash = parsed.hash ?? seriesId
// Read only from IndexedDB cache
const cached = await objectCache.get('series', hash)
if (cached) {
return cached as Series
}
// Also try by ID if hash lookup failed
const cachedById = await objectCache.getById('series', seriesId)
if (cachedById) {
return cachedById as Series
}
// Not found in cache - return null (no network request)
return null
}