fix: add timeout in getting user's relay and also pass ndk pool's relays in relayset
This commit is contained in:
parent
9aa57c1adf
commit
d96e5088b8
@ -1,11 +1,14 @@
|
|||||||
import {
|
import {
|
||||||
|
getRelayListForUser,
|
||||||
NDKFilter,
|
NDKFilter,
|
||||||
NDKKind,
|
NDKKind,
|
||||||
|
NDKRelaySet,
|
||||||
NDKSubscription,
|
NDKSubscription,
|
||||||
NDKSubscriptionCacheUsage
|
NDKSubscriptionCacheUsage
|
||||||
} from '@nostr-dev-kit/ndk'
|
} from '@nostr-dev-kit/ndk'
|
||||||
import { useEffect, useState } from 'react'
|
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'
|
import { useNDKContext } from './useNDKContext'
|
||||||
|
|
||||||
export const useComments = (mod: ModDetails) => {
|
export const useComments = (mod: ModDetails) => {
|
||||||
@ -16,20 +19,51 @@ export const useComments = (mod: ModDetails) => {
|
|||||||
let subscription: NDKSubscription // Define the subscription variable here for cleanup
|
let subscription: NDKSubscription // Define the subscription variable here for cleanup
|
||||||
|
|
||||||
const setupSubscription = async () => {
|
const setupSubscription = async () => {
|
||||||
const filter: NDKFilter = {
|
// Find the mod author's relays.
|
||||||
kinds: [NDKKind.Text],
|
|
||||||
'#a': [mod.aTag],
|
|
||||||
'#p': [mod.author]
|
|
||||||
}
|
|
||||||
|
|
||||||
subscription = ndk.subscribe(filter, {
|
const authorReadRelays = await Promise.race([
|
||||||
closeOnEose: false,
|
getRelayListForUser(mod.author, ndk),
|
||||||
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST
|
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[]
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('ndk.pool.urls() :>> ', ndk.pool.urls())
|
const filter: NDKFilter = {
|
||||||
|
kinds: [NDKKind.Text],
|
||||||
|
'#a': [mod.aTag]
|
||||||
|
}
|
||||||
|
|
||||||
|
const relayUrls = new Set<string>()
|
||||||
|
|
||||||
|
ndk.pool.urls().forEach((relayUrl) => {
|
||||||
|
relayUrls.add(relayUrl)
|
||||||
|
})
|
||||||
|
|
||||||
|
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) => {
|
subscription.on('event', (ndkEvent) => {
|
||||||
|
console.log('ndkEvent :>> ', ndkEvent)
|
||||||
|
|
||||||
setCommentEvents((prev) => {
|
setCommentEvents((prev) => {
|
||||||
if (prev.find((e) => e.id === ndkEvent.id)) {
|
if (prev.find((e) => e.id === ndkEvent.id)) {
|
||||||
return [...prev]
|
return [...prev]
|
||||||
|
Loading…
Reference in New Issue
Block a user