From 8c858ba018948ab46c24d3795fac5ecb4f9350da Mon Sep 17 00:00:00 2001 From: fatchan Date: Wed, 27 Mar 2019 04:29:17 +0000 Subject: [PATCH] view improvements and enforce URL format --- controllers/api.js | 2 +- controllers/frontend.js | 9 ++++++--- views/mixins/post.pug | 9 +++++++++ views/pages/board.pug | 13 ++++--------- views/pages/thread.pug | 15 ++++----------- 5 files changed, 24 insertions(+), 24 deletions(-) create mode 100644 views/mixins/post.pug diff --git a/controllers/api.js b/controllers/api.js index 69f45807..f2228c14 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -47,7 +47,7 @@ router.get('/api/board/:board/recent/:page', Posts.checkBoard, async (req, res, }); // get a thread -router.get('/api/board/:board/thread/:thread', Posts.checkBoard, async (req, res, next) => { +router.get('/api/board/:board/thread/:thread([a-f\d]{24})', Posts.checkBoard, async (req, res, next) => { //get the recently bumped thread & preview posts let thread; diff --git a/controllers/frontend.js b/controllers/frontend.js index 536c549a..e45ac334 100644 --- a/controllers/frontend.js +++ b/controllers/frontend.js @@ -38,7 +38,7 @@ router.get('/:board/:page?', Posts.checkBoard, async (req, res, next) => { }); // thread view page -router.get('/:board/thread/:thread', Posts.checkBoard, async (req, res, next) => { +router.get('/:board/thread/:thread([a-f\\d]{24})', Posts.checkBoard, async (req, res, next) => { //get the recently bumped thread & preview posts let thread; @@ -48,15 +48,18 @@ router.get('/:board/thread/:thread', Posts.checkBoard, async (req, res, next) => return next(err); } + if (!thread) { + return res.status(404).render('404'); + } + //render the page res.render('thread', { csrf: req.csrfToken(), board: req.params.board, - threads: [thread] || [] + thread: thread }); }); - module.exports = router; diff --git a/views/mixins/post.pug b/views/mixins/post.pug new file mode 100644 index 00000000..a49170c5 --- /dev/null +++ b/views/mixins/post.pug @@ -0,0 +1,9 @@ +mixin post(board, post) + .post-container + if post.thread == null + a(href=`/${board}/thread/${post._id}`) #{post._id} + else + a(href=`/${board}/thread/${post.thread}#${post._id}`) #{post._id} + span , #{post.author} + span , #{post.date} + p #{post.content} diff --git a/views/pages/board.pug b/views/pages/board.pug index 7ea04e02..1c14166a 100644 --- a/views/pages/board.pug +++ b/views/pages/board.pug @@ -1,20 +1,15 @@ extends ../layout.pug +include ../mixins/post.pug block head title /#{board}/ block content + hr for thread in threads h1 OP: - div #{thread._id} - div #{thread.author} - div #{thread.date} - div #{thread.content} + +post(board, thread) h1 Replies: for post in thread.replies - div #{post._id} - div #{post.author} - div #{post.date} - div #{post.content} - br + +post(board, post) hr diff --git a/views/pages/thread.pug b/views/pages/thread.pug index 7ea04e02..cbdb3b46 100644 --- a/views/pages/thread.pug +++ b/views/pages/thread.pug @@ -1,20 +1,13 @@ extends ../layout.pug +include ../mixins/post.pug block head - title /#{board}/ + title /#{board}/ - title placeholder block content - for thread in threads h1 OP: - div #{thread._id} - div #{thread.author} - div #{thread.date} - div #{thread.content} + +post(board, thread) h1 Replies: for post in thread.replies - div #{post._id} - div #{post.author} - div #{post.date} - div #{post.content} - br + +post(board, post) hr