From 11147482d28d752466523d3fd844df23d9df8258 Mon Sep 17 00:00:00 2001 From: biglyderv Date: Wed, 26 Feb 2025 01:10:09 -0500 Subject: [PATCH] prototype of comments --- index.js | 2 +- public/index.css | 4 +-- routes/comment.js | 43 +++++++++++++++++++++++++++ routes/hub.js | 72 +++++++++++++++++++++++++++++++++++++++++++++ routes/init.js | 6 ++-- routes/walls.js | 57 +++++++++++++++++++++++++++++++++++ views/comment.ejs | 8 +++++ views/commenter.ejs | 3 ++ views/replies.ejs | 9 ++++++ views/wall.ejs | 9 ++++++ views/walls.ejs | 8 +++++ 11 files changed, 216 insertions(+), 5 deletions(-) create mode 100644 routes/comment.js create mode 100644 routes/hub.js create mode 100644 routes/walls.js create mode 100644 views/comment.ejs create mode 100644 views/commenter.ejs create mode 100644 views/replies.ejs create mode 100644 views/wall.ejs create mode 100644 views/walls.ejs diff --git a/index.js b/index.js index 1159b1a..062f74c 100644 --- a/index.js +++ b/index.js @@ -14,4 +14,4 @@ app.listen(port, () => { doInit(app); -// TODO: rate limit stuff \ No newline at end of file +// TODO: rate limit stuff and fix long urls \ No newline at end of file diff --git a/public/index.css b/public/index.css index 1a09355..d48d55e 100644 --- a/public/index.css +++ b/public/index.css @@ -1,5 +1,5 @@ :root { - font-family: sans-serif; + font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; font-size: 1em; --main-1: rgb(13, 12, 17); @@ -124,7 +124,7 @@ body { color: inherit; font-weight: bold; margin: 5px; - display: flex; + display: inline-flex; flex-direction: column; align-items: center; justify-content: center; diff --git a/routes/comment.js b/routes/comment.js new file mode 100644 index 0000000..fd4652a --- /dev/null +++ b/routes/comment.js @@ -0,0 +1,43 @@ +import { Router } from "express"; +import { initDb } from "../db.js"; +const router = Router(); +let db = await initDb(); + +//todo: fix jank + +router.get('/:id', async (req, res, next) => { + + let replies = [{parentType: 'comment', parentId: req.params.id}]; + let feeder = req.params.id; + + while (true) { + let tmpReplies = await db.all('SELECT * FROM feeder WHERE childType = ? AND childId = ?', [ + 'comment', + feeder, + ]); + if (tmpReplies.length == 0) break; + feeder = tmpReplies[0].parentId + replies.splice(0,0,tmpReplies[0]) + } + + for (let reply of replies) { + let dat = await db.all('SELECT * FROM comment WHERE id = ?', [ + reply.parentId + ]); + if (reply.parentType != 'comment' || dat.length < 1) { + reply.comment = {username: 'fail', date: 'fail', content: 'fail'}; + continue; + } + reply.comment = dat[0]; + } + + + res.ctx.mainPage = 'commenter' + res.ctx.mainCtx = { + replies, + opLink: `/comments/${req.params.id}` + } + next(); +}) + +export default router; \ No newline at end of file diff --git a/routes/hub.js b/routes/hub.js new file mode 100644 index 0000000..bac2f60 --- /dev/null +++ b/routes/hub.js @@ -0,0 +1,72 @@ +import { Router } from "express"; +const router = Router(); + +//todo: fix jank + +router.get('/login', (req, res, next) => { + res.ctx.mainPage = 'hub' + res.ctx.mainCtx = { + title: "Log in", + message: 'Log into an existing account.', + action: "/api/form/you/login", + inputs: [ + { + "key": "Username", + "type": "text", + "name": "user", + "default": "" + }, + { + "key": "Password", + "type": "password", + "name": "pass", + "default": "" + } + ] + } + next(); +}) + +router.get('/logout', (req, res, next) => { + res.ctx.mainPage = 'form' + res.ctx.mainCtx = { + title: "Log out", + message: 'Are you sure?', + action: "/api/form/you/logout", + inputs: [] + } + next(); +}) + + +router.get('/new', (req, res, next) => { + res.ctx.mainPage = 'form' + res.ctx.mainCtx = { + title: "Register", + message: 'Create an account and join the community!', + action: "/api/form/you/new", + inputs: [ + { + "key": "Username", + "type": "text", + "name": "user", + "default": "" + }, + { + "key": "Password", + "type": "password", + "name": "pass", + "default": "" + }, + { + "key": "Password (again)", + "type": "password", + "name": "pass2", + "default": "" + } + ] + } + next(); +}) + +export default router; \ No newline at end of file diff --git a/routes/init.js b/routes/init.js index 708c995..9980bdf 100644 --- a/routes/init.js +++ b/routes/init.js @@ -8,12 +8,14 @@ let db = await initDb(); const upload = multer({ dest: 'uploads/' }); const aliases = { - '/': '/walls/get/home' + '/': '/walls/get/hub/main/0' }; const routers = { '/you': './routes/you.js', - '/api/form/you': './routes/youApi.js' + '/api/form/you': './routes/youApi.js', + '/walls': './routes/walls.js', + '/comments': './routes/comment.js' } function doAliases(app) { diff --git a/routes/walls.js b/routes/walls.js new file mode 100644 index 0000000..c49ffc0 --- /dev/null +++ b/routes/walls.js @@ -0,0 +1,57 @@ +import { Router } from "express"; +import { initDb } from "../db.js"; +const router = Router(); +let db = await initDb(); + +//todo: fix jank + +router.get('/list', (req, res, next) => { + res.ctx.mainPage = 'walls' + res.ctx.mainCtx = { + walls: [ + { + 'alias': 'hub/main', + 'name': 'Home', + 'desc': 'Discuss anything you desire.' + }, + { + 'alias': `users/${res.auth.username}`, + 'name': 'You', + 'desc': 'Connect with your fanbase.' + } + ] + } + next(); +}) + +router.get('/get/:type/:id/:num', async (req, res, next) => { + if (isNaN(req.params.num * 1)) { + next(); + return; + } + + let replies = await db.all('SELECT * FROM feeder WHERE parentType = ? AND parentId = ? ORDER BY sortId DESC LIMIT ? OFFSET ?', [ + req.params.type, + req.params.id, + 10, + req.params.num * 10 + ]); + + 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]; + } + + res.ctx.mainPage = 'replies' + res.ctx.mainCtx = { + page: req.params.num, + replies, + opLink: `/${req.params.type}/${req.params.id}` + } + next(); +}) + +export default router; \ No newline at end of file diff --git a/views/comment.ejs b/views/comment.ejs new file mode 100644 index 0000000..daf01d4 --- /dev/null +++ b/views/comment.ejs @@ -0,0 +1,8 @@ +
+
+
<%= username %>
+
<%= new Date(date) + '' %>
+
+
<%= content %>
+ <%- include ('vblock.ejs', {link: `/walls/get${opLink}/0`, icon: '/icon.svg', name: 'Replies'}) %> +
\ No newline at end of file diff --git a/views/commenter.ejs b/views/commenter.ejs new file mode 100644 index 0000000..2ec5118 --- /dev/null +++ b/views/commenter.ejs @@ -0,0 +1,3 @@ +<% for (let reply of replies) { %> +<%- include ('comment.ejs', reply.comment || {}) %> +<% } %> \ No newline at end of file diff --git a/views/replies.ejs b/views/replies.ejs new file mode 100644 index 0000000..b0398e4 --- /dev/null +++ b/views/replies.ejs @@ -0,0 +1,9 @@ +
+

Replies

+ <%- include ('vblock.ejs', {link: opLink, icon: '/icon.svg', name: 'Original'}) %> + <%- include ('vblock.ejs', {link: `${page - 1}`, icon: '/icon.svg', name: 'Previous'}) %> + <%- include ('vblock.ejs', {link: `${page * 1 + 1}`, icon: '/icon.svg', name: 'Next'}) %> +
+<% for (let reply of replies) { %> +<%- include ('comment.ejs', reply.comment || {}) %> +<% } %> \ No newline at end of file diff --git a/views/wall.ejs b/views/wall.ejs new file mode 100644 index 0000000..70d3e1f --- /dev/null +++ b/views/wall.ejs @@ -0,0 +1,9 @@ +
+ +
+ <%= name %> +

+ <%= desc %> +

+
+
\ No newline at end of file diff --git a/views/walls.ejs b/views/walls.ejs new file mode 100644 index 0000000..c55bbce --- /dev/null +++ b/views/walls.ejs @@ -0,0 +1,8 @@ +
+

My Walls

+
+ <% for (let wall of walls) { %> + <%- include ('wall.ejs', wall) %> + <% } %> +
+
\ No newline at end of file