This commit is contained in:
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 bunker = !!parseInt(bunkerSetting);
const outbox = !!parseInt(outboxSetting); const outbox = !!parseInt(outboxSetting);
// #TODO: user can config outbox relays
const outboxRelayUrls = normalizeRelayUrlSet(['wss://purplepag.es/']);
const tauriAdapter = new NDKCacheAdapterTauri(db); const tauriAdapter = new NDKCacheAdapterTauri(db);
const instance = new NDK({ const instance = new NDK({
explicitRelayUrls, explicitRelayUrls,
cacheAdapter: tauriAdapter, outboxRelayUrls,
outboxRelayUrls: ['wss://purplepag.es'],
enableOutboxModel: outbox, enableOutboxModel: outbox,
autoConnectUserRelays: true, autoConnectUserRelays: true,
autoFetchUserMutelist: true, autoFetchUserMutelist: true,
cacheAdapter: tauriAdapter,
// clientName: 'Lume', // clientName: 'Lume',
// clientNip89: '', // clientNip89: '',
}); });

View File

@@ -1,11 +1,10 @@
import { NDKUserProfile } from '@nostr-dev-kit/ndk'; 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 { nip19 } from 'nostr-tools';
import { useNDK } from '@libs/ndk/provider'; import { useNDK } from '@libs/ndk/provider';
export function useProfile(pubkey: string, embed?: string) { export function useProfile(pubkey: string, embed?: string) {
const queryClient = useQueryClient();
const { ndk } = useNDK(); const { ndk } = useNDK();
const { const {
isFetching, isFetching,
@@ -14,31 +13,38 @@ export function useProfile(pubkey: string, embed?: string) {
} = useQuery({ } = useQuery({
queryKey: ['user', pubkey], queryKey: ['user', pubkey],
queryFn: async () => { queryFn: async () => {
// parse data from nostr.band api try {
if (embed) { // parse data from nostr.band api
const profile: NDKUserProfile = JSON.parse(embed); 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; return profile;
} } catch (e) {
console.error(e);
// 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( throw new Error(
`Cannot get metadata for ${pubkey}, will be retry after 10 seconds` `Cannot get metadata for ${pubkey}, will be retry after 10 seconds`
); );
return profile; }
}, },
initialData: () => queryClient.getQueryData(['user', pubkey]) as NDKUserProfile,
refetchOnWindowFocus: false, refetchOnWindowFocus: false,
refetchOnReconnect: false, refetchOnReconnect: false,
retry: 2, retry: 2,