From 0a0c715df4114fe3f519bcafe63f963533149196 Mon Sep 17 00:00:00 2001 From: fatchan Date: Sat, 15 Jun 2019 11:59:13 +0000 Subject: [PATCH] fix builds for boards with more than 10 pages -- not a hardcoded maximum anymore + html removing, prune threads and rebuilding when number of max pages changes --- build.js | 18 +++++------ models/forms/changeboardsettings.js | 47 +++++++++++++++++++++++++---- models/forms/makepost.js | 2 +- models/pages/board.js | 2 +- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/build.js b/build.js index e9849d15..6e28e6f1 100644 --- a/build.js +++ b/build.js @@ -5,7 +5,6 @@ const Posts = require(__dirname+'/db/posts.js') , uploadDirectory = require(__dirname+'/helpers/uploadDirectory.js') , render = require(__dirname+'/helpers/render.js'); - function addBacklinks(thread, preview) { //preview means this is not the full thread const postMap = new Map() postMap.set(thread.postId, thread) @@ -38,7 +37,7 @@ function addBacklinks(thread, preview) { //preview means this is not the full th module.exports = { buildCatalog: async (board) => { -//console.log('building catalog', `${board._id}/catalog.html`); +console.log('building catalog', `${board._id}/catalog.html`); if (!board._id) { board = await Boards.findOne(board); } @@ -50,7 +49,7 @@ module.exports = { }, buildThread: async (threadId, board) => { -//console.log('building thread', `${board._id || board}/thread/${threadId}.html`); +console.log('building thread', `${board._id || board}/thread/${threadId}.html`); if (!board._id) { board = await Boards.findOne(board); } @@ -68,10 +67,10 @@ module.exports = { }, buildBoard: async (board, page, maxPage=null) => { -//console.log('building board page', `${board._id}/${page === 1 ? 'index' : page}.html`); +console.log('building board page', `${board._id}/${page === 1 ? 'index' : page}.html`); const threads = await Posts.getRecent(board._id, page); if (!maxPage) { - maxPage = Math.ceil((await Posts.getPages(board._id)) / 10); + maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10)); } for (let k = 0; k < threads.length; k++) { @@ -88,8 +87,8 @@ module.exports = { }, //building multiple pages (for rebuilds) - buildBoardMultiple: async (board, startpage=1, endpage=10) => { - const maxPage = Math.ceil((await Posts.getPages(board._id)) / 10); + buildBoardMultiple: async (board, startpage=1, endpage) => { + const maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10)); if (endpage === 0) { //deleted only/all posts, so only 1 page will remain endpage = 1; @@ -98,8 +97,7 @@ module.exports = { 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 threads = await Posts.getRecent(board._id, startpage, difference*Math.ceil(board.settings.threadLimit/10)); for (let k = 0; k < threads.length; k++) { const thread = threads[k]; addBacklinks(thread, true); @@ -107,7 +105,7 @@ module.exports = { const buildArray = []; for (let i = startpage; i <= endpage; i++) { -//console.log('multi building board page', `${board._id}/${i === 1 ? 'index' : i}.html`); +console.log('multi building board page', `${board._id}/${i === 1 ? 'index' : i}.html`); let spliceStart = (i-1)*10; if (spliceStart > 0) { spliceStart = spliceStart - 1; diff --git a/models/forms/changeboardsettings.js b/models/forms/changeboardsettings.js index 796c24c8..3553b3bf 100644 --- a/models/forms/changeboardsettings.js +++ b/models/forms/changeboardsettings.js @@ -1,6 +1,10 @@ 'use strict'; const Boards = require(__dirname+'/../../db/boards.js') + , Posts = require(__dirname+'/../../db/posts.js') + , uploadDirectory = require(__dirname+'/../../helpers/uploadDirectory.js') + , { buildCatalog, buildBoardMultiple } = require(__dirname+'/../../build.js') + , remove = require('fs-extra').remove module.exports = async (req, res, next) => { @@ -30,12 +34,43 @@ module.exports = async (req, res, next) => { } }); - //should i rebuild any pages here since the post form might change? probably not. at most board pages. + //update this in locals incase is used in later parts + res.locals.board.settings = newSettings; - return res.render('message', { - 'title': 'Success', - 'message': 'Updated settings.', - 'redirect': `/${req.params.board}/manage.html` - }); + //do rebuilding and pruning if max number of pages is changed and any threads are pruned + const oldMaxPage = Math.ceil(oldSettings.threadLimit/10); + const newMaxPage = Math.ceil(newSettings.threadLimit/10); + if (newMaxPage < oldMaxPage) { + //prune old threads + const prunedThreads = await Posts.pruneOldThreads(req.params.board, res.locals.board.settings.threadLimit); + if (prunedThreads.length > 0) { + const promises = []; + //remove pruned threads html also + for (let i = 0; i < prunedThreads.length; i++) { + promises.push(remove(`${uploadDirectory}html/${req.params.board}/thread/${prunedThreads[i]}.html`)); + } + //remove board page html for pages > newMaxPage + for (let i = newMaxPage+1; i <= oldMaxPage; i++) { + promises.push(remove(`${uploadDirectory}html/${req.params.board}/${i}.html`)); + } + //rebuild valid board pages for page numbers to be <= newMaxPage + promises.push(buildBoardMultiple(res.locals.board, 1, newMaxPage)); + //rebuild catalog since some threads were pruned + promises.push(buildCatalog(res.locals.board)); + await Promise.all(promises); + } + } + + /* + TODO: delete all board html when breaking change is made (captcha is enabled, specifically) + not a complete rebuild because we could not rebuild the page for every thread. + just leave them to build on load. maybe rebuild index pages only as a smart compromise + */ + + return res.render('message', { + 'title': 'Success', + 'message': 'Updated settings.', + 'redirect': `/${req.params.board}/manage.html` + }); } diff --git a/models/forms/makepost.js b/models/forms/makepost.js index 293e40f5..d6108a22 100644 --- a/models/forms/makepost.js +++ b/models/forms/makepost.js @@ -291,7 +291,7 @@ module.exports = async (req, res, next) => { 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)); + parallelPromises.push(buildBoardMultiple(res.locals.board, 1, Math.ceil(res.locals.board.settings.threadLimit/10))); } //always rebuild catalog for post counts and ordering diff --git a/models/pages/board.js b/models/pages/board.js index e2fa88d9..000a2d45 100644 --- a/models/pages/board.js +++ b/models/pages/board.js @@ -8,7 +8,7 @@ module.exports = async (req, res, next) => { const page = req.params.page === 'index' ? 1 : req.params.page; try { - const maxPage = Math.ceil((await Posts.getPages(req.params.board)) / 10); + const maxPage = Math.min(Math.ceil((await Posts.getPages(req.params.board)) / 10), Math.ceil(res.locals.board.settings.threadLimit/10)); if (page > maxPage && maxPage > 0) { return next(); }