From 0c291d6375e2a55ea29f04f0ed909bca50e5f29d Mon Sep 17 00:00:00 2001 From: Jonathan Staab Date: Mon, 24 Apr 2023 16:11:01 -0500 Subject: [PATCH] Wait for relays that require AUTH --- src/agent/pool.ts | 30 +++++++++++++++++++++++++----- src/app/views/ChatListItem.svelte | 4 ++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/agent/pool.ts b/src/agent/pool.ts index 9ec61475..e9424d18 100644 --- a/src/agent/pool.ts +++ b/src/agent/pool.ts @@ -5,8 +5,9 @@ import {verifySignature} from "nostr-tools" import {pluck, identity} from "ramda" import {ensurePlural, switcher} from "hurdak/lib/hurdak" import {warn, log, error} from "src/util/logger" -import {union, difference} from "src/util/misc" +import {union, sleep, difference} from "src/util/misc" import {normalizeRelayUrl} from "src/util/nostr" +import {relays} from "src/agent/db" const Config = { multiplextrUrl: null, @@ -174,7 +175,7 @@ function getQuality(url) { return [0.5, "Not Connected"] } -function getExecutor(urls) { +async function getExecutor(urls, {bypassBoot = false} = {}) { if (forceUrls.length > 0) { urls = forceUrls } @@ -210,12 +211,31 @@ function getExecutor(urls) { }, }) + // Eagerly connect and handle AUTH + await Promise.all( + ensurePlural(executor.target.sockets).map(async socket => { + const relay = relays.get(socket.url) + const waitForBoot = relay?.limitation?.payment_required || relay?.limitation?.auth_required + + if (socket.status === Socket.STATUS.NEW) { + socket.booted = sleep(2000) + + await socket.connect() + } + + // Delay REQ/EVENT until AUTH flow happens + if (!bypassBoot && waitForBoot) { + await socket.booted + } + }) + ) + return executor } async function publish({relays, event, onProgress, timeout = 3000, verb = "EVENT"}) { const urls = getUrls(relays) - const executor = getExecutor(urls) + const executor = await getExecutor(urls, {bypassBoot: verb === "AUTH"}) Meta.onPublish(urls) @@ -285,7 +305,7 @@ type SubscribeOpts = { async function subscribe({relays, filter, onEvent, onEose}: SubscribeOpts) { const urls = getUrls(relays) - const executor = getExecutor(urls) + const executor = await getExecutor(urls) const filters = ensurePlural(filter) const now = Date.now() const seen = new Map() @@ -358,7 +378,7 @@ async function subscribe({relays, filter, onEvent, onEose}: SubscribeOpts) { async function count(filter) { const filters = ensurePlural(filter) - const executor = getExecutor(["wss://rbr.bio"]) + const executor = await getExecutor(["wss://rbr.bio"]) return new Promise(resolve => { const sub = executor.count(filters, { diff --git a/src/app/views/ChatListItem.svelte b/src/app/views/ChatListItem.svelte index aa690159..fdcdb668 100644 --- a/src/app/views/ChatListItem.svelte +++ b/src/app/views/ChatListItem.svelte @@ -30,12 +30,12 @@

{room.name || ""}

{#if joined} - + Leave {:else} - + Join