added comment sql

This commit is contained in:
Ren Amamiya 2023-03-26 08:53:50 +07:00
parent 5d89a66ee0
commit 28215dbd5f
5 changed files with 37 additions and 24 deletions

View File

@ -85,5 +85,6 @@ CREATE TABLE
kind INTEGER NOT NULL DEFAULT 1,
tags TEXT NOT NULL,
content TEXT NOT NULL,
parent_id TEXT
parent_id TEXT,
parent_comment_id TEXT
);

View File

@ -4,6 +4,8 @@ import { RelayContext } from '@components/relaysProvider';
import { relaysAtom } from '@stores/relays';
import { createCacheCommentNote } from '@utils/storage';
import { useAtomValue } from 'jotai';
import { useContext, useEffect, useState } from 'react';
@ -37,7 +39,10 @@ export default function NoteMetadata({
(event: any) => {
switch (event.kind) {
case 1:
// update state
setComments((comments) => (comments += 1));
// save comment to database
createCacheCommentNote(event, eventID);
break;
case 7:
if (event.content === '🤙' || event.content === '+') {

View File

@ -86,7 +86,7 @@ export const NoteRepost = memo(function NoteRepost({ id }: { id: string }) {
if (event) {
return (
<div className="relative mt-3 rounded-lg border border-zinc-700 bg-zinc-800 p-2 py-3">
<div className="relative mt-3 mb-2 rounded-lg border border-zinc-700 bg-zinc-800 p-2 py-3">
<div className="relative z-10 flex flex-col">
<UserExtend pubkey={event.pubkey} time={event.created_at} />
<div className="-mt-5 pl-[52px]">

View File

@ -7,7 +7,7 @@ import { RelayContext } from '@components/relaysProvider';
import { relaysAtom } from '@stores/relays';
import { getNoteByID } from '@utils/storage';
import { getAllCommentNotes, getNoteByID } from '@utils/storage';
import { useAtomValue } from 'jotai';
import { useRouter } from 'next/router';
@ -33,31 +33,12 @@ export default function Page() {
const [comments, setComments] = useState([]);
useEffect(() => {
let unsubscribe: () => void;
getNoteByID(id)
.then((res) => {
// update state
setRootEvent(res);
// get all comments
unsubscribe = pool.subscribe(
[
{
'#e': [id],
kinds: [1],
},
],
relays,
(event: any) => {
setComments((comments) => [...comments, event]);
}
);
getAllCommentNotes(id).then((res: any) => setComments(res));
})
.catch(console.error);
return () => {
unsubscribe();
};
}, [id, pool, relays]);
return (

View File

@ -88,7 +88,7 @@ export async function getCacheProfile(id) {
return result[0];
}
// get note by id
// get all notes
export async function getAllNotes() {
const db = await connect();
return await db.select(`SELECT * FROM cache_notes GROUP BY parent_id ORDER BY created_at DESC LIMIT 1000`);
@ -117,3 +117,29 @@ export async function createCacheNote(data) {
]
);
}
// get all comment notes
export async function getAllCommentNotes(eid) {
const db = await connect();
return await db.select(
`SELECT * FROM cache_notes WHERE parent_comment_id = "${eid}" ORDER BY created_at DESC LIMIT 1000`
);
}
// create cache comment note
export async function createCacheCommentNote(data, eid) {
const db = await connect();
return await db.execute(
'INSERT OR IGNORE INTO cache_notes (id, pubkey, created_at, kind, content, tags, parent_id, parent_comment_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?);',
[
data.id,
data.pubkey,
data.created_at,
data.kind,
data.content,
JSON.stringify(data.tags),
getParentID(data.tags, data.id),
eid,
]
);
}