comments and replies work now
This commit is contained in:
parent
11147482d2
commit
9e5989825f
6 changed files with 102 additions and 10 deletions
|
@ -22,20 +22,24 @@ router.get('/:id', async (req, res, next) => {
|
||||||
|
|
||||||
for (let reply of replies) {
|
for (let reply of replies) {
|
||||||
let dat = await db.all('SELECT * FROM comment WHERE id = ?', [
|
let dat = await db.all('SELECT * FROM comment WHERE id = ?', [
|
||||||
reply.parentId
|
reply.childId
|
||||||
]);
|
]);
|
||||||
if (reply.parentType != 'comment' || dat.length < 1) {
|
if (reply.childType != 'comment' || dat.length < 1) {
|
||||||
reply.comment = {username: 'fail', date: 'fail', content: 'fail'};
|
reply.comment = 'fail'
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
reply.comment = dat[0];
|
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.mainPage = 'commenter'
|
||||||
res.ctx.mainCtx = {
|
res.ctx.mainCtx = {
|
||||||
replies,
|
replies
|
||||||
opLink: `/comments/${req.params.id}`
|
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
})
|
})
|
||||||
|
|
46
routes/commenter.js
Normal file
46
routes/commenter.js
Normal 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;
|
|
@ -14,8 +14,9 @@ const aliases = {
|
||||||
const routers = {
|
const routers = {
|
||||||
'/you': './routes/you.js',
|
'/you': './routes/you.js',
|
||||||
'/api/form/you': './routes/youApi.js',
|
'/api/form/you': './routes/youApi.js',
|
||||||
|
'/api/file/comment': './routes/commenter.js',
|
||||||
'/walls': './routes/walls.js',
|
'/walls': './routes/walls.js',
|
||||||
'/comments': './routes/comment.js'
|
'/comment': './routes/comment.js'
|
||||||
}
|
}
|
||||||
|
|
||||||
function doAliases(app) {
|
function doAliases(app) {
|
||||||
|
|
|
@ -38,18 +38,28 @@ router.get('/get/:type/:id/:num', async (req, res, next) => {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
for (let reply of replies) {
|
for (let reply of replies) {
|
||||||
if (reply.childType != 'comment') continue;
|
|
||||||
let dat = await db.all('SELECT * FROM comment WHERE id = ?', [
|
let dat = await db.all('SELECT * FROM comment WHERE id = ?', [
|
||||||
reply.childId
|
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.mainPage = 'replies'
|
||||||
res.ctx.mainCtx = {
|
res.ctx.mainCtx = {
|
||||||
page: req.params.num,
|
page: req.params.num,
|
||||||
replies,
|
replies,
|
||||||
opLink: `/${req.params.type}/${req.params.id}`
|
|
||||||
|
opLink: `/${req.params.type}/${req.params.id}`,
|
||||||
|
type: req.params.type,
|
||||||
|
id: req.params.id
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<div class='main'>
|
<div class='main'>
|
||||||
<div class="header-big">
|
<div class="header-big">
|
||||||
<div><%= username %> </div>
|
<div><%= username %> </div>
|
||||||
<div><%= new Date(date) + '' %> </div>
|
<div><%= new Date(date).toISOString() %> </div>
|
||||||
</div>
|
</div>
|
||||||
<div><%= content %></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>
|
</div>
|
|
@ -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'>
|
<div class='main'>
|
||||||
<h1 class="header-big">Replies</h1>
|
<h1 class="header-big">Replies</h1>
|
||||||
<%- include ('vblock.ejs', {link: opLink, icon: '/icon.svg', name: 'Original'}) %>
|
<%- include ('vblock.ejs', {link: opLink, icon: '/icon.svg', name: 'Original'}) %>
|
||||||
|
|
Loading…
Reference in a new issue