Browse Source

Fixed relay list and profile being empty on page refresh.

master
silberengel 7 months ago
parent
commit
a93e528949
  1. 11
      src/lib/components/publications/PublicationFeed.svelte
  2. 19
      src/lib/components/util/Profile.svelte
  3. 36
      src/lib/stores/userStore.ts
  4. 10
      src/lib/utils/nostrUtils.ts

11
src/lib/components/publications/PublicationFeed.svelte

@ -86,6 +86,17 @@ @@ -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;
}

19
src/lib/components/util/Profile.svelte

@ -21,7 +21,7 @@ @@ -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 @@ @@ -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) {

36
src/lib/stores/userStore.ts

@ -288,14 +288,20 @@ export async function loginWithAmber(amberSigner: NDKSigner, user: NDKUser) { @@ -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) { @@ -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) { @@ -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");

10
src/lib/utils/nostrUtils.ts

@ -5,7 +5,7 @@ import { npubCache } from "./npubCache.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( @@ -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

Loading…
Cancel
Save