mirror of
https://github.com/hzrd149/nsite-gateway.git
synced 2025-06-23 12:05:01 +00:00
Use users relays when searching for blossom servers
This commit is contained in:
parent
b53e9c4dc6
commit
48ae3882a3
5
.changeset/smart-years-end.md
Normal file
5
.changeset/smart-years-end.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"nsite-ts": patch
|
||||
---
|
||||
|
||||
Use users relays when searching for blossom servers
|
@ -1,13 +1,18 @@
|
||||
import { getServersFromServerListEvent, USER_BLOSSOM_SERVER_LIST_KIND } from "blossom-client-sdk";
|
||||
import { NDKRelaySet } from "@nostr-dev-kit/ndk";
|
||||
|
||||
import ndk from "./ndk.js";
|
||||
import { BLOSSOM_SERVERS, MAX_FILE_SIZE } from "./env.js";
|
||||
import { makeRequestWithAbort } from "./helpers/http.js";
|
||||
|
||||
export async function getUserBlossomServers(pubkey: string) {
|
||||
const blossomServersEvent = await ndk.fetchEvent([{ kinds: [USER_BLOSSOM_SERVER_LIST_KIND], authors: [pubkey] }]);
|
||||
export async function getUserBlossomServers(pubkey: string, relays?: string[]) {
|
||||
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
|
||||
|
13
src/index.ts
13
src/index.ts
@ -70,9 +70,16 @@ app.use(async (ctx, next) => {
|
||||
let servers = await userServers.get<string[] | undefined>(pubkey);
|
||||
if (!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);
|
||||
|
||||
@ -93,7 +100,7 @@ app.use(async (ctx, next) => {
|
||||
}
|
||||
|
||||
ctx.status = 500;
|
||||
ctx.body = "Failed to download blob";
|
||||
ctx.body = "Failed to find blob";
|
||||
} else await next();
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user