add pagination to boards

merge-requests/208/head
fatchan 5 years ago
parent 3700769204
commit a5205ac480
  1. 3
      controllers/api.js
  2. 20
      controllers/frontend.js
  3. 6
      db-models/posts.js
  4. 2
      views/mixins/post.pug
  5. 5
      views/pages/board.pug

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

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

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

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

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

Loading…
Cancel
Save