add note replies model
This commit is contained in:
@@ -44,7 +44,6 @@ CREATE TABLE
|
|||||||
content TEXT NOT NULL,
|
content TEXT NOT NULL,
|
||||||
created_at INTEGER NOT NULL,
|
created_at INTEGER NOT NULL,
|
||||||
parent_id TEXT,
|
parent_id TEXT,
|
||||||
parent_comment_id TEXT,
|
|
||||||
FOREIGN KEY (account_id) REFERENCES accounts (id)
|
FOREIGN KEY (account_id) REFERENCES accounts (id)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
13
src-tauri/migrations/20230619082415_add_replies.sql
Normal file
13
src-tauri/migrations/20230619082415_add_replies.sql
Normal 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)
|
||||||
|
);
|
||||||
@@ -99,6 +99,12 @@ fn main() {
|
|||||||
sql: include_str!("../migrations/20230617003135_add_channel_messages.sql"),
|
sql: include_str!("../migrations/20230617003135_add_channel_messages.sql"),
|
||||||
kind: MigrationKind::Up,
|
kind: MigrationKind::Up,
|
||||||
},
|
},
|
||||||
|
Migration {
|
||||||
|
version: 20230619082415,
|
||||||
|
description: "add replies",
|
||||||
|
sql: include_str!("../migrations/20230619082415_add_replies.sql"),
|
||||||
|
kind: MigrationKind::Up,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
.build(),
|
.build(),
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ export function ThreadBlock({ params }: { params: any }) {
|
|||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<div className="px-3">
|
<div className="px-3">
|
||||||
<RepliesList id={params.content} />
|
<RepliesList parent_id={params.content} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -123,15 +123,6 @@ export async function countTotalNotes() {
|
|||||||
return result[0].total;
|
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
|
// get all notes
|
||||||
export async function getNotes(time: number, limit: number, offset: number) {
|
export async function getNotes(time: number, limit: number, offset: number) {
|
||||||
const db = await connect();
|
const db = await connect();
|
||||||
@@ -167,34 +158,6 @@ export async function getNotesByAuthor(
|
|||||||
return notes;
|
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
|
// get note by id
|
||||||
export async function getNoteByID(event_id: string) {
|
export async function getNoteByID(event_id: string) {
|
||||||
const db = await connect();
|
const db = await connect();
|
||||||
@@ -204,14 +167,6 @@ export async function getNoteByID(event_id: string) {
|
|||||||
return result[0];
|
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
|
// create note
|
||||||
export async function createNote(
|
export async function createNote(
|
||||||
event_id: string,
|
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
|
// create reply note
|
||||||
export async function createReplyNote(
|
export async function createReplyNote(
|
||||||
|
parent_id: string,
|
||||||
event_id: string,
|
event_id: string,
|
||||||
pubkey: string,
|
pubkey: string,
|
||||||
kind: number,
|
kind: number,
|
||||||
tags: any,
|
tags: any,
|
||||||
content: string,
|
content: string,
|
||||||
created_at: number,
|
created_at: number,
|
||||||
parent_comment_id: string,
|
|
||||||
) {
|
) {
|
||||||
const db = await connect();
|
const db = await connect();
|
||||||
const account = await getActiveAccount();
|
|
||||||
const parentID = getParentID(tags, event_id);
|
|
||||||
|
|
||||||
return await db.execute(
|
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 (?, ?, ?, ?, ?, ?, ?, ?, ?);",
|
"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],
|
||||||
event_id,
|
|
||||||
account.id,
|
|
||||||
pubkey,
|
|
||||||
kind,
|
|
||||||
tags,
|
|
||||||
content,
|
|
||||||
created_at,
|
|
||||||
parentID,
|
|
||||||
parent_comment_id,
|
|
||||||
],
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,13 +25,13 @@ const fetcher = async ([, ndk, id]) => {
|
|||||||
case 1:
|
case 1:
|
||||||
replies += 1;
|
replies += 1;
|
||||||
createReplyNote(
|
createReplyNote(
|
||||||
|
id,
|
||||||
event.id,
|
event.id,
|
||||||
event.pubkey,
|
event.pubkey,
|
||||||
event.kind,
|
event.kind,
|
||||||
event.tags,
|
event.tags,
|
||||||
event.content,
|
event.content,
|
||||||
event.created_at,
|
event.created_at,
|
||||||
id,
|
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
|
|||||||
@@ -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 { EmptyIcon } from "@shared/icons";
|
||||||
import { Reply } from "@shared/notes/replies/item";
|
import { Reply } from "@shared/notes/replies/item";
|
||||||
import { RelayContext } from "@shared/relayProvider";
|
|
||||||
import { useContext } from "react";
|
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
|
|
||||||
const fetcher = async ([, ndk, id]) => {
|
const fetcher = ([, id]) => getReplies(id);
|
||||||
const filter: NDKFilter = {
|
|
||||||
"#e": [id],
|
|
||||||
kinds: [1],
|
|
||||||
};
|
|
||||||
const events = await ndk.fetchEvents(filter);
|
|
||||||
return [...events];
|
|
||||||
};
|
|
||||||
|
|
||||||
export function RepliesList({ id }: { id: string }) {
|
export function RepliesList({ parent_id }: { parent_id: string }) {
|
||||||
const ndk = useContext(RelayContext);
|
const { data }: any = useSWR(["note-replies", parent_id], fetcher);
|
||||||
const { data } = useSWR(["note-replies", ndk, id], fetcher);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mt-5">
|
<div className="mt-5">
|
||||||
|
|||||||
Reference in New Issue
Block a user