chore: fix fetching of user profile

This commit is contained in:
daniyal 2024-12-28 00:43:00 +05:00
parent 4f9fdd19b0
commit 006ed7b548
3 changed files with 11 additions and 18 deletions

View File

@ -41,8 +41,7 @@ export interface NDKContextType {
) => Promise<NDKEvent | null> ) => Promise<NDKEvent | null>
findMetadata: ( findMetadata: (
pubkey: string, pubkey: string,
opts?: NDKSubscriptionOptions, opts?: NDKSubscriptionOptions
storeProfileEvent?: boolean
) => Promise<NDKUserProfile | null> ) => Promise<NDKUserProfile | null>
getNDKRelayList: (pubkey: Hexpubkey) => Promise<NDKRelayList> getNDKRelayList: (pubkey: Hexpubkey) => Promise<NDKRelayList>
publish: (event: NDKEvent, explicitRelayUrls?: string[]) => Promise<string[]> publish: (event: NDKEvent, explicitRelayUrls?: string[]) => Promise<string[]>
@ -205,21 +204,17 @@ export const NDKContextProvider = ({ children }: { children: ReactNode }) => {
*/ */
const findMetadata = async ( const findMetadata = async (
pubkey: string, pubkey: string,
opts?: NDKSubscriptionOptions, opts?: NDKSubscriptionOptions
storeProfileEvent?: boolean
): Promise<NDKUserProfile | null> => { ): Promise<NDKUserProfile | null> => {
const npub = hexToNpub(pubkey) const npub = hexToNpub(pubkey)
const user = new NDKUser({ npub }) const user = new NDKUser({ npub })
user.ndk = ndk user.ndk = ndk
return await user.fetchProfile( return await user.fetchProfile({
{ cacheUsage: NDKSubscriptionCacheUsage.PARALLEL,
cacheUsage: NDKSubscriptionCacheUsage.PARALLEL, ...(opts || {})
...(opts || {}) })
},
storeProfileEvent
)
} }
const getNDKRelayList = async (pubkey: Hexpubkey) => { const getNDKRelayList = async (pubkey: Hexpubkey) => {
@ -250,8 +245,7 @@ export const NDKContextProvider = ({ children }: { children: ReactNode }) => {
ndkRelaySet = NDKRelaySet.fromRelayUrls(explicitRelayUrls, ndk) ndkRelaySet = NDKRelaySet.fromRelayUrls(explicitRelayUrls, ndk)
} }
return event return await Promise.race([event.publish(ndkRelaySet), timeout(3000)])
.publish(ndkRelaySet, 10000)
.then((res) => { .then((res) => {
const relaysPublishedOn = Array.from(res) const relaysPublishedOn = Array.from(res)
return relaysPublishedOn.map((relay) => relay.url) return relaysPublishedOn.map((relay) => relay.url)

View File

@ -25,9 +25,8 @@ export const useAuth = () => {
const { getRelayInfo } = useDvm() const { getRelayInfo } = useDvm()
const { findMetadata, getNDKRelayList } = useNDKContext() const { findMetadata, getNDKRelayList } = useNDKContext()
const { auth: authState, relays: relaysState } = useAppSelector( const authState = useAppSelector((state) => state.auth)
(state) => state const relaysState = useAppSelector((state) => state.relays)
)
const checkSession = useCallback(() => { const checkSession = useCallback(() => {
const savedAuthToken = getAuthToken() const savedAuthToken = getAuthToken()
@ -66,7 +65,7 @@ export const useAuth = () => {
const emptyMetadata = getEmptyMetadataEvent() const emptyMetadata = getEmptyMetadataEvent()
try { try {
const profile = await findMetadata(pubkey, {}, true) const profile = await findMetadata(pubkey)
if (profile && profile.profileEvent) { if (profile && profile.profileEvent) {
const event: Event = JSON.parse(profile.profileEvent) const event: Event = JSON.parse(profile.profileEvent)

View File

@ -164,7 +164,7 @@ export const MainLayout = () => {
if (loggedIn) { if (loggedIn) {
if (!loginMethod || !usersPubkey) return logout() if (!loginMethod || !usersPubkey) return logout()
findMetadata(usersPubkey, {}, true).then((profile) => { findMetadata(usersPubkey).then((profile) => {
if (profile && profile.profileEvent) { if (profile && profile.profileEvent) {
try { try {
const event: Event = JSON.parse(profile.profileEvent) const event: Event = JSON.parse(profile.profileEvent)