From 01bb68d87b41b5120e61ba85e9db73b71f236179 Mon Sep 17 00:00:00 2001 From: daniyal Date: Sat, 28 Dec 2024 00:44:21 +0500 Subject: [PATCH] chore: fix issue in publishing sigit --- src/hooks/useNDK.ts | 47 +++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/hooks/useNDK.ts b/src/hooks/useNDK.ts index fbd8d23..42fa053 100644 --- a/src/hooks/useNDK.ts +++ b/src/hooks/useNDK.ts @@ -2,7 +2,7 @@ import { useCallback } from 'react' import { toast } from 'react-toastify' import { bytesToHex } from '@noble/hashes/utils' -import { NDKEvent, NDKFilter, NDKKind } from '@nostr-dev-kit/ndk' +import { NDKEvent, NDKFilter, NDKKind, NDKRelaySet } from '@nostr-dev-kit/ndk' import _ from 'lodash' import { Event, @@ -35,14 +35,20 @@ import { getUserAppDataFromBlossom, hexToNpub, parseJson, + SIGIT_RELAY, unixNow, uploadUserAppDataToBlossom } from '../utils' export const useNDK = () => { const dispatch = useAppDispatch() - const { ndk, fetchEvent, fetchEventsFromUserRelays, publish } = - useNDKContext() + const { + ndk, + fetchEvent, + fetchEventsFromUserRelays, + publish, + getNDKRelayList + } = useNDKContext() const usersPubkey = useAppSelector((state) => state.auth.usersPubkey) const appData = useAppSelector((state) => state.userAppData) const processedEvents = useAppSelector( @@ -448,16 +454,33 @@ export const useNDK = () => { // Publish the notification event to the recipient's read relays const ndkEvent = new NDKEvent(ndk, wrappedEvent) - await publish(ndkEvent).catch((err) => { - // Log an error if publishing the notification event fails - console.log( - `An error occurred while publishing notification event for ${hexToNpub(receiver)}`, - err - ) - throw err - }) + + const ndkRelayList = await getNDKRelayList(receiver) + + const readRelayUrls = [...ndkRelayList.readRelayUrls] + if (!readRelayUrls.includes(SIGIT_RELAY)) { + readRelayUrls.push(SIGIT_RELAY) + } + + await ndkEvent + .publish(NDKRelaySet.fromRelayUrls(readRelayUrls, ndk, true)) + .then((publishedOnRelays) => { + if (publishedOnRelays.size === 0) { + throw new Error('Could not publish to any relay') + } + + return publishedOnRelays + }) + .catch((err) => { + // Log an error if publishing the notification event fails + console.log( + `An error occurred while publishing notification event for ${hexToNpub(receiver)}`, + err + ) + throw err + }) }, - [ndk, publish, usersPubkey] + [ndk, usersPubkey, getNDKRelayList] ) return {