From 93027bfd09845e527dae13b4f21c89b667836696 Mon Sep 17 00:00:00 2001 From: biglyderv Date: Wed, 26 Feb 2025 19:50:37 -0500 Subject: [PATCH] reply chain fixes, refactoring --- lib.js | 27 ++++++++++++++++++++++++++- routes/comment.js | 30 ++++++++++++------------------ routes/init.js | 4 ++-- routes/walls.js | 24 ++---------------------- 4 files changed, 42 insertions(+), 43 deletions(-) diff --git a/lib.js b/lib.js index c477aef..55156e4 100644 --- a/lib.js +++ b/lib.js @@ -9,4 +9,29 @@ function apiStat(res, next, message, success = false, redirect = '/') { next(); } -export { importRouters, apiStat }; \ No newline at end of file +async function replyIterator(replies, db) { + for (let reply of replies) { + if (reply.childType == 'hub') { + reply.comment = { type: reply.childType, id: reply.childId, content: `The ${reply.childId} hub.`, username: false, date: undefined }; + continue; + } else if (reply.childType == 'users') { + reply.comment = { type: reply.childType, id: reply.childId, content: `User ${reply.childId}'s wall.`, username: false, date: undefined }; + 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; + } + return replies; +} + +export { importRouters, apiStat, replyIterator }; \ No newline at end of file diff --git a/routes/comment.js b/routes/comment.js index 77f885e..4d02065 100644 --- a/routes/comment.js +++ b/routes/comment.js @@ -1,5 +1,6 @@ import { Router } from "express"; import { initDb } from "../db.js"; +import { replyIterator } from "../lib.js"; const router = Router(); let db = await initDb(); @@ -7,33 +8,26 @@ let db = await initDb(); router.get('/:id', async (req, res, next) => { - let replies = [{parentType: 'comment', parentId: req.params.id}]; + let replies = []; let feeder = req.params.id; + let feederType = 'comment'; while (true) { let tmpReplies = await db.all('SELECT * FROM feeder WHERE childType = ? AND childId = ?', [ - 'comment', + feederType, feeder, ]); - if (tmpReplies.length == 0) break; - feeder = tmpReplies[0].parentId + console.log(replies) + if (tmpReplies.length == 0) { + replies.splice(0,0,{childType: feederType, childId: feeder}) + break; + } + feeder = tmpReplies[0].parentId; + feederType = tmpReplies[0].parentType; replies.splice(0,0,tmpReplies[0]) } - for (let reply of replies) { - 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.idOriginal = reply.comment.id; - reply.comment.id = reply.childId; - reply.comment.type = reply.childType; - } - + replies = await replyIterator(replies,db); replies = replies.filter(x => x.comment != 'fail'); res.ctx.mainPage = 'commenter' diff --git a/routes/init.js b/routes/init.js index 003940e..74e3498 100644 --- a/routes/init.js +++ b/routes/init.js @@ -63,14 +63,14 @@ async function auther(req, res, next) { function initr(req, res, next) { let headerCtx = [ - { link: '/walls/get/home', icon: '/icon.svg', name: 'DervNet' }, + { link: '/walls/get/home/0', icon: '/icon.svg', name: 'DervNet' }, { link: '/walls/list', icon: '/walls.svg', name: 'Explore' }, { link: '/you/logout', icon: '/logout.svg', name: 'Leave' } // fix icon ]; if (!res.auth || !res.auth.valid) { headerCtx = [ - { link: '/walls/get/home', icon: '/icon.svg', name: 'DervNet' }, + { link: '/walls/get/home/0', icon: '/icon.svg', name: 'DervNet' }, { link: '/walls/list', icon: '/walls.svg', name: 'Explore' }, { link: '/you/login', icon: '/login.svg', name: 'Log in' }, { link: '/you/new', icon: '/join.svg', name: 'Join' } diff --git a/routes/walls.js b/routes/walls.js index 9324f1c..c1f2281 100644 --- a/routes/walls.js +++ b/routes/walls.js @@ -1,5 +1,6 @@ import { Router } from "express"; import { initDb } from "../db.js"; +import { replyIterator } from "../lib.js"; const router = Router(); let db = await initDb(); @@ -42,28 +43,7 @@ router.get('/get/:type/:id/:num', async (req, res, next) => { childId: req.params.id }) - //TODO: make iterator - for (let reply of replies) { - if (reply.childType == 'hub') { - reply.comment = { type: reply.childType, id: reply.childId, content: `The ${reply.childId} hub.`, username: false, date: undefined }; - continue; - } else if (reply.childType == 'users') { - reply.comment = { type: reply.childType, id: reply.childId, content: `User ${reply.childId}'s wall.`, username: false, date: undefined }; - 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 = await replyIterator(replies,db); let comment = replies.splice(0, 1)[0]; replies = replies.filter(x => x.comment != 'fail');