reply chain fixes, refactoring
This commit is contained in:
parent
f0782b0472
commit
93027bfd09
4 changed files with 42 additions and 43 deletions
27
lib.js
27
lib.js
|
@ -9,4 +9,29 @@ function apiStat(res, next, message, success = false, redirect = '/') {
|
|||
next();
|
||||
}
|
||||
|
||||
export { importRouters, apiStat };
|
||||
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 };
|
|
@ -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'
|
||||
|
|
|
@ -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' }
|
||||
|
|
|
@ -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');
|
||||
|
|
Loading…
Reference in a new issue