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.childId ]); 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 } next(); }) export default router;