derv-net/routes/comment.js

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2025-02-26 01:10:09 -05:00
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 = ?', [
2025-02-26 01:40:27 -05:00
reply.childId
2025-02-26 01:10:09 -05:00
]);
2025-02-26 01:40:27 -05:00
if (reply.childType != 'comment' || dat.length < 1) {
reply.comment = 'fail'
2025-02-26 01:10:09 -05:00
continue;
}
reply.comment = dat[0];
2025-02-26 01:40:27 -05:00
reply.comment.id = reply.childId;
reply.comment.type = reply.childType;
2025-02-26 01:10:09 -05:00
}
2025-02-26 01:40:27 -05:00
replies = replies.filter(x => x.comment != 'fail');
console.log(replies)
2025-02-26 01:10:09 -05:00
res.ctx.mainPage = 'commenter'
res.ctx.mainCtx = {
2025-02-26 01:40:27 -05:00
replies
2025-02-26 01:10:09 -05:00
}
next();
})
export default router;