migrate to ndk
This commit is contained in:
@@ -1,56 +1,31 @@
|
||||
import { RelayContext } from "@shared/relayProvider";
|
||||
import { useActiveAccount } from "@stores/accounts";
|
||||
import { READONLY_RELAYS } from "@stores/constants";
|
||||
import { createNote, getNoteByID } from "@utils/storage";
|
||||
import { getParentID } from "@utils/transform";
|
||||
import { useContext } from "react";
|
||||
import useSWR from "swr";
|
||||
import useSWRSubscription from "swr/subscription";
|
||||
|
||||
const fetcher = ([, id]) => getNoteByID(id);
|
||||
const fetcher = async ([, ndk, id]) => {
|
||||
const result = await getNoteByID(id);
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
} else {
|
||||
const event = await ndk.fetchEvent(id);
|
||||
await createNote(
|
||||
event.id,
|
||||
event.pubkey,
|
||||
event.kind,
|
||||
event.tags,
|
||||
event.content,
|
||||
event.created_at,
|
||||
);
|
||||
event["event_id"] = event.id;
|
||||
return event;
|
||||
}
|
||||
};
|
||||
|
||||
export function useEvent(id: string) {
|
||||
const pool: any = useContext(RelayContext);
|
||||
const account = useActiveAccount((state: any) => state.account);
|
||||
const ndk = useContext(RelayContext);
|
||||
const { data } = useSWR(["note", ndk, id], fetcher);
|
||||
|
||||
const { data: cache } = useSWR(["event", id], fetcher);
|
||||
const { data: newest } = useSWRSubscription(
|
||||
!cache ? id : null,
|
||||
(key, { next }) => {
|
||||
const unsubscribe = pool.subscribe(
|
||||
[
|
||||
{
|
||||
ids: [key],
|
||||
},
|
||||
],
|
||||
READONLY_RELAYS,
|
||||
(event: any) => {
|
||||
const parentID = getParentID(event.tags, event.id);
|
||||
// insert event to local database
|
||||
createNote(
|
||||
event.id,
|
||||
account.id,
|
||||
event.pubkey,
|
||||
event.kind,
|
||||
event.tags,
|
||||
event.content,
|
||||
event.created_at,
|
||||
);
|
||||
// update state
|
||||
next(null, event);
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
},
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
return cache ? cache : newest;
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import NDK from "@nostr-dev-kit/ndk";
|
||||
import { RelayContext } from "@shared/relayProvider";
|
||||
import { METADATA_RELAY } from "@stores/constants";
|
||||
import { createPleb, getPleb } from "@utils/storage";
|
||||
import { nip19 } from "nostr-tools";
|
||||
import { useContext } from "react";
|
||||
import useSWR from "swr";
|
||||
import useSWRSubscription from "swr/subscription";
|
||||
|
||||
const fetcher = async (key: string) => {
|
||||
const fetcher = async ([, ndk, key]) => {
|
||||
let npub: string;
|
||||
|
||||
if (key.substring(0, 4) === "npub") {
|
||||
@@ -18,59 +17,27 @@ const fetcher = async (key: string) => {
|
||||
const current = Math.floor(Date.now() / 1000);
|
||||
const result = await getPleb(npub);
|
||||
|
||||
if (result && result.created_at + 86400 < current) {
|
||||
if (result && result.created_at + 86400 > current) {
|
||||
return result;
|
||||
} else {
|
||||
return null;
|
||||
const user = ndk.getUser({ npub });
|
||||
await user.fetchProfile();
|
||||
await createPleb(key, user.profile);
|
||||
|
||||
return user.profile;
|
||||
}
|
||||
};
|
||||
|
||||
export function useProfile(key: string) {
|
||||
const pool: any = useContext(RelayContext);
|
||||
|
||||
const {
|
||||
data: cache,
|
||||
error,
|
||||
isLoading,
|
||||
} = useSWR(key, fetcher, {
|
||||
const ndk = useContext(RelayContext);
|
||||
const { data, error, isLoading } = useSWR(["profile", ndk, key], fetcher, {
|
||||
revalidateIfStale: false,
|
||||
revalidateOnFocus: false,
|
||||
revalidateOnReconnect: true,
|
||||
});
|
||||
|
||||
const { data: newest } = useSWRSubscription(
|
||||
cache ? null : key,
|
||||
(_, { next }) => {
|
||||
const unsubscribe = pool.subscribe(
|
||||
[
|
||||
{
|
||||
authors: [key],
|
||||
kinds: [0],
|
||||
},
|
||||
],
|
||||
METADATA_RELAY,
|
||||
(event: { content: string }) => {
|
||||
const content = JSON.parse(event.content);
|
||||
// update state
|
||||
next(null, content);
|
||||
// save to database
|
||||
createPleb(key, event);
|
||||
},
|
||||
undefined,
|
||||
undefined,
|
||||
{
|
||||
unsubscribeOnEose: true,
|
||||
},
|
||||
);
|
||||
|
||||
return () => {
|
||||
unsubscribe();
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
user: newest ? newest : cache,
|
||||
user: data,
|
||||
isLoading,
|
||||
isError: error,
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { NDKTag, NDKUserProfile } from "@nostr-dev-kit/ndk";
|
||||
import { getParentID } from "@utils/transform";
|
||||
import { nip19 } from "nostr-tools";
|
||||
import Database from "tauri-plugin-sql-api";
|
||||
@@ -76,10 +77,9 @@ export async function getPleb(npub: string) {
|
||||
}
|
||||
|
||||
// create pleb
|
||||
export async function createPleb(key: string, json: any) {
|
||||
export async function createPleb(key: string, data: NDKUserProfile) {
|
||||
const db = await connect();
|
||||
const data = JSON.parse(json.content);
|
||||
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
let npub: string;
|
||||
|
||||
if (key.substring(0, 4) === "npub") {
|
||||
@@ -89,21 +89,20 @@ export async function createPleb(key: string, json: any) {
|
||||
}
|
||||
|
||||
return await db.execute(
|
||||
"INSERT OR REPLACE INTO plebs (npub, display_name, name, username, about, bio, website, picture, banner, nip05, lud06, lud16, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
|
||||
"INSERT OR REPLACE INTO plebs (npub, name, displayName, image, banner, bio, nip05, lud06, lud16, about, zapService, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
|
||||
[
|
||||
npub,
|
||||
data.display_name || data.displayName,
|
||||
data.name,
|
||||
data.username,
|
||||
data.about,
|
||||
data.bio,
|
||||
data.website,
|
||||
data.picture || data.image,
|
||||
data.displayName,
|
||||
data.image,
|
||||
data.banner,
|
||||
data.bio,
|
||||
data.nip05,
|
||||
data.lud06,
|
||||
data.lud16,
|
||||
data.created_at,
|
||||
data.about,
|
||||
data.zapService,
|
||||
now,
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -216,39 +215,41 @@ export async function getLatestNotes(time: number) {
|
||||
// create note
|
||||
export async function createNote(
|
||||
event_id: string,
|
||||
account_id: number,
|
||||
pubkey: string,
|
||||
kind: number,
|
||||
tags: string[],
|
||||
tags: any,
|
||||
content: string,
|
||||
created_at: number,
|
||||
) {
|
||||
const db = await connect();
|
||||
const account = await getActiveAccount();
|
||||
const parentID = getParentID(tags, event_id);
|
||||
|
||||
return await db.execute(
|
||||
"INSERT INTO notes (event_id, account_id, pubkey, kind, tags, content, created_at, parent_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?);",
|
||||
[event_id, account_id, pubkey, kind, tags, content, created_at, parentID],
|
||||
"INSERT OR IGNORE INTO notes (event_id, account_id, pubkey, kind, tags, content, created_at, parent_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?);",
|
||||
[event_id, account.id, pubkey, kind, tags, content, created_at, parentID],
|
||||
);
|
||||
}
|
||||
|
||||
// create reply note
|
||||
export async function createReplyNote(
|
||||
event_id: string,
|
||||
account_id: number,
|
||||
pubkey: string,
|
||||
kind: number,
|
||||
tags: string[],
|
||||
tags: any,
|
||||
content: string,
|
||||
created_at: number,
|
||||
parent_comment_id: string,
|
||||
) {
|
||||
const db = await connect();
|
||||
const account = await getActiveAccount();
|
||||
const parentID = getParentID(tags, event_id);
|
||||
|
||||
return await db.execute(
|
||||
"INSERT INTO notes (event_id, account_id, pubkey, kind, tags, content, created_at, parent_id, parent_comment_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
|
||||
"INSERT OR IGNORE INTO notes (event_id, account_id, pubkey, kind, tags, content, created_at, parent_id, parent_comment_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);",
|
||||
[
|
||||
event_id,
|
||||
account_id,
|
||||
account.id,
|
||||
pubkey,
|
||||
kind,
|
||||
tags,
|
||||
@@ -298,7 +299,7 @@ export async function updateChannelMetadata(event_id: string, value: string) {
|
||||
|
||||
return await db.execute(
|
||||
"UPDATE channels SET name = ?, picture = ?, about = ? WHERE event_id = ?;",
|
||||
[data.name, data.picture, data.about, event_id],
|
||||
[data.name, data.image, data.about, event_id],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,20 @@
|
||||
import destr from "destr";
|
||||
import { nip19 } from "nostr-tools";
|
||||
|
||||
export function truncateContent(str, n) {
|
||||
return str.length > n ? `${str.slice(0, n - 1)}…` : str;
|
||||
}
|
||||
|
||||
export function setToArray(tags: any) {
|
||||
const newArray = [];
|
||||
tags.forEach((item) => {
|
||||
const hexpubkey = nip19.decode(item.npub).data;
|
||||
newArray.push(hexpubkey);
|
||||
});
|
||||
|
||||
return newArray;
|
||||
}
|
||||
|
||||
// convert NIP-02 to array of pubkey
|
||||
export function nip02ToArray(tags: any) {
|
||||
const arr = [];
|
||||
|
||||
Reference in New Issue
Block a user