reply chain fixes, refactoring
This commit is contained in:
parent
f0782b0472
commit
93027bfd09
4 changed files with 42 additions and 43 deletions
|
@ -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'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue