diff --git a/controllers/api.js b/controllers/api.js index 772bf166..08f470da 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -12,6 +12,7 @@ const express = require('express') , getCatalog = require(__dirname+'/../models/api/get-catalog.js') , getBoards = require(__dirname+'/../models/api/get-boards.js'); +/* (async () => { await Boards.deleteIncrement('pol'); await Boards.deleteIncrement('b'); @@ -30,7 +31,7 @@ const express = require('express') description: 'post anything here', }) })(); - +*/ // make new post router.post('/board/:board', Boards.exists, (req, res, next) => { diff --git a/controllers/frontend.js b/controllers/frontend.js index 93b4527b..64765f29 100644 --- a/controllers/frontend.js +++ b/controllers/frontend.js @@ -8,7 +8,25 @@ const express = require('express') , thread = require(__dirname+'/../models/frontend/thread.js'); // board page/recents -router.get('/:board/:page(\\d+)?', Boards.exists, board); +router.get('/:board/:page(\\d+)?', Boards.exists, (req, res, next) => { + + const errors = []; + + if (req.params.page && req.params.page <= 0) { + errors.push('Invalid page.'); + } + + if (errors.length > 0) { + return res.status(400).render('message', { + 'title': 'Bad request', + 'errors': errors, + 'redirect': `/${req.params.board}` + }); + } + + board(req, res); + +}); // thread view page router.get('/:board/thread/:id(\\d+)', Boards.exists, thread); diff --git a/db-models/posts.js b/db-models/posts.js index 661b3bdd..3adbfaeb 100644 --- a/db-models/posts.js +++ b/db-models/posts.js @@ -16,7 +16,7 @@ module.exports = { 'thread': null }).sort({ 'bumped': -1 - }).limit(10).toArray(); + }).skip(10*(page-1)).limit(10).toArray(); // add posts to all threads in parallel await Promise.all(threads.map(async thread => { @@ -34,6 +34,10 @@ module.exports = { }, + getPages: async (board) => { + return db.collection(board).estimatedDocumentCount(); + }, + getThread: async (board, id) => { // get thread post and potential replies concurrently diff --git a/views/mixins/post.pug b/views/mixins/post.pug index 64125cf3..68722ebc 100644 --- a/views/mixins/post.pug +++ b/views/mixins/post.pug @@ -1,5 +1,5 @@ mixin post(board, post, truncate) - article(class='post-container '+(post.thread ? '' : 'op')) + article(id=post._id class='post-container '+(post.thread ? '' : 'op')) header.post-info input.post-check(type='checkbox', name='checked[]' value=post._id) if post.subject diff --git a/views/pages/board.pug b/views/pages/board.pug index 1d98f6ba..3c954f41 100644 --- a/views/pages/board.pug +++ b/views/pages/board.pug @@ -20,6 +20,11 @@ block content for post in thread.replies +post(board, post, true) hr(size=1) + if pages > 0 + span.pages Page + - for(let i = 0; i < pages; i++) + span: a(href=`/${board._id}/${i+1}`) #{i+1} + hr(size=1) section.delete-wrapper input#password(type='password', name='password', placeholder='password (for deletion)' autocomplete='off') input(type='submit', value='delete')