feat: in-memory fallback for storing user profiles (#110)
This commit is contained in:
@ -1,39 +0,0 @@
|
||||
import { HexKey, TaggedRawEvent, UserMetadata } from "Nostr";
|
||||
import { hexToBech32 } from "../Util";
|
||||
|
||||
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,
|
||||
|
||||
/**
|
||||
* bech32 encoded pub key
|
||||
*/
|
||||
npub: string
|
||||
};
|
||||
|
||||
export function mapEventToProfile(ev: TaggedRawEvent) {
|
||||
try {
|
||||
let data: UserMetadata = JSON.parse(ev.content);
|
||||
return {
|
||||
pubkey: ev.pubkey,
|
||||
npub: hexToBech32("npub", ev.pubkey),
|
||||
created: ev.created_at,
|
||||
loaded: new Date().getTime(),
|
||||
...data
|
||||
} as MetadataCache;
|
||||
} catch (e) {
|
||||
console.error("Failed to parse JSON", ev, e);
|
||||
}
|
||||
}
|
@ -1,16 +1,20 @@
|
||||
import Dexie, { Table } from "dexie";
|
||||
import { MetadataCache } from "Db/User";
|
||||
import { MetadataCache } from "State/Users";
|
||||
import { hexToBech32 } from "Util";
|
||||
|
||||
export const NAME = 'snortDB'
|
||||
export const VERSION = 2
|
||||
|
||||
const STORES = {
|
||||
users: '++pubkey, name, display_name, picture, nip05, npub'
|
||||
}
|
||||
|
||||
export class SnortDB extends Dexie {
|
||||
users!: Table<MetadataCache>;
|
||||
|
||||
constructor() {
|
||||
super('snortDB');
|
||||
this.version(2).stores({
|
||||
users: '++pubkey, name, display_name, picture, nip05, npub'
|
||||
}).upgrade(tx => {
|
||||
super(NAME);
|
||||
this.version(VERSION).stores(STORES).upgrade(tx => {
|
||||
return tx.table("users").toCollection().modify(user => {
|
||||
user.npub = hexToBech32("npub", user.pubkey)
|
||||
})
|
||||
|
Reference in New Issue
Block a user