batch process nip5/zapper queue
This commit is contained in:
parent
08e3619418
commit
1d45225336
@ -107,50 +107,68 @@ class UserProfileCache extends FeedCache<MetadataCache> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async #processZapperQueue() {
|
async #processZapperQueue() {
|
||||||
while (this.#zapperQueue.length > 0) {
|
await this.#batchQueue(
|
||||||
const i = this.#zapperQueue.shift();
|
this.#zapperQueue,
|
||||||
if (i) {
|
async i => {
|
||||||
try {
|
const svc = new LNURL(i.lnurl);
|
||||||
const svc = new LNURL(i.lnurl);
|
await svc.load();
|
||||||
await svc.load();
|
const p = this.getFromCache(i.pubkey);
|
||||||
const p = this.getFromCache(i.pubkey);
|
if (p) {
|
||||||
if (p) {
|
this.#setItem({
|
||||||
this.#setItem({
|
...p,
|
||||||
...p,
|
zapService: svc.zapperPubkey,
|
||||||
zapService: svc.zapperPubkey,
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
console.warn("Failed to load LNURL for zapper pubkey", i.lnurl);
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
5
|
||||||
|
);
|
||||||
|
|
||||||
setTimeout(() => this.#processZapperQueue(), 1_000);
|
setTimeout(() => this.#processZapperQueue(), 1_000);
|
||||||
}
|
}
|
||||||
|
|
||||||
async #processNip5Queue() {
|
async #processNip5Queue() {
|
||||||
while (this.#nip5Queue.length > 0) {
|
await this.#batchQueue(
|
||||||
const i = this.#nip5Queue.shift();
|
this.#nip5Queue,
|
||||||
if (i) {
|
async i => {
|
||||||
try {
|
const [name, domain] = i.nip05.split("@");
|
||||||
const [name, domain] = i.nip05.split("@");
|
const nip5pk = await fetchNip05Pubkey(name, domain);
|
||||||
const nip5pk = await fetchNip05Pubkey(name, domain);
|
const p = this.getFromCache(i.pubkey);
|
||||||
const p = this.getFromCache(i.pubkey);
|
if (p) {
|
||||||
if (p) {
|
this.#setItem({
|
||||||
this.#setItem({
|
...p,
|
||||||
...p,
|
isNostrAddressValid: i.pubkey === nip5pk,
|
||||||
isNostrAddressValid: i.pubkey === nip5pk,
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
console.warn("Failed to load nip-05", i.nip05);
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
5
|
||||||
|
);
|
||||||
|
|
||||||
setTimeout(() => this.#processNip5Queue(), 1_000);
|
setTimeout(() => this.#processNip5Queue(), 1_000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async #batchQueue<T>(queue: Array<T>, proc: (v: T) => Promise<void>, batchSize = 3) {
|
||||||
|
const batch = [];
|
||||||
|
while (queue.length > 0) {
|
||||||
|
const i = queue.shift();
|
||||||
|
if (i) {
|
||||||
|
batch.push(
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
await proc(i);
|
||||||
|
} catch {
|
||||||
|
console.warn("Failed to process item", i);
|
||||||
|
}
|
||||||
|
batch.pop(); // pop any
|
||||||
|
})()
|
||||||
|
);
|
||||||
|
if (batch.length === batchSize) {
|
||||||
|
await Promise.all(batch);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await Promise.all(batch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UserCache = new UserProfileCache();
|
export const UserCache = new UserProfileCache();
|
||||||
|
@ -6,12 +6,14 @@ interface NostrJson {
|
|||||||
names: Record<string, string>;
|
names: Record<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchNip05Pubkey(name: string, domain: string) {
|
export async function fetchNip05Pubkey(name: string, domain: string, timeout = 2_000) {
|
||||||
if (!name || !domain) {
|
if (!name || !domain) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const res = await fetch(`https://${domain}/.well-known/nostr.json?name=${encodeURIComponent(name)}`);
|
const res = await fetch(`https://${domain}/.well-known/nostr.json?name=${encodeURIComponent(name)}`, {
|
||||||
|
signal: AbortSignal.timeout(timeout),
|
||||||
|
});
|
||||||
const data: NostrJson = await res.json();
|
const data: NostrJson = await res.json();
|
||||||
const match = Object.keys(data.names).find(n => {
|
const match = Object.keys(data.names).find(n => {
|
||||||
return n.toLowerCase() === name.toLowerCase();
|
return n.toLowerCase() === name.toLowerCase();
|
||||||
|
Loading…
Reference in New Issue
Block a user