Use users relays when searching for blossom servers

This commit is contained in:
hzrd149 2024-09-26 08:51:32 -05:00
parent b53e9c4dc6
commit 48ae3882a3
3 changed files with 23 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
"nsite-ts": patch
---
Use users relays when searching for blossom servers

View File

@ -1,13 +1,18 @@
import { getServersFromServerListEvent, USER_BLOSSOM_SERVER_LIST_KIND } from "blossom-client-sdk"; import { getServersFromServerListEvent, USER_BLOSSOM_SERVER_LIST_KIND } from "blossom-client-sdk";
import { NDKRelaySet } from "@nostr-dev-kit/ndk";
import ndk from "./ndk.js"; import ndk from "./ndk.js";
import { BLOSSOM_SERVERS, MAX_FILE_SIZE } from "./env.js"; import { BLOSSOM_SERVERS, MAX_FILE_SIZE } from "./env.js";
import { makeRequestWithAbort } from "./helpers/http.js"; import { makeRequestWithAbort } from "./helpers/http.js";
export async function getUserBlossomServers(pubkey: string) { export async function getUserBlossomServers(pubkey: string, relays?: string[]) {
const blossomServersEvent = await ndk.fetchEvent([{ kinds: [USER_BLOSSOM_SERVER_LIST_KIND], authors: [pubkey] }]); const blossomServersEvent = await ndk.fetchEvent(
[{ kinds: [USER_BLOSSOM_SERVER_LIST_KIND], authors: [pubkey] }],
{},
relays ? NDKRelaySet.fromRelayUrls(relays, ndk, true) : undefined,
);
return blossomServersEvent && getServersFromServerListEvent(blossomServersEvent).map((u) => u.toString()); return blossomServersEvent ? getServersFromServerListEvent(blossomServersEvent).map((u) => u.toString()) : undefined;
} }
// TODO: download the file to /tmp and verify it // TODO: download the file to /tmp and verify it

View File

@ -70,9 +70,16 @@ app.use(async (ctx, next) => {
let servers = await userServers.get<string[] | undefined>(pubkey); let servers = await userServers.get<string[] | undefined>(pubkey);
if (!servers) { if (!servers) {
console.log(`${pubkey}: Searching for blossom servers`); console.log(`${pubkey}: Searching for blossom servers`);
servers = (await getUserBlossomServers(pubkey)) ?? []; servers = await getUserBlossomServers(pubkey, relays);
await userServers.set(pubkey, servers); if (servers) {
await userServers.set(pubkey, servers);
console.log(`${pubkey}: Found ${servers.length} servers`);
} else {
servers = [];
await userServers.set(pubkey, [], 30_000);
console.log(`${pubkey}: Failed to find servers`);
}
} }
servers.push(...BLOSSOM_SERVERS); servers.push(...BLOSSOM_SERVERS);
@ -93,7 +100,7 @@ app.use(async (ctx, next) => {
} }
ctx.status = 500; ctx.status = 500;
ctx.body = "Failed to download blob"; ctx.body = "Failed to find blob";
} else await next(); } else await next();
}); });