start of json api read only for boards, threads, catalog, banners

merge-requests/208/head
fatchan 5 years ago
parent 5d683590b3
commit 143ea5a0a0
  1. 2
      gulp/res/css/style.css
  2. 29
      helpers/build.js
  3. 9
      helpers/render.js
  4. 1
      models/forms/actionhandler.js
  5. 3
      models/forms/changeboardsettings.js
  6. 3
      models/forms/deleteboard.js
  7. 3
      models/forms/deletepost.js
  8. 2
      models/forms/makepost.js
  9. 7
      views/pages/catalog.pug

@ -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;

@ -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);

@ -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;
};

@ -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({

@ -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) {

@ -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
]);
}

@ -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`)
}));
}

@ -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',

@ -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]

Loading…
Cancel
Save