diff --git a/src/lib/components/publications/PublicationFeed.svelte b/src/lib/components/publications/PublicationFeed.svelte index 1e6939c..7dc8b82 100644 --- a/src/lib/components/publications/PublicationFeed.svelte +++ b/src/lib/components/publications/PublicationFeed.svelte @@ -86,6 +86,17 @@ if (newRelays.length === 0) { console.debug('[PublicationFeed] No relays available, waiting...'); + // Set up a retry mechanism when relays become available + const unsubscribe = activeInboxRelays.subscribe((relays) => { + if (relays.length > 0 && !hasInitialized) { + console.debug('[PublicationFeed] Relays now available, retrying initialization'); + unsubscribe(); + setTimeout(() => { + hasInitialized = true; + initializeAndFetch(); + }, 1000); + } + }); return; } diff --git a/src/lib/components/util/Profile.svelte b/src/lib/components/util/Profile.svelte index cc5ff4a..d39c286 100644 --- a/src/lib/components/util/Profile.svelte +++ b/src/lib/components/util/Profile.svelte @@ -21,7 +21,7 @@ import NDK, { NDKNip46Signer, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk"; import { onMount } from "svelte"; import { getUserMetadata } from "$lib/utils/nostrUtils"; - import { activeInboxRelays } from "$lib/ndk"; + import { activeInboxRelays, activeOutboxRelays } from "$lib/ndk"; let { pubkey, isNav = false } = $props<{ pubkey?: string, isNav?: boolean }>(); @@ -187,6 +187,23 @@ try { console.log("Refreshing profile for npub:", userState.npub); + // Check if we have relays available + const inboxRelays = get(activeInboxRelays); + const outboxRelays = get(activeOutboxRelays); + + if (inboxRelays.length === 0 && outboxRelays.length === 0) { + console.log("Profile: No relays available, will retry when relays become available"); + // Set up a retry mechanism when relays become available + const unsubscribe = activeInboxRelays.subscribe((relays) => { + if (relays.length > 0 && !isRefreshingProfile) { + console.log("Profile: Relays now available, retrying profile fetch"); + unsubscribe(); + setTimeout(() => refreshProfile(), 1000); + } + }); + return; + } + // Try using NDK's built-in profile fetching first const ndk = get(ndkInstance); if (ndk && userState.ndkUser) { diff --git a/src/lib/stores/userStore.ts b/src/lib/stores/userStore.ts index df73ab7..1e58f42 100644 --- a/src/lib/stores/userStore.ts +++ b/src/lib/stores/userStore.ts @@ -288,14 +288,20 @@ export async function loginWithAmber(amberSigner: NDKSigner, user: NDKUser) { */ export async function loginWithNpub(pubkeyOrNpub: string) { const ndk = get(ndkInstance); - if (!ndk) throw new Error("NDK not initialized"); - // Only clear previous login state after successful login + if (!ndk) { + throw new Error("NDK not initialized"); + } + let hexPubkey: string; - if (pubkeyOrNpub.startsWith("npub")) { + if (pubkeyOrNpub.startsWith("npub1")) { try { - hexPubkey = nip19.decode(pubkeyOrNpub).data as string; + const decoded = nip19.decode(pubkeyOrNpub); + if (decoded.type !== "npub") { + throw new Error("Invalid npub format"); + } + hexPubkey = decoded.data; } catch (e) { - console.error("Failed to decode hex pubkey from npub:", pubkeyOrNpub, e); + console.error("Failed to decode npub:", pubkeyOrNpub, e); throw e; } } else { @@ -313,6 +319,18 @@ export async function loginWithNpub(pubkeyOrNpub: string) { const user = ndk.getUser({ npub }); let profile: NostrProfile | null = null; + + // First, update relay stores to ensure we have relays available + try { + console.debug('[userStore.ts] loginWithNpub: Updating relay stores for authenticated user'); + await updateActiveRelayStores(ndk); + } catch (error) { + console.warn('[userStore.ts] loginWithNpub: Failed to update relay stores:', error); + } + + // Wait a moment for relay stores to be properly initialized + await new Promise(resolve => setTimeout(resolve, 500)); + try { profile = await getUserMetadata(npub, true); // Force fresh fetch console.log("Login with npub - fetched profile:", profile); @@ -344,14 +362,6 @@ export async function loginWithNpub(pubkeyOrNpub: string) { userStore.set(userState); userPubkey.set(user.pubkey); - // Update relay stores with the new user's relays - try { - console.debug('[userStore.ts] loginWithNpub: Updating relay stores for authenticated user'); - await updateActiveRelayStores(ndk); - } catch (error) { - console.warn('[userStore.ts] loginWithNpub: Failed to update relay stores:', error); - } - clearLogin(); localStorage.removeItem("alexandria/logout/flag"); persistLogin(user, "npub"); diff --git a/src/lib/utils/nostrUtils.ts b/src/lib/utils/nostrUtils.ts index c36108f..d3be24d 100644 --- a/src/lib/utils/nostrUtils.ts +++ b/src/lib/utils/nostrUtils.ts @@ -5,7 +5,7 @@ import { npubCache } from "./npubCache.ts"; import NDK, { NDKEvent, NDKRelaySet, NDKUser } from "@nostr-dev-kit/ndk"; import type { NDKKind, NostrEvent } from "@nostr-dev-kit/ndk"; import type { Filter } from "./search_types.ts"; -import { communityRelays, secondaryRelays } from "../consts.ts"; +import { communityRelays, secondaryRelays, searchRelays } from "../consts.ts"; import { activeInboxRelays, activeOutboxRelays } from "../ndk.ts"; import { NDKRelaySet as NDKRelaySetFromNDK } from "@nostr-dev-kit/ndk"; import { sha256 } from "@noble/hashes/sha2.js"; @@ -446,15 +446,17 @@ export async function fetchEventWithFallback( // Use both inbox and outbox relays for better event discovery const inboxRelays = get(activeInboxRelays); const outboxRelays = get(activeOutboxRelays); - const allRelays = [...inboxRelays, ...outboxRelays]; + let allRelays = [...inboxRelays, ...outboxRelays]; console.log("fetchEventWithFallback: Using inbox relays:", inboxRelays); console.log("fetchEventWithFallback: Using outbox relays:", outboxRelays); // Check if we have any relays available if (allRelays.length === 0) { - console.warn("fetchEventWithFallback: No relays available for event fetch"); - return null; + console.warn("fetchEventWithFallback: No relays available for event fetch, using fallback relays"); + // Use fallback relays when no relays are available + allRelays = [...secondaryRelays, ...searchRelays]; + console.log("fetchEventWithFallback: Using fallback relays:", allRelays); } // Create relay set from all available relays