snort/packages/app/src/Cache/index.ts

56 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-04-18 12:17:50 +00:00
import { HexKey, RawEvent, UserMetadata } from "@snort/nostr";
2023-03-03 14:47:14 +00:00
import { hexToBech32, unixNowMs } from "Util";
2023-03-29 12:10:22 +00:00
import { DmCache } from "./DMCache";
2023-04-25 11:57:09 +00:00
import { InteractionCache } from "./EventInteractionCache";
2023-03-29 12:10:22 +00:00
import { UserCache } from "./UserCache";
2023-03-03 14:30:31 +00:00
export interface MetadataCache extends UserMetadata {
/**
* When the object was saved in cache
*/
loaded: number;
/**
* When the source metadata event was created
*/
created: number;
/**
* The pubkey of the owner of this metadata
*/
pubkey: HexKey;
/**
* The bech32 encoded pubkey
*/
npub: string;
2023-03-05 17:54:55 +00:00
/**
* Pubkey of zapper service
*/
zapService?: HexKey;
2023-03-03 14:30:31 +00:00
}
2023-04-18 12:17:50 +00:00
export function mapEventToProfile(ev: RawEvent) {
2023-03-03 14:30:31 +00:00
try {
const data: UserMetadata = JSON.parse(ev.content);
return {
pubkey: ev.pubkey,
npub: hexToBech32("npub", ev.pubkey),
created: ev.created_at,
...data,
2023-03-03 14:47:14 +00:00
loaded: unixNowMs(),
2023-03-03 14:30:31 +00:00
} as MetadataCache;
} catch (e) {
console.error("Failed to parse JSON", ev, e);
}
}
2023-03-29 12:10:22 +00:00
export async function preload() {
await UserCache.preload();
await DmCache.preload();
2023-04-25 11:57:09 +00:00
await InteractionCache.preload();
2023-03-29 12:10:22 +00:00
}
export { UserCache, DmCache };