comments and replies work now

This commit is contained in:
biglyderv 2025-02-26 01:40:27 -05:00
parent 11147482d2
commit 9e5989825f
Signed by: biglyderv
GPG key ID: 0E2EB0B4CD7397B5
6 changed files with 102 additions and 10 deletions

View file

@ -22,20 +22,24 @@ router.get('/:id', async (req, res, next) => {
for (let reply of replies) {
let dat = await db.all('SELECT * FROM comment WHERE id = ?', [
reply.parentId
reply.childId
]);
if (reply.parentType != 'comment' || dat.length < 1) {
reply.comment = {username: 'fail', date: 'fail', content: 'fail'};
if (reply.childType != 'comment' || dat.length < 1) {
reply.comment = 'fail'
continue;
}
reply.comment = dat[0];
reply.comment.id = reply.childId;
reply.comment.type = reply.childType;
}
replies = replies.filter(x => x.comment != 'fail');
console.log(replies)
res.ctx.mainPage = 'commenter'
res.ctx.mainCtx = {
replies,
opLink: `/comments/${req.params.id}`
replies
}
next();
})

46
routes/commenter.js Normal file
View file

@ -0,0 +1,46 @@
import { Router } from "express";
import { apiStat } from "../lib.js";
import { initDb } from "../db.js";
import { randomBytes } from 'node:crypto';
let db = await initDb();
const router = Router();
const minChar = 1;
const maxChar = 32;
router.post('/', async (req, res, next) => {
let { username } = res.auth;
let { post, type, id } = req.body;
if (!username || !post || !id || !type) {
apiStat(res, next, 'Fields are missing.')
return;
}
if (post.length < minChar || post.length > maxChar) {
apiStat(res, next, `Post length isn't ${minChar} to ${maxChar} characters.`)
return;
}
let token = randomBytes(12).toString('hex');
await db.run('INSERT INTO comment (username, date, content, id) VALUES (?,?,?,?)', [
username,
(+new Date),
post,
token
]);
await db.run('INSERT INTO feeder (parentType, parentId , childType , childId , sortId ) VALUES(?,?,?,?,?)', [
type,
id,
'comment',
token,
(+new Date)
]);
apiStat(res, next, `Comment submitted.`, '/comment/' + token)
})
export default router;

View file

@ -14,8 +14,9 @@ const aliases = {
const routers = {
'/you': './routes/you.js',
'/api/form/you': './routes/youApi.js',
'/api/file/comment': './routes/commenter.js',
'/walls': './routes/walls.js',
'/comments': './routes/comment.js'
'/comment': './routes/comment.js'
}
function doAliases(app) {

View file

@ -38,18 +38,28 @@ router.get('/get/:type/:id/:num', async (req, res, next) => {
]);
for (let reply of replies) {
if (reply.childType != 'comment') continue;
let dat = await db.all('SELECT * FROM comment WHERE id = ?', [
reply.childId
]);
reply.comment = dat[0];
if (reply.childType != 'comment' || dat.length < 1) {
reply.comment = 'fail';
continue;
}
reply.comment = dat[0];
reply.comment.type = reply.childType;
reply.comment.id = reply.childId;
}
replies = replies.filter(x => x.comment != 'fail');
res.ctx.mainPage = 'replies'
res.ctx.mainCtx = {
page: req.params.num,
replies,
opLink: `/${req.params.type}/${req.params.id}`
opLink: `/${req.params.type}/${req.params.id}`,
type: req.params.type,
id: req.params.id
}
next();
})

View file

@ -1,8 +1,8 @@
<div class='main'>
<div class="header-big">
<div><%= username %> </div>
<div><%= new Date(date) + '' %> </div>
<div><%= new Date(date).toISOString() %> </div>
</div>
<div><%= content %></div>
<%- include ('vblock.ejs', {link: `/walls/get${opLink}/0`, icon: '/icon.svg', name: 'Replies'}) %>
<%- include ('vblock.ejs', {link: `/walls/get/${type}/${id}/0`, icon: '/icon.svg', name: 'Replies'}) %>
</div>

View file

@ -1,3 +1,34 @@
<%- include('form.ejs', {
title: "Reply",
message: 'Send your thoughts to the world.',
action: "/api/file/comment",
inputs: [
{
"key": "Message",
"type": "textarea",
"name": "post",
"default": ""
},
{
"key": "Attachments",
"type": "file",
"name": "post",
"default": ""
},
{
"key": "",
"type": "hidden",
"name": "type",
"default": type
},
{
"key": "",
"type": "hidden",
"name": "id",
"default": id
},
]
}) %>
<div class='main'>
<h1 class="header-big">Replies</h1>
<%- include ('vblock.ejs', {link: opLink, icon: '/icon.svg', name: 'Original'}) %>