Support multiple relays for fetching content

This commit is contained in:
2024-10-22 19:41:19 +02:00
parent 856b10358c
commit 453a0f14d3
3 changed files with 15 additions and 10 deletions

View File

@@ -1,4 +1,4 @@
import { NRelay1 } from "@nostrify/nostrify";
import { NPool, NRelay1 } from "@nostrify/nostrify";
import config from "./config.ts";
export interface NEvent {
@@ -11,13 +11,18 @@ export interface NEvent {
tags: Array<[string, string, string?]>;
}
export const relay = new NRelay1(config.home_relay_url);
const relayPool = new NPool({
open: (url) => new NRelay1(url),
reqRouter: async (filters) => new Map(
config.relay_urls.map(url => [ url, filters ])
)
});
export async function fetchReplaceableEvent(
pubkey: string,
identifier: string,
) {
let events = await relay.query([{
let events = await relayPool.query([{
authors: [pubkey],
kinds: [30023],
"#d": [identifier],
@@ -27,7 +32,7 @@ export async function fetchReplaceableEvent(
if (events.length > 0) {
return events[0];
} else {
events = await relay.query([{
events = await relayPool.query([{
authors: [pubkey],
kinds: [30024],
"#d": [identifier],
@@ -39,7 +44,7 @@ export async function fetchReplaceableEvent(
}
export async function fetchArticlesByAuthor(pubkey: string) {
const events = await relay.query([{
const events = await relayPool.query([{
authors: [pubkey],
kinds: [30023],
limit: 10,
@@ -49,7 +54,7 @@ export async function fetchArticlesByAuthor(pubkey: string) {
}
export async function fetchProfileEvent(pubkey: string) {
const events = await relay.query([{
const events = await relayPool.query([{
authors: [pubkey],
kinds: [0],
limit: 1,