This commit is contained in:
reya 2023-12-04 13:36:16 +07:00
parent 4093821fd0
commit 8795923443
2 changed files with 33 additions and 24 deletions

View File

@ -64,14 +64,17 @@ export const NDKInstance = () => {
const bunker = !!parseInt(bunkerSetting);
const outbox = !!parseInt(outboxSetting);
// #TODO: user can config outbox relays
const outboxRelayUrls = normalizeRelayUrlSet(['wss://purplepag.es/']);
const tauriAdapter = new NDKCacheAdapterTauri(db);
const instance = new NDK({
explicitRelayUrls,
cacheAdapter: tauriAdapter,
outboxRelayUrls: ['wss://purplepag.es'],
outboxRelayUrls,
enableOutboxModel: outbox,
autoConnectUserRelays: true,
autoFetchUserMutelist: true,
cacheAdapter: tauriAdapter,
// clientName: 'Lume',
// clientNip89: '',
});

View File

@ -1,11 +1,10 @@
import { NDKUserProfile } from '@nostr-dev-kit/ndk';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useQuery } from '@tanstack/react-query';
import { nip19 } from 'nostr-tools';
import { useNDK } from '@libs/ndk/provider';
export function useProfile(pubkey: string, embed?: string) {
const queryClient = useQueryClient();
const { ndk } = useNDK();
const {
isFetching,
@ -14,31 +13,38 @@ export function useProfile(pubkey: string, embed?: string) {
} = useQuery({
queryKey: ['user', pubkey],
queryFn: async () => {
// parse data from nostr.band api
if (embed) {
const profile: NDKUserProfile = JSON.parse(embed);
try {
// parse data from nostr.band api
if (embed) {
const profile: NDKUserProfile = JSON.parse(embed);
return profile;
}
// get clean pubkey without any special characters
let hexstring = pubkey.replace(/[^a-zA-Z0-9]/g, '');
if (hexstring.startsWith('npub1') || hexstring.startsWith('nprofile1')) {
const decoded = nip19.decode(hexstring);
if (decoded.type === 'nprofile') hexstring = decoded.data.pubkey;
if (decoded.type === 'npub') hexstring = decoded.data;
}
const user = ndk.getUser({ pubkey: hexstring });
const profile = await user.fetchProfile();
if (!profile)
throw new Error(
`Cannot get metadata for ${pubkey}, will be retry after 10 seconds`
);
return profile;
}
// get clean pubkey without any special characters
let hexstring = pubkey.replace(/[^a-zA-Z0-9]/g, '');
if (hexstring.startsWith('npub1') || hexstring.startsWith('nprofile1')) {
const decoded = nip19.decode(hexstring);
if (decoded.type === 'nprofile') hexstring = decoded.data.pubkey;
if (decoded.type === 'npub') hexstring = decoded.data;
}
const user = ndk.getUser({ pubkey: hexstring });
const profile = await user.fetchProfile();
if (!profile)
} catch (e) {
console.error(e);
throw new Error(
`Cannot get metadata for ${pubkey}, will be retry after 10 seconds`
);
return profile;
}
},
initialData: () => queryClient.getQueryData(['user', pubkey]) as NDKUserProfile,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: 2,