From 143ea5a0a05e234723d7235c474cbcb168df4b4b Mon Sep 17 00:00:00 2001 From: fatchan Date: Mon, 2 Sep 2019 11:04:47 +0000 Subject: [PATCH] start of json api read only for boards, threads, catalog, banners --- gulp/res/css/style.css | 2 +- helpers/build.js | 29 ++++++++++++++++++++++------- helpers/render.js | 9 +++++++-- models/forms/actionhandler.js | 1 + models/forms/changeboardsettings.js | 3 ++- models/forms/deleteboard.js | 3 ++- models/forms/deletepost.js | 3 ++- models/forms/makepost.js | 2 +- views/pages/catalog.pug | 7 ++++--- 9 files changed, 42 insertions(+), 17 deletions(-) diff --git a/gulp/res/css/style.css b/gulp/res/css/style.css index 405872a3..2fb25429 100644 --- a/gulp/res/css/style.css +++ b/gulp/res/css/style.css @@ -49,7 +49,7 @@ pre { .navbar { background: var(--post-color); - border-bottom: 1px solid var(--box-border-color); + border-bottom: 1px solid var(--post-outline-color); position: fixed; width: 100%; z-index: 1; diff --git a/helpers/build.js b/helpers/build.js index 6e2750f3..5cc5d144 100644 --- a/helpers/build.js +++ b/helpers/build.js @@ -11,7 +11,10 @@ module.exports = { buildBanners: async (options) => { const label = `/${options.board._id}/banners.html`; const start = process.hrtime(); - const html = render(label, 'banners.pug', options); + const html = render(label, 'banners.pug', options, { + 'name': `/${options.board._id}/banners.json`, + 'data': options.board.banners + }); const end = process.hrtime(start); console.log(timeDiffString(label, end)); return html; @@ -27,7 +30,10 @@ module.exports = { const html = render(label, 'catalog.pug', { ...options, threads, - }); + }, { + 'name': `/${options.board._id}/catalog.json`, + 'data': threads + }); const end = process.hrtime(start); console.log(timeDiffString(label, end)); return html; @@ -46,7 +52,10 @@ module.exports = { const html = render(label, 'thread.pug', { ...options, thread, - }); + }, { + 'name': `/${options.board._id}/thread/${options.threadId}.json`, + 'data': thread + }); const end = process.hrtime(start); console.log(timeDiffString(label, end)); return html; @@ -57,12 +66,14 @@ module.exports = { const start = process.hrtime(); const threads = await Posts.getRecent(options.board._id, options.page); if (!options.maxPage) { - options.maxPage = Math.min(Math.ceil((await Posts.getPages(board._id)) / 10), Math.ceil(board.settings.threadLimit/10)); + options.maxPage = Math.min(Math.ceil((await Posts.getPages(options.board._id)) / 10), Math.ceil(options.board.settings.threadLimit/10)); } - const html = render(label, 'board.pug', { ...options, threads, + }, { + 'name': `/${options.board._id}/${options.page === 1 ? 'index' : options.page}.json`, + 'data': threads }); const end = process.hrtime(start); console.log(timeDiffString(label, end)); @@ -89,13 +100,17 @@ module.exports = { if (spliceStart > 0) { spliceStart = spliceStart - 1; } + const pageThreads = threads.splice(0,10); buildArray.push( render(`${options.board._id}/${i === 1 ? 'index' : i}.html`, 'board.pug', { board: options.board, - threads: threads.splice(0,10), + threads: pageThreads, maxPage, page: i, - }) + }, { + 'name': `/${options.board._id}/${i === 1 ? 'index' : i}.json`, + 'data': pageThreads + }) ); } await Promise.all(buildArray); diff --git a/helpers/render.js b/helpers/render.js index 2e84332f..a872f58f 100644 --- a/helpers/render.js +++ b/helpers/render.js @@ -8,10 +8,15 @@ const { defaultTheme, cacheTemplates, meta }= require(__dirname+'/../configs/mai , redlock = require(__dirname+'/../redlock.js') , templateDirectory = path.join(__dirname+'/../views/pages/') -module.exports = async (htmlName, templateName, options) => { +module.exports = async (htmlName, templateName, options, json=null) => { const html = pug.renderFile(`${templateDirectory}${templateName}`, { ...options, cache: cacheTemplates, meta, defaultTheme }); const lock = await redlock.lock(`locks:${htmlName}`, 3000); //what is a reasonable ttl? - await outputFile(`${uploadDirectory}html/${htmlName}`, html); + const htmlPromise = outputFile(`${uploadDirectory}html/${htmlName}`, html); + let jsonPromise; + if (json !== null) { + jsonPromise = outputFile(`${uploadDirectory}json/${json.name}`, JSON.stringify(json.data)); + } + await Promise.all([htmlPromise, jsonPromise]); await lock.unlock(); return html; }; diff --git a/models/forms/actionhandler.js b/models/forms/actionhandler.js index 054e9b98..8b9c6f82 100644 --- a/models/forms/actionhandler.js +++ b/models/forms/actionhandler.js @@ -343,6 +343,7 @@ module.exports = async (req, res, next) => { for (let k = beforePages[boardName]; k > afterPages; k--) { //deleting html for pages that no longer should exist parallelPromises.push(remove(`${uploadDirectory}html/${boardName}/${k}.html`)); + parallelPromises.push(remove(`${uploadDirectory}json/${boardName}/${k}.json`)); } } buildQueue.push({ diff --git a/models/forms/changeboardsettings.js b/models/forms/changeboardsettings.js index bbcc9a92..5fe5d040 100644 --- a/models/forms/changeboardsettings.js +++ b/models/forms/changeboardsettings.js @@ -117,9 +117,10 @@ module.exports = async (req, res, next) => { const prunedThreads = await Posts.pruneThreads(res.locals.board); if (prunedThreads.length > 0) { await deletePosts(prunedThreads, req.params.board); - //remove board page html for pages > newMaxPage + //remove board page html/json for pages > newMaxPage for (let i = newMaxPage+1; i <= oldMaxPage; i++) { promises.push(remove(`${uploadDirectory}html/${req.params.board}/${i}.html`)); + promises.push(remove(`${uploadDirectory}json/${req.params.board}/${i}.json`)); } //rebuild all board pages for page nav numbers, and catalog if (!captchaEnabled) { diff --git a/models/forms/deleteboard.js b/models/forms/deleteboard.js index a87196af..a1eb4da9 100644 --- a/models/forms/deleteboard.js +++ b/models/forms/deleteboard.js @@ -18,7 +18,8 @@ module.exports = async (uri) => { await Promise.all([ Modlogs.deleteBoard(uri), //bans for the board Bans.deleteBoard(uri), //bans for the board - remove(`${uploadDirectory}html/${uri}/`) //html + remove(`${uploadDirectory}html/${uri}/`), //html + remove(`${uploadDirectory}json/${uri}/`) //json ]); } diff --git a/models/forms/deletepost.js b/models/forms/deletepost.js index d0a49cbf..40f1d2b9 100644 --- a/models/forms/deletepost.js +++ b/models/forms/deletepost.js @@ -21,9 +21,10 @@ module.exports = async (posts, board, all=false) => { const threads = posts.filter(x => x.thread == null); if (threads.length > 0) { - //delete the html for threads + //delete the html/json for threads await Promise.all(threads.map(thread => { remove(`${uploadDirectory}html/${thread.board}/thread/${thread.postId}.html`) + remove(`${uploadDirectory}json/${thread.board}/thread/${thread.postId}.json`) })); } diff --git a/models/forms/makepost.js b/models/forms/makepost.js index 2776458d..8eb06981 100644 --- a/models/forms/makepost.js +++ b/models/forms/makepost.js @@ -407,7 +407,7 @@ module.exports = async (req, res, next) => { if (enableCaptcha) { if (res.locals.board.settings.captchaMode == 2) { //only delete threads if all posts require threads, otherwise just build board pages for thread captcha - await remove(`${uploadDirectory}html/${req.params.board}/thread/`); + await remove(`${uploadDirectory}html/${req.params.board}/thread/`); //not deleting json cos it doesnt need to be } buildQueue.push({ 'task': 'buildBoardMultiple', diff --git a/views/pages/catalog.pug b/views/pages/catalog.pug index 5b624674..85197e04 100644 --- a/views/pages/catalog.pug +++ b/views/pages/catalog.pug @@ -19,9 +19,10 @@ block content hr(size=1) if threads.length === 0 p No posts. - section.catalog - for thread, i in threads - +catalogtile(board, thread, i+1) + else + section.catalog + for thread, i in threads + +catalogtile(board, thread, i+1) hr(size=1) nav.pages a(href=`/${board._id}/index.html`) [Index]