mirror of
https://github.com/luminous-devs/lume.git
synced 2024-09-29 16:30:55 +00:00
feat: update ark provider
This commit is contained in:
parent
bc48391a1a
commit
353c18bb76
@ -145,7 +145,6 @@ export class Ark {
|
||||
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST,
|
||||
});
|
||||
|
||||
if (!profile) return null;
|
||||
return profile;
|
||||
} catch {
|
||||
throw new Error("user not found");
|
||||
@ -167,8 +166,9 @@ export class Ark {
|
||||
(user) => user.pubkey,
|
||||
);
|
||||
|
||||
if (!pubkey || pubkey === this.account.pubkey)
|
||||
if (!pubkey || pubkey === this.account.pubkey) {
|
||||
this.account.contacts = contacts;
|
||||
}
|
||||
|
||||
return contacts;
|
||||
} catch (e) {
|
||||
|
@ -1,8 +1,10 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { useArk } from "./useArk";
|
||||
|
||||
export function useProfile(pubkey: string) {
|
||||
const ark = useArk();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const {
|
||||
isLoading,
|
||||
isError,
|
||||
@ -17,6 +19,9 @@ export function useProfile(pubkey: string) {
|
||||
);
|
||||
return profile;
|
||||
},
|
||||
initialData: () => {
|
||||
return queryClient.getQueryData(["user", pubkey]);
|
||||
},
|
||||
refetchOnMount: false,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnReconnect: false,
|
||||
|
@ -23,6 +23,7 @@ import { useSetAtom } from "jotai";
|
||||
import Linkify from "linkify-react";
|
||||
import { normalizeRelayUrlSet } from "nostr-fetch";
|
||||
import { PropsWithChildren, useEffect, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { Ark } from "./ark";
|
||||
import { LumeContext } from "./context";
|
||||
|
||||
@ -80,24 +81,28 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
|
||||
|
||||
return new NDKPrivateKeySigner(userPrivkey);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
toast.error(String(e));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function initNDK() {
|
||||
try {
|
||||
const explicitRelayUrls = normalizeRelayUrlSet([
|
||||
"wss://nostr.mutinywallet.com/",
|
||||
"wss://bostr.nokotaro.com/",
|
||||
]);
|
||||
|
||||
const outboxRelayUrls = normalizeRelayUrlSet(["wss://purplepag.es/"]);
|
||||
|
||||
const tauriCache = new NDKCacheAdapterTauri(storage);
|
||||
const ndk = new NDK({
|
||||
cacheAdapter: tauriCache,
|
||||
explicitRelayUrls,
|
||||
outboxRelayUrls,
|
||||
enableOutboxModel: !storage.settings.lowPower,
|
||||
autoConnectUserRelays: !storage.settings.lowPower,
|
||||
autoFetchUserMutelist: !storage.settings.lowPower,
|
||||
autoFetchUserMutelist: false, // #TODO: add support mute list
|
||||
clientName: "Lume",
|
||||
});
|
||||
|
||||
@ -115,7 +120,10 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
|
||||
await ndk.connect(3000);
|
||||
|
||||
// auth
|
||||
ndk.relayAuthDefaultPolicy = async (relay: NDKRelay, challenge: string) => {
|
||||
ndk.relayAuthDefaultPolicy = async (
|
||||
relay: NDKRelay,
|
||||
challenge: string,
|
||||
) => {
|
||||
const signIn = NDKRelayAuthPolicies.signIn({ ndk });
|
||||
const event = await signIn(relay, challenge).catch((e) =>
|
||||
console.log("auth failed", e),
|
||||
@ -129,6 +137,9 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
|
||||
};
|
||||
|
||||
setNDK(ndk);
|
||||
} catch (e) {
|
||||
toast.error(String(e));
|
||||
}
|
||||
}
|
||||
|
||||
async function initArk() {
|
||||
@ -137,12 +148,25 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
|
||||
// ark utils
|
||||
const ark = new Ark({ ndk, account: storage.currentUser });
|
||||
|
||||
try {
|
||||
if (ndk && storage.currentUser) {
|
||||
const user = new NDKUser({ pubkey: storage.currentUser.pubkey });
|
||||
ndk.activeUser = user;
|
||||
|
||||
// update contacts
|
||||
await ark.getUserContacts();
|
||||
const contacts = await ark.getUserContacts();
|
||||
|
||||
if (contacts?.length) {
|
||||
console.log("total contacts: ", contacts.length);
|
||||
for (const pubkey of ark.account.contacts) {
|
||||
await queryClient.prefetchQuery({
|
||||
queryKey: ["user", pubkey],
|
||||
queryFn: async () => {
|
||||
return await ark.getUserProfile(pubkey);
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// subscribe for new activity
|
||||
const activitySub = ndk.subscribe(
|
||||
@ -183,56 +207,9 @@ export const LumeProvider = ({ children }: PropsWithChildren<object>) => {
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
// prefetch activty
|
||||
await queryClient.prefetchInfiniteQuery({
|
||||
queryKey: ["activity"],
|
||||
initialPageParam: 0,
|
||||
queryFn: async ({
|
||||
signal,
|
||||
pageParam,
|
||||
}: {
|
||||
signal: AbortSignal;
|
||||
pageParam: number;
|
||||
}) => {
|
||||
const events = await ark.getInfiniteEvents({
|
||||
filter: {
|
||||
kinds: [NDKKind.Text, NDKKind.Repost, NDKKind.Zap],
|
||||
"#p": [ark.account.pubkey],
|
||||
},
|
||||
limit: FETCH_LIMIT,
|
||||
pageParam,
|
||||
signal,
|
||||
});
|
||||
|
||||
return events;
|
||||
},
|
||||
});
|
||||
|
||||
// prefetch timeline
|
||||
await queryClient.prefetchInfiniteQuery({
|
||||
queryKey: ["timeline-9999"],
|
||||
initialPageParam: 0,
|
||||
queryFn: async ({
|
||||
signal,
|
||||
pageParam,
|
||||
}: {
|
||||
signal: AbortSignal;
|
||||
pageParam: number;
|
||||
}) => {
|
||||
const events = await ark.getInfiniteEvents({
|
||||
filter: {
|
||||
kinds: [NDKKind.Text, NDKKind.Repost],
|
||||
authors: ark.account.contacts,
|
||||
},
|
||||
limit: FETCH_LIMIT,
|
||||
pageParam,
|
||||
signal,
|
||||
});
|
||||
|
||||
return events;
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
toast.error(String(e));
|
||||
}
|
||||
|
||||
setArk(ark);
|
||||
|
Loading…
Reference in New Issue
Block a user