diff --git a/routes/comment.js b/routes/comment.js index fd4652a..0222798 100644 --- a/routes/comment.js +++ b/routes/comment.js @@ -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(); }) diff --git a/routes/commenter.js b/routes/commenter.js new file mode 100644 index 0000000..7c41546 --- /dev/null +++ b/routes/commenter.js @@ -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; \ No newline at end of file diff --git a/routes/init.js b/routes/init.js index 9980bdf..5aff352 100644 --- a/routes/init.js +++ b/routes/init.js @@ -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) { diff --git a/routes/walls.js b/routes/walls.js index c49ffc0..70d070d 100644 --- a/routes/walls.js +++ b/routes/walls.js @@ -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 ]); + 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(); }) diff --git a/views/comment.ejs b/views/comment.ejs index daf01d4..c1bb3f2 100644 --- a/views/comment.ejs +++ b/views/comment.ejs @@ -1,8 +1,8 @@
<%= username %>
-
<%= new Date(date) + '' %>
+
<%= new Date(date).toISOString() %>
<%= content %>
- <%- 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'}) %>
\ No newline at end of file diff --git a/views/replies.ejs b/views/replies.ejs index b0398e4..de7e874 100644 --- a/views/replies.ejs +++ b/views/replies.ejs @@ -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 + }, + ] +}) %>

Replies

<%- include ('vblock.ejs', {link: opLink, icon: '/icon.svg', name: 'Original'}) %>