fully support nip05

This commit is contained in:
Ren Amamiya
2023-06-30 16:36:03 +07:00
parent 1ba7f823cb
commit 332dbf608d
17 changed files with 250 additions and 245 deletions

View File

@@ -1,4 +1,3 @@
import { createReplyNote } from "./storage";
import NDK, {
NDKConstructorParams,
NDKEvent,
@@ -15,21 +14,10 @@ export async function initNDK(relays?: string[]): Promise<NDK> {
const opts: NDKConstructorParams = {};
const defaultRelays = new Set(relays || FULL_RELAYS);
/*
for (const relay of defaultRelays) {
const url = new URL(relay);
url.protocol = url.protocol = url.protocol.replace("wss", "https");
const res = await fetch(url.href, { method: "HEAD", timeout: 5 });
if (!res.ok) {
defaultRelays.delete(relay);
}
}
*/
opts.explicitRelayUrls = [...defaultRelays];
const ndk = new NDK(opts);
await ndk.connect();
await ndk.connect(500);
return ndk;
}
@@ -57,15 +45,10 @@ export async function prefetchEvents(
}
export function usePublish() {
const { account } = useAccount();
const ndk = useContext(RelayContext);
const { account } = useAccount();
if (!ndk.signer) {
const signer = new NDKPrivateKeySigner(account?.privkey);
ndk.signer = signer;
}
const publish = ({
const publish = async ({
content,
kind,
tags,
@@ -73,8 +56,9 @@ export function usePublish() {
content: string;
kind: NDKKind;
tags: string[][];
}): NDKEvent => {
}): Promise<NDKEvent> => {
const event = new NDKEvent(ndk);
const signer = new NDKPrivateKeySigner(account.privkey);
event.content = content;
event.kind = kind;
@@ -82,7 +66,8 @@ export function usePublish() {
event.pubkey = account.pubkey;
event.tags = tags;
event.publish();
await event.sign(signer);
await event.publish();
return event;
};

View File

@@ -1,6 +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";
let db: null | Database = null;
@@ -73,55 +71,6 @@ export async function updateAccount(
);
}
// get all plebs
export async function getPlebs() {
const db = await connect();
return await db.select("SELECT * FROM plebs ORDER BY created_at DESC;");
}
// get pleb by pubkey
export async function getPleb(npub: string) {
const db = await connect();
const result = await db.select(`SELECT * FROM plebs WHERE npub = "${npub}";`);
if (result) {
return result[0];
} else {
return null;
}
}
// create pleb
export async function createPleb(key: string, data: NDKUserProfile) {
const db = await connect();
const now = Math.floor(Date.now() / 1000);
let npub: string;
if (key.substring(0, 4) === "npub") {
npub = key;
} else {
npub = nip19.npubEncode(key);
}
return await db.execute(
"INSERT OR REPLACE INTO plebs (npub, name, displayName, image, banner, bio, nip05, lud06, lud16, about, zapService, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);",
[
npub,
data.name,
data.displayName,
data.image,
data.banner,
data.bio,
data.nip05,
data.lud06,
data.lud16,
data.about,
data.zapService,
now,
],
);
}
// count total notes
export async function countTotalChannels() {
const db = await connect();
@@ -139,14 +88,14 @@ export async function countTotalNotes() {
}
// get all notes
export async function getNotes(time: number, limit: number, offset: number) {
export async function getNotes(limit: number, offset: number) {
const db = await connect();
const totalNotes = await countTotalNotes();
const nextCursor = offset + limit;
const notes: any = { data: null, nextCursor: 0 };
const query: any = await db.select(
`SELECT * FROM notes WHERE created_at <= "${time}" AND kind IN (1, 6, 1063) GROUP BY parent_id ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";`,
`SELECT * FROM notes WHERE kind IN (1, 6, 1063) GROUP BY parent_id ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";`,
);
notes["data"] = query;
@@ -169,7 +118,6 @@ export async function getNotesByPubkey(pubkey: string) {
// get all notes by authors
export async function getNotesByAuthors(
authors: string,
time: number,
limit: number,
offset: number,
) {
@@ -181,7 +129,7 @@ export async function getNotesByAuthors(
const notes: any = { data: null, nextCursor: 0 };
const query: any = await db.select(
`SELECT * FROM notes WHERE created_at <= "${time}" AND pubkey IN (${finalArray}) AND kind IN (1, 6, 1063) GROUP BY parent_id ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";`,
`SELECT * FROM notes WHERE pubkey IN (${finalArray}) AND kind IN (1, 6, 1063) GROUP BY parent_id ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";`,
);
notes["data"] = query;