fix: no need to pass relay set to subscribe function, just include p tag with authors pubkey

This commit is contained in:
daniyal 2024-10-24 20:29:57 +05:00
parent 8ee6f98654
commit 9aa57c1adf

View File

@ -1,14 +1,11 @@
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, UserRelaysType } from 'types' import { CommentEvent, ModDetails } from 'types'
import { log, LogType } from 'utils'
import { useNDKContext } from './useNDKContext' import { useNDKContext } from './useNDKContext'
export const useComments = (mod: ModDetails) => { export const useComments = (mod: ModDetails) => {
@ -19,35 +16,18 @@ 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 () => {
// 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 = { const filter: NDKFilter = {
kinds: [NDKKind.Text], kinds: [NDKKind.Text],
'#a': [mod.aTag] '#a': [mod.aTag],
'#p': [mod.author]
} }
subscription = ndk.subscribe( subscription = ndk.subscribe(filter, {
filter, closeOnEose: false,
{ cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST
closeOnEose: false, })
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST
}, console.log('ndk.pool.urls() :>> ', ndk.pool.urls())
NDKRelaySet.fromRelayUrls(authorReadRelays, ndk, true)
)
subscription.on('event', (ndkEvent) => { subscription.on('event', (ndkEvent) => {
setCommentEvents((prev) => { setCommentEvents((prev) => {