You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.6 KiB
69 lines
1.6 KiB
/** |
|
* Profile caching (kind 0 events) |
|
*/ |
|
|
|
import { getDB } from './indexeddb-store.js'; |
|
import type { NostrEvent } from '../../types/nostr.js'; |
|
import { KIND } from '../../types/kind-lookup.js'; |
|
|
|
export interface CachedProfile { |
|
pubkey: string; |
|
event: NostrEvent; |
|
cached_at: number; |
|
} |
|
|
|
/** |
|
* Store a profile in cache |
|
*/ |
|
export async function cacheProfile(event: NostrEvent): Promise<void> { |
|
if (event.kind !== KIND.METADATA) throw new Error('Not a profile event'); |
|
try { |
|
const db = await getDB(); |
|
const cached: CachedProfile = { |
|
pubkey: event.pubkey, |
|
event, |
|
cached_at: Date.now() |
|
}; |
|
await db.put('profiles', cached); |
|
} catch (error) { |
|
// Cache write failed (non-critical) |
|
// Don't throw - caching failures shouldn't break the app |
|
} |
|
} |
|
|
|
/** |
|
* Get profile by pubkey from cache |
|
*/ |
|
export async function getProfile(pubkey: string): Promise<CachedProfile | undefined> { |
|
try { |
|
const db = await getDB(); |
|
return await db.get('profiles', pubkey); |
|
} catch (error) { |
|
// Cache read failed (non-critical) |
|
return undefined; |
|
} |
|
} |
|
|
|
/** |
|
* Get multiple profiles |
|
*/ |
|
export async function getProfiles(pubkeys: string[]): Promise<Map<string, CachedProfile>> { |
|
try { |
|
const db = await getDB(); |
|
const profiles = new Map<string, CachedProfile>(); |
|
const tx = db.transaction('profiles', 'readonly'); |
|
|
|
for (const pubkey of pubkeys) { |
|
const profile = await tx.store.get(pubkey); |
|
if (profile) { |
|
profiles.set(pubkey, profile); |
|
} |
|
} |
|
|
|
await tx.done; |
|
return profiles; |
|
} catch (error) { |
|
// Cache read failed (non-critical) |
|
return new Map(); |
|
} |
|
}
|
|
|