import type { Series } from '@/types/nostr' import { objectCache } from './objectCache' import { parseObjectId } from './urlGenerator' export async function getSeriesByAuthor(authorPubkey: string, _timeoutMs: number = 5000): Promise { // 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 { // Try to parse seriesId as id format (__) 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 }