forked from Kieran/snort
feat: offload zapper key loading to timer task
This commit is contained in:
parent
8f504cfef9
commit
ed878f57f4
@ -1,11 +1,14 @@
|
|||||||
import FeedCache from "Cache/FeedCache";
|
import FeedCache from "Cache/FeedCache";
|
||||||
import { db } from "Db";
|
import { db } from "Db";
|
||||||
import { LNURL } from "LNURL";
|
|
||||||
import { MetadataCache } from "Cache";
|
import { MetadataCache } from "Cache";
|
||||||
|
import { LNURL } from "LNURL";
|
||||||
|
|
||||||
class UserProfileCache extends FeedCache<MetadataCache> {
|
class UserProfileCache extends FeedCache<MetadataCache> {
|
||||||
|
#zapperQueue: Array<{ pubkey: string; lnurl: string }> = [];
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super("UserCache", db.users);
|
super("UserCache", db.users);
|
||||||
|
this.#processZapperQueue();
|
||||||
}
|
}
|
||||||
|
|
||||||
key(of: MetadataCache): string {
|
key(of: MetadataCache): string {
|
||||||
@ -63,38 +66,59 @@ class UserProfileCache extends FeedCache<MetadataCache> {
|
|||||||
})();
|
})();
|
||||||
console.debug(`Updating ${m.pubkey} ${updateType}`, m);
|
console.debug(`Updating ${m.pubkey} ${updateType}`, m);
|
||||||
if (updateType !== "no_change") {
|
if (updateType !== "no_change") {
|
||||||
if (updateType !== "refresh_profile") {
|
|
||||||
// fetch zapper key
|
|
||||||
const lnurl = m.lud16 || m.lud06;
|
|
||||||
if (lnurl) {
|
|
||||||
try {
|
|
||||||
const svc = new LNURL(lnurl);
|
|
||||||
await svc.load();
|
|
||||||
m.zapService = svc.zapperPubkey;
|
|
||||||
} catch {
|
|
||||||
console.warn("Failed to load LNURL for zapper pubkey", lnurl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const writeProfile = {
|
const writeProfile = {
|
||||||
...existing,
|
...existing,
|
||||||
...m,
|
...m,
|
||||||
};
|
};
|
||||||
this.cache.set(m.pubkey, writeProfile);
|
await this.#setItem(writeProfile);
|
||||||
if (db.ready) {
|
if (updateType !== "refresh_profile") {
|
||||||
await db.users.put(writeProfile);
|
const lnurl = m.lud16 ?? m.lud06;
|
||||||
this.onTable.add(m.pubkey);
|
if (lnurl) {
|
||||||
|
this.#zapperQueue.push({
|
||||||
|
pubkey: m.pubkey,
|
||||||
|
lnurl,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.notifyChange([m.pubkey]);
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
return updateType;
|
||||||
}
|
}
|
||||||
|
|
||||||
takeSnapshot(): MetadataCache[] {
|
takeSnapshot(): MetadataCache[] {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async #setItem(m: MetadataCache) {
|
||||||
|
this.cache.set(m.pubkey, m);
|
||||||
|
if (db.ready) {
|
||||||
|
await db.users.put(m);
|
||||||
|
this.onTable.add(m.pubkey);
|
||||||
|
}
|
||||||
|
this.notifyChange([m.pubkey]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async #processZapperQueue() {
|
||||||
|
while (this.#zapperQueue.length > 0) {
|
||||||
|
const i = this.#zapperQueue.shift();
|
||||||
|
if (i) {
|
||||||
|
try {
|
||||||
|
const svc = new LNURL(i.lnurl);
|
||||||
|
await svc.load();
|
||||||
|
const p = this.getFromCache(i.pubkey);
|
||||||
|
if (p) {
|
||||||
|
this.#setItem({
|
||||||
|
...p,
|
||||||
|
zapService: svc.zapperPubkey,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
console.warn("Failed to load LNURL for zapper pubkey", i.lnurl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(() => this.#processZapperQueue(), 1_000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UserCache = new UserProfileCache();
|
export const UserCache = new UserProfileCache();
|
||||||
|
@ -65,7 +65,7 @@ class ProfileLoaderService {
|
|||||||
|
|
||||||
const q = System.Query<PubkeyReplaceableNoteStore>(PubkeyReplaceableNoteStore, sub);
|
const q = System.Query<PubkeyReplaceableNoteStore>(PubkeyReplaceableNoteStore, sub);
|
||||||
// never release this callback, it will stop firing anyway after eose
|
// never release this callback, it will stop firing anyway after eose
|
||||||
const releaseOnEvent = q.onEvent(this.onProfileEvent);
|
const releaseOnEvent = q.onEvent(e => this.onProfileEvent(e));
|
||||||
const results = await new Promise<Readonly<Array<TaggedRawEvent>>>(resolve => {
|
const results = await new Promise<Readonly<Array<TaggedRawEvent>>>(resolve => {
|
||||||
let timeout: ReturnType<typeof setTimeout> | undefined = undefined;
|
let timeout: ReturnType<typeof setTimeout> | undefined = undefined;
|
||||||
const release = q.hook(() => {
|
const release = q.hook(() => {
|
||||||
|
Loading…
Reference in New Issue
Block a user