add nostr-fetch

This commit is contained in:
Ren Amamiya
2023-07-11 14:27:14 +07:00
parent 339783a1a4
commit 41460436df
11 changed files with 202 additions and 163 deletions

View File

@@ -20,3 +20,6 @@ export function dateToUnix(_date?: Date) {
return Math.floor(date.getTime() / 1000);
}
export const nHoursAgo = (hrs: number): number =>
Math.floor((Date.now() - hrs * 60 * 60 * 1000) / 1000);

View File

@@ -4,26 +4,26 @@ import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useNDK } from '@libs/ndk/provider';
import { createNote } from '@libs/storage';
import { dateToUnix, getHourAgo } from '@utils/date';
import { dateToUnix, getHourAgo, nHoursAgo } from '@utils/date';
import { useAccount } from '@utils/hooks/useAccount';
import { usePublish } from '@utils/hooks/usePublish';
import { nip02ToArray } from '@utils/transform';
import { LumeEvent } from '@utils/types';
export function useSocial() {
const queryClient = useQueryClient();
const publish = usePublish();
const { ndk } = useNDK();
const { ndk, fetcher, relayUrls } = useNDK();
const { account } = useAccount();
const { status, data: userFollows } = useQuery(
['userFollows', account.pubkey],
async () => {
const res = await ndk.fetchEvents({
const res = await fetcher.fetchLastEvent(relayUrls, {
kinds: [3],
authors: [account.pubkey],
});
const latest = [...res].slice(-1)[0];
const list = nip02ToArray(latest.tags);
const list = nip02ToArray(res.tags);
return list;
},
{
@@ -68,14 +68,14 @@ export function useSocial() {
});
// fetch events
const filter: NDKFilter = {
authors: [pubkey],
kinds: [1, 6],
since: dateToUnix(getHourAgo(48, new Date())),
};
const events = await ndk.fetchEvents(filter);
events.forEach((event: NDKEvent) => {
createNote(
const events = await fetcher.fetchAllEvents(
relayUrls,
{ authors: [pubkey], kinds: [1, 6] },
{ since: nHoursAgo(48) }
);
for (const event of events) {
await createNote(
event.id,
event.pubkey,
event.kind,
@@ -83,7 +83,7 @@ export function useSocial() {
event.content,
event.created_at
);
});
}
};
return { status, userFollows, follow, unfollow };