diff --git a/build.js b/build.js index 12e6ab27..303dc3c5 100644 --- a/build.js +++ b/build.js @@ -16,7 +16,7 @@ module.exports = { }, buildThread: async (threadId, board) => { -//console.log('building thread', `${board._id}/thread/${threadId}.html`); +//console.log('building thread', `${board._id || board}/thread/${threadId}.html`); if (!board._id) { board = await Boards.findOne(board); } @@ -47,7 +47,13 @@ module.exports = { //building multiple pages (for rebuilds) buildBoardMultiple: async (board, startpage=1, endpage=10) => { const maxPage = Math.ceil((await Posts.getPages(board._id)) / 10); - endpage = maxPage < endpage ? maxPage : endpage; + if (endpage === 0) { + //deleted only/all posts, so only 1 page will remain + endpage = 1; + } else if (maxPage < endpage) { + //else just build up to the max page if it is greater than input page number + endpage = maxPage + } const difference = endpage-startpage + 1; //+1 because for single pagemust be > 0 const threads = await Posts.getRecent(board._id, startpage, difference*10); const buildArray = []; diff --git a/models/forms/make-post.js b/models/forms/make-post.js index aa0a1f58..e578d6e4 100644 --- a/models/forms/make-post.js +++ b/models/forms/make-post.js @@ -234,14 +234,15 @@ module.exports = async (req, res, next, numFiles) => { } const postId = await Posts.insertOne(req.params.board, data, thread); + const successRedirect = `/${req.params.board}/thread/${req.body.thread || postId}.html#${postId}`; + + //build just the thread they need to see first and send them immediately + await buildThread(data.thread || postId, res.locals.board); + res.redirect(successRedirect); - //now we need to rebuild pages + //now rebuild other pages const parallelPromises = [] - //always need to rebuild catalog - parallelPromises.push(buildCatalog(res.locals.board)); if (data.thread) { - //new reply, so build the thread first - parallelPromises.push(buildThread(thread.postId, res.locals.board)); //refersh pages const threadPage = await Posts.getThreadPage(req.params.board, thread); if (data.email === 'sage') { @@ -252,17 +253,18 @@ module.exports = async (req, res, next, numFiles) => { parallelPromises.push(buildBoardMultiple(res.locals.board, 1, threadPage)); } } else { - //new thread, rebuild all pages and prune old threads + //new thread, rebuild all pages and prunes old threads const prunedThreads = await Posts.pruneOldThreads(req.params.board, res.locals.board.settings.threadLimit); for (let i = 0; i < prunedThreads.length; i++) { parallelPromises.push(remove(`${uploadDirectory}html/${req.params.board}/thread/${prunedThreads[i]}.html`)); } parallelPromises.push(buildBoardMultiple(res.locals.board, 1, 10)); } - await Promise.all(parallelPromises); - const successRedirect = `/${req.params.board}/thread/${req.body.thread || postId}.html#${postId}`; + //always rebuild catalog for post counts and ordering + parallelPromises.push(buildCatalog(res.locals.board)); - return res.redirect(successRedirect); + //finish building other pages + await Promise.all(parallelPromises); }