add note replies model

This commit is contained in:
Ren Amamiya 2023-06-19 15:41:50 +07:00
parent ad51971239
commit aa8531b32b
7 changed files with 37 additions and 78 deletions

View File

@ -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)
);

View File

@ -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)
);

View File

@ -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(),

View File

@ -61,7 +61,7 @@ export function ThreadBlock({ params }: { params: any }) {
</div>
)}
<div className="px-3">
<RepliesList id={params.content} />
<RepliesList parent_id={params.content} />
</div>
</div>
</div>

View File

@ -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],
);
}

View File

@ -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:

View File

@ -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 (
<div className="mt-5">