diff --git a/src-tauri/migrations/20230418013219_initial_data.sql b/src-tauri/migrations/20230418013219_initial_data.sql index 4f7ac9c3..0ab7a60a 100644 --- a/src-tauri/migrations/20230418013219_initial_data.sql +++ b/src-tauri/migrations/20230418013219_initial_data.sql @@ -44,7 +44,6 @@ CREATE TABLE content TEXT NOT NULL, created_at INTEGER NOT NULL, parent_id TEXT, - parent_comment_id TEXT, FOREIGN KEY (account_id) REFERENCES accounts (id) ); diff --git a/src-tauri/migrations/20230619082415_add_replies.sql b/src-tauri/migrations/20230619082415_add_replies.sql new file mode 100644 index 00000000..9dc877ca --- /dev/null +++ b/src-tauri/migrations/20230619082415_add_replies.sql @@ -0,0 +1,13 @@ +-- Add migration script here +CREATE TABLE + replies ( + id INTEGER NOT NULL PRIMARY KEY, + parent_id TEXT NOT NULL, + event_id TEXT NOT NULL UNIQUE, + pubkey TEXT NOT NULL, + kind INTEGER NOT NULL DEFAULT 1, + tags JSON, + content TEXT NOT NULL, + created_at INTEGER NOT NULL, + FOREIGN KEY (parent_id) REFERENCES notes (event_id) + ); \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 007543f1..937a3695 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -99,6 +99,12 @@ fn main() { sql: include_str!("../migrations/20230617003135_add_channel_messages.sql"), kind: MigrationKind::Up, }, + Migration { + version: 20230619082415, + description: "add replies", + sql: include_str!("../migrations/20230619082415_add_replies.sql"), + kind: MigrationKind::Up, + }, ], ) .build(), diff --git a/src/app/space/components/blocks/thread.tsx b/src/app/space/components/blocks/thread.tsx index ae2fe34c..f00310a9 100644 --- a/src/app/space/components/blocks/thread.tsx +++ b/src/app/space/components/blocks/thread.tsx @@ -61,7 +61,7 @@ export function ThreadBlock({ params }: { params: any }) { )}
- +
diff --git a/src/libs/storage.tsx b/src/libs/storage.tsx index 9114d646..302e04b0 100644 --- a/src/libs/storage.tsx +++ b/src/libs/storage.tsx @@ -123,15 +123,6 @@ export async function countTotalNotes() { return result[0].total; } -// count total notes -export async function countTotalLongNotes() { - const db = await connect(); - const result = await db.select( - 'SELECT COUNT(*) AS "total" FROM notes WHERE kind = 30023;', - ); - return result[0].total; -} - // get all notes export async function getNotes(time: number, limit: number, offset: number) { const db = await connect(); @@ -167,34 +158,6 @@ export async function getNotesByAuthor( return notes; } -// get all long notes -export async function getLongNotes( - time: number, - limit: number, - offset: number, -) { - const db = await connect(); - - const notes: any = { data: null, nextCursor: 0 }; - const query: any = await db.select( - `SELECT * FROM notes WHERE created_at <= "${time}" AND kind = 30023 ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";`, - ); - - notes["data"] = query; - notes["nextCursor"] = offset + limit; - - return notes; -} - -// get all note authors -export async function getNoteAuthors() { - const db = await connect(); - const result = await db.select( - "SELECT DISTINCT pubkey FROM notes ORDER BY created_at DESC", - ); - return result; -} - // get note by id export async function getNoteByID(event_id: string) { const db = await connect(); @@ -204,14 +167,6 @@ export async function getNoteByID(event_id: string) { return result[0]; } -// get all latest notes -export async function getLatestNotes(time: number) { - const db = await connect(); - return await db.select( - `SELECT * FROM notes WHERE created_at > "${time}" GROUP BY parent_id ORDER BY created_at DESC;`, - ); -} - // create note export async function createNote( event_id: string, @@ -231,33 +186,28 @@ export async function createNote( ); } +// get note replies +export async function getReplies(parent_id: string) { + const db = await connect(); + return await db.select( + `SELECT * FROM replies WHERE parent_id = "${parent_id}" ORDER BY created_at DESC;`, + ); +} + // create reply note export async function createReplyNote( + parent_id: string, event_id: string, pubkey: string, kind: number, 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 OR IGNORE INTO notes (event_id, account_id, pubkey, kind, tags, content, created_at, parent_id, parent_comment_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", - [ - event_id, - account.id, - pubkey, - kind, - tags, - content, - created_at, - parentID, - parent_comment_id, - ], + "INSERT OR IGNORE INTO replies (parent_id, event_id, pubkey, kind, tags, content, created_at) VALUES (?, ?, ?, ?, ?, ?, ?);", + [parent_id, event_id, pubkey, kind, tags, content, created_at], ); } diff --git a/src/shared/notes/metadata.tsx b/src/shared/notes/metadata.tsx index 0cdc1965..1a669ad4 100644 --- a/src/shared/notes/metadata.tsx +++ b/src/shared/notes/metadata.tsx @@ -25,13 +25,13 @@ const fetcher = async ([, ndk, id]) => { case 1: replies += 1; createReplyNote( + id, event.id, event.pubkey, event.kind, event.tags, event.content, event.created_at, - id, ); break; case 6: diff --git a/src/shared/notes/replies/list.tsx b/src/shared/notes/replies/list.tsx index a67b6832..c39bd5bb 100644 --- a/src/shared/notes/replies/list.tsx +++ b/src/shared/notes/replies/list.tsx @@ -1,22 +1,13 @@ -import { NDKEvent, NDKFilter } from "@nostr-dev-kit/ndk"; +import { getReplies } from "@libs/storage"; +import { NDKEvent } from "@nostr-dev-kit/ndk"; import { EmptyIcon } from "@shared/icons"; import { Reply } from "@shared/notes/replies/item"; -import { RelayContext } from "@shared/relayProvider"; -import { useContext } from "react"; import useSWR from "swr"; -const fetcher = async ([, ndk, id]) => { - const filter: NDKFilter = { - "#e": [id], - kinds: [1], - }; - const events = await ndk.fetchEvents(filter); - return [...events]; -}; +const fetcher = ([, id]) => getReplies(id); -export function RepliesList({ id }: { id: string }) { - const ndk = useContext(RelayContext); - const { data } = useSWR(["note-replies", ndk, id], fetcher); +export function RepliesList({ parent_id }: { parent_id: string }) { + const { data }: any = useSWR(["note-replies", parent_id], fetcher); return (