From 9aa57c1adf2f09be3780ac2469e2948d5be2ea56 Mon Sep 17 00:00:00 2001 From: daniyal Date: Thu, 24 Oct 2024 20:29:57 +0500 Subject: [PATCH 1/3] fix: no need to pass relay set to subscribe function, just include p tag with authors pubkey --- src/hooks/useComments.ts | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/src/hooks/useComments.ts b/src/hooks/useComments.ts index 6be5932..355a01a 100644 --- a/src/hooks/useComments.ts +++ b/src/hooks/useComments.ts @@ -1,14 +1,11 @@ import { - getRelayListForUser, NDKFilter, NDKKind, - NDKRelaySet, NDKSubscription, NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk' import { useEffect, useState } from 'react' -import { CommentEvent, ModDetails, UserRelaysType } from 'types' -import { log, LogType } from 'utils' +import { CommentEvent, ModDetails } from 'types' import { useNDKContext } from './useNDKContext' export const useComments = (mod: ModDetails) => { @@ -19,35 +16,18 @@ export const useComments = (mod: ModDetails) => { let subscription: NDKSubscription // Define the subscription variable here for cleanup const setupSubscription = async () => { - // Find the mod author's relays. - const authorReadRelays = await getRelayListForUser(mod.author, ndk) - .then((ndkRelayList) => { - if (ndkRelayList) return ndkRelayList[UserRelaysType.Read] - return [] // Return an empty array if ndkRelayList is undefined - }) - .catch((err) => { - log( - true, - LogType.Error, - `An error occurred in fetching user's (${mod.author}) ${UserRelaysType.Read}`, - err - ) - return [] as string[] - }) - const filter: NDKFilter = { kinds: [NDKKind.Text], - '#a': [mod.aTag] + '#a': [mod.aTag], + '#p': [mod.author] } - subscription = ndk.subscribe( - filter, - { - closeOnEose: false, - cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST - }, - NDKRelaySet.fromRelayUrls(authorReadRelays, ndk, true) - ) + subscription = ndk.subscribe(filter, { + closeOnEose: false, + cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST + }) + + console.log('ndk.pool.urls() :>> ', ndk.pool.urls()) subscription.on('event', (ndkEvent) => { setCommentEvents((prev) => { From d96e5088b8fb2226e9f7d304704784abefdce15f Mon Sep 17 00:00:00 2001 From: daniyal Date: Thu, 24 Oct 2024 21:07:18 +0500 Subject: [PATCH 2/3] fix: add timeout in getting user's relay and also pass ndk pool's relays in relayset --- src/hooks/useComments.ts | 48 ++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/hooks/useComments.ts b/src/hooks/useComments.ts index 355a01a..f11dbce 100644 --- a/src/hooks/useComments.ts +++ b/src/hooks/useComments.ts @@ -1,11 +1,14 @@ import { + getRelayListForUser, NDKFilter, NDKKind, + NDKRelaySet, NDKSubscription, NDKSubscriptionCacheUsage } from '@nostr-dev-kit/ndk' import { useEffect, useState } from 'react' -import { CommentEvent, ModDetails } from 'types' +import { CommentEvent, ModDetails, UserRelaysType } from 'types' +import { log, LogType, timeout } from 'utils' import { useNDKContext } from './useNDKContext' export const useComments = (mod: ModDetails) => { @@ -16,20 +19,51 @@ export const useComments = (mod: ModDetails) => { let subscription: NDKSubscription // Define the subscription variable here for cleanup const setupSubscription = async () => { + // Find the mod author's relays. + + const authorReadRelays = await Promise.race([ + getRelayListForUser(mod.author, ndk), + timeout(10 * 1000) // add a 10 sec timeout + ]) + .then((ndkRelayList) => { + if (ndkRelayList) return ndkRelayList[UserRelaysType.Read] + return [] // Return an empty array if ndkRelayList is undefined + }) + .catch((err) => { + log( + true, + LogType.Error, + `An error occurred in fetching user's (${mod.author}) ${UserRelaysType.Read}`, + err + ) + return [] as string[] + }) + const filter: NDKFilter = { kinds: [NDKKind.Text], - '#a': [mod.aTag], - '#p': [mod.author] + '#a': [mod.aTag] } - subscription = ndk.subscribe(filter, { - closeOnEose: false, - cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST + const relayUrls = new Set() + + ndk.pool.urls().forEach((relayUrl) => { + relayUrls.add(relayUrl) }) - console.log('ndk.pool.urls() :>> ', ndk.pool.urls()) + authorReadRelays.forEach((relayUrl) => relayUrls.add(relayUrl)) + + subscription = ndk.subscribe( + filter, + { + closeOnEose: false, + cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST + }, + NDKRelaySet.fromRelayUrls(Array.from(relayUrls), ndk) + ) subscription.on('event', (ndkEvent) => { + console.log('ndkEvent :>> ', ndkEvent) + setCommentEvents((prev) => { if (prev.find((e) => e.id === ndkEvent.id)) { return [...prev] From d3c2d5fe7a1d5788e9eb84a62b16ade698096461 Mon Sep 17 00:00:00 2001 From: daniyal Date: Thu, 24 Oct 2024 21:08:40 +0500 Subject: [PATCH 3/3] chore: quick fix --- src/hooks/useComments.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/hooks/useComments.ts b/src/hooks/useComments.ts index f11dbce..fb57eb8 100644 --- a/src/hooks/useComments.ts +++ b/src/hooks/useComments.ts @@ -62,8 +62,6 @@ export const useComments = (mod: ModDetails) => { ) subscription.on('event', (ndkEvent) => { - console.log('ndkEvent :>> ', ndkEvent) - setCommentEvents((prev) => { if (prev.find((e) => e.id === ndkEvent.id)) { return [...prev]