clean up & save onboarding process as state

This commit is contained in:
Ren Amamiya
2023-08-10 12:34:11 +07:00
parent d63690e9d0
commit e6d8f084ae
38 changed files with 545 additions and 695 deletions

View File

@@ -1,4 +1,5 @@
import { NDKUser } from '@nostr-dev-kit/ndk';
import { NDKEvent, NDKKind, NDKPrivateKeySigner, NDKUser } from '@nostr-dev-kit/ndk';
import destr from 'destr';
import { LRUCache } from 'lru-cache';
import { NostrEvent } from 'nostr-fetch';
import { nip19 } from 'nostr-tools';
@@ -8,18 +9,22 @@ import {
countTotalNotes,
createChat,
createNote,
getActiveAccount,
getLastLogin,
updateAccount,
} from '@libs/storage';
import { useStronghold } from '@stores/stronghold';
import { nHoursAgo } from '@utils/date';
import { useAccount } from '@utils/hooks/useAccount';
export function useNostr() {
const privkey = useStronghold((state) => state.privkey);
const { ndk, relayUrls, fetcher } = useNDK();
const { account } = useAccount();
async function fetchNetwork(prevFollow?: string[]) {
const account = await getActiveAccount();
const follows = new Set<string>(prevFollow || []);
const lruNetwork = new LRUCache<string, string, void>({ max: 300 });
@@ -27,6 +32,7 @@ export function useNostr() {
// fetch user's follows
if (!prevFollow) {
console.log("fetching user's follow...");
const user = ndk.getUser({ hexpubkey: account.pubkey });
const list = await user.follows();
list.forEach((item: NDKUser) => {
@@ -36,7 +42,7 @@ export function useNostr() {
// fetch network
if (!account.network) {
console.log('fetching network...', follows.size);
console.log("fetching user's network...");
const events = await fetcher.fetchAllEvents(
relayUrls,
{ kinds: [3], authors: [...follows] },
@@ -62,14 +68,15 @@ export function useNostr() {
return [...new Set([...follows, ...network])];
}
const fetchNotes = async (prevFollow?: string[]) => {
async function fetchNotes(prevFollow?: string[]) {
try {
const network = (await fetchNetwork(prevFollow)) as string[];
const network = await fetchNetwork(prevFollow);
const totalNotes = await countTotalNotes();
const lastLogin = await getLastLogin();
if (network.length > 0) {
console.log('fetching notes...');
let since: number;
if (totalNotes === 0 || lastLogin === 0) {
since = nHoursAgo(24);
@@ -96,15 +103,15 @@ export function useNostr() {
}
}
return true;
return { status: 'ok' };
} catch (e) {
console.log('error: ', e);
console.error('failed fetch notes, error: ', e);
return { status: 'failed', message: e };
}
};
}
const fetchChats = async () => {
async function fetchChats() {
try {
const account = await getActiveAccount();
const lastLogin = await getLastLogin();
const incomingMessages = await fetcher.fetchAllEvents(
relayUrls,
@@ -128,11 +135,60 @@ export function useNostr() {
);
}
return true;
return { status: 'ok' };
} catch (e) {
console.log('error: ', e);
console.error('failed fetch incoming messages, error: ', e);
return { status: 'failed', message: e };
}
}
const publish = async ({
content,
kind,
tags,
}: {
content: string;
kind: NDKKind | number;
tags: string[][];
}): Promise<NDKEvent> => {
if (!privkey) throw new Error('Private key not found');
const event = new NDKEvent(ndk);
const signer = new NDKPrivateKeySigner(privkey);
event.content = content;
event.kind = kind;
event.created_at = Math.floor(Date.now() / 1000);
event.pubkey = account.pubkey;
event.tags = tags;
await event.sign(signer);
await event.publish();
return event;
};
return { fetchNotes, fetchChats };
const createZap = async (event: NostrEvent, amount: number, message?: string) => {
// @ts-expect-error, LumeEvent to NostrEvent
event.id = event.event_id;
// @ts-expect-error, LumeEvent to NostrEvent
if (typeof event.content !== 'string') event.content = event.content.original;
if (typeof event.tags === 'string') event.tags = destr(event.tags);
if (!privkey) throw new Error('Private key not found');
if (!ndk.signer) {
const signer = new NDKPrivateKeySigner(privkey);
ndk.signer = signer;
}
const ndkEvent = new NDKEvent(ndk, event);
const res = await ndkEvent.zap(amount, message ?? 'zap from lume');
return res;
};
return { fetchNotes, fetchChats, publish, createZap };
}

View File

@@ -1,60 +0,0 @@
import { NDKEvent, NDKKind, NDKPrivateKeySigner, NostrEvent } from '@nostr-dev-kit/ndk';
import destr from 'destr';
import { useNDK } from '@libs/ndk/provider';
import { useStronghold } from '@stores/stronghold';
import { useAccount } from '@utils/hooks/useAccount';
export function usePublish() {
const { ndk } = useNDK();
const { account } = useAccount();
const privkey = useStronghold((state) => state.privkey);
const publish = async ({
content,
kind,
tags,
}: {
content: string;
kind: NDKKind | number;
tags: string[][];
}): Promise<NDKEvent> => {
if (!privkey) throw new Error('Private key not found');
const event = new NDKEvent(ndk);
const signer = new NDKPrivateKeySigner(privkey);
event.content = content;
event.kind = kind;
event.created_at = Math.floor(Date.now() / 1000);
event.pubkey = account.pubkey;
event.tags = tags;
await event.sign(signer);
await event.publish();
return event;
};
const createZap = async (event: NostrEvent, amount: number, message?: string) => {
// @ts-expect-error, lumeevent to nostrevent
event.id = event.event_id;
// @ts-expect-error, lumeevent to nostrevent
if (typeof event.content !== 'string') event.content = event.content.original;
if (typeof event.tags === 'string') event.tags = destr(event.tags);
if (!privkey) throw new Error('Private key not found');
if (!ndk.signer) {
const signer = new NDKPrivateKeySigner(privkey);
ndk.signer = signer;
}
const ndkEvent = new NDKEvent(ndk, event);
const res = await ndkEvent.zap(amount, message ?? 'test zap from lume');
return res;
};
return { publish, createZap };
}

View File

@@ -5,13 +5,14 @@ import { createNote } from '@libs/storage';
import { nHoursAgo } from '@utils/date';
import { useAccount } from '@utils/hooks/useAccount';
import { usePublish } from '@utils/hooks/usePublish';
import { nip02ToArray } from '@utils/transform';
import { useNostr } from './useNostr';
export function useSocial() {
const queryClient = useQueryClient();
const { publish } = usePublish();
const { publish } = useNostr();
const { fetcher, relayUrls } = useNDK();
const { account } = useAccount();
const { status, data: userFollows } = useQuery(

View File

@@ -3,10 +3,10 @@ import { open } from '@tauri-apps/plugin-dialog';
import { VoidApi } from '@void-cat/api';
import { createBlobFromFile } from '@utils/createBlobFromFile';
import { usePublish } from '@utils/hooks/usePublish';
import { useNostr } from '@utils/hooks/useNostr';
export function useImageUploader() {
const { publish } = usePublish();
const { publish } = useNostr();
const upload = async (file: null | string, nip94?: boolean) => {
const voidcat = new VoidApi('https://void.cat');