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 ]); replies.splice(0, 0, { childType: req.params.type, 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; } let comment = replies.splice(0, 1)[0]; 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}`, type: req.params.type, id: req.params.id, comment } next(); }) export default router;