feat: in-memory fallback for storing user profiles (#110)

This commit is contained in:
Alejandro
2023-01-27 22:38:41 +01:00
committed by GitHub
parent 2e3fe92b1f
commit d070185322
21 changed files with 369 additions and 123 deletions

View File

@ -1,7 +1,7 @@
import { HexKey, TaggedRawEvent } from "Nostr";
import { ProfileCacheExpire } from "Const";
import { db } from "Db";
import { mapEventToProfile, MetadataCache } from "Db/User";
import { mapEventToProfile, MetadataCache, } from "State/Users";
import { getDb } from "State/Users/Db";
import Connection, { RelaySettings } from "Nostr/Connection";
import Event from "Nostr/Event";
import EventKind from "Nostr/EventKind";
@ -167,7 +167,8 @@ export class NostrSystem {
async _FetchMetadata() {
let missing = new Set<HexKey>();
let meta = await db.users.bulkGet(Array.from(this.WantsMetadata));
const db = getDb()
let meta = await db.bulkGet(Array.from(this.WantsMetadata));
let expire = new Date().getTime() - ProfileCacheExpire;
for (let pk of this.WantsMetadata) {
let m = meta.find(a => a?.pubkey === pk);
@ -190,18 +191,18 @@ export class NostrSystem {
sub.OnEvent = async (e) => {
let profile = mapEventToProfile(e);
if (profile) {
let existing = await db.users.get(profile.pubkey);
if ((existing?.created ?? 0) < profile.created) {
await db.users.put(profile);
} else if (existing) {
await db.users.update(profile.pubkey, { loaded: new Date().getTime() });
let existing = await db.find(profile.pubkey);
if((existing?.created ?? 0) < profile.created) {
await db.put(profile);
} else if(existing) {
await db.update(profile.pubkey, { loaded: new Date().getTime() });
}
}
}
let results = await this.RequestSubscription(sub);
let couldNotFetch = Array.from(missing).filter(a => !results.some(b => b.pubkey === a));
console.debug("No profiles: ", couldNotFetch);
await db.users.bulkPut(couldNotFetch.map(a => {
await db.bulkPut(couldNotFetch.map(a => {
return {
pubkey: a,
loaded: new Date().getTime()
@ -213,4 +214,4 @@ export class NostrSystem {
}
}
export const System = new NostrSystem();
export const System = new NostrSystem();