From f92adeb610a0d1074eb3242e6aa65ee98bbf375d Mon Sep 17 00:00:00 2001 From: fatchan Date: Wed, 27 Mar 2019 05:14:42 +0000 Subject: [PATCH] add boards with title, description, etc --- controllers/api.js | 24 +++++++++++++++++------- controllers/frontend.js | 13 ++++++++----- models/boards.js | 41 +++++++++++++++++++++++++++++++++++++++++ models/posts.js | 14 +++----------- views/mixins/post.pug | 5 +++-- views/pages/board.pug | 3 ++- views/pages/thread.pug | 2 +- 7 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 models/boards.js diff --git a/controllers/api.js b/controllers/api.js index f2228c14..b1cbdbcb 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -3,7 +3,8 @@ const express = require('express') , router = express.Router() , utils = require('../utils.js') - , Posts = require(__dirname+'/../models/posts.js'); + , Posts = require(__dirname+'/../models/posts.js') + , Boards = require(__dirname+'/../models/boards.js'); /* roughly: @@ -18,17 +19,17 @@ roughly: */ // make new post -router.post('/api/board/:board', Posts.checkBoard, async (req, res, next) => { +router.post('/api/board/:board', Boards.exists, async (req, res, next) => { }); // delete a post -router.delete('/api/board/:board/post/:id', Posts.checkBoard, async (req, res, next) => { +router.delete('/api/board/:board/post/:id', Boards.exists, async (req, res, next) => { }); // get recent threads and preview posts -router.get('/api/board/:board/recent/:page', Posts.checkBoard, async (req, res, next) => { +router.get('/api/board/:board/recent/:page', Boards.exists, async (req, res, next) => { //get the recently bumped thread & preview posts let threads; @@ -47,7 +48,7 @@ router.get('/api/board/:board/recent/:page', Posts.checkBoard, async (req, res, }); // get a thread -router.get('/api/board/:board/thread/:thread([a-f\d]{24})', Posts.checkBoard, async (req, res, next) => { +router.get('/api/board/:board/thread/:thread([a-f\d]{24})', Boards.exists, async (req, res, next) => { //get the recently bumped thread & preview posts let thread; @@ -66,7 +67,7 @@ router.get('/api/board/:board/thread/:thread([a-f\d]{24})', Posts.checkBoard, as }); // get array of threads (catalog) -router.get('/api/board/:board/catalog', Posts.checkBoard, async (req, res, next) => { +router.get('/api/board/:board/catalog', Boards.exists, async (req, res, next) => { //get the recently bumped thread & preview posts let data; @@ -86,18 +87,27 @@ router.get('/api/board/:board/catalog', Posts.checkBoard, async (req, res, next) /* (async () => { + await Boards.deleteAll(); + await Boards.insertOne({ + _id: 'b', + name: 'random', + title: 'random posts', + description: 'post anything here', + }) await Posts.deleteAll('b'); for (let i = 0; i < 5; i++) { const thread = await Posts.insertOne('b', { 'author': 'Anonymous', + 'title': 'post title', 'date': new Date(), 'content': Math.random().toString(36).replace(/[^a-z]+/g, ''), 'thread': null }) - for (let j = 0; j < 5; j++) { + for (let j = 0; j < 30; j++) { await new Promise(resolve => {setTimeout(resolve, 500)}) const post = await Posts.insertOne('b', { 'author': 'Anonymous', + 'title': 'post title', 'date': new Date(), 'content': Math.random().toString(36).replace(/[^a-z]+/g, ''), 'thread': thread.insertedId diff --git a/controllers/frontend.js b/controllers/frontend.js index e45ac334..b9a1d729 100644 --- a/controllers/frontend.js +++ b/controllers/frontend.js @@ -3,7 +3,8 @@ const express = require('express') , router = express.Router() , utils = require('../utils.js') - , Posts = require(__dirname+'/../models/posts.js'); + , Posts = require(__dirname+'/../models/posts.js') + , Boards = require(__dirname+'/../models/boards.js'); /* roughly: @@ -18,7 +19,7 @@ roughly: */ // board page/recents -router.get('/:board/:page?', Posts.checkBoard, async (req, res, next) => { +router.get('/:board/:page?', Boards.exists, async (req, res, next) => { //get the recently bumped thread & preview posts let threads; @@ -31,14 +32,13 @@ router.get('/:board/:page?', Posts.checkBoard, async (req, res, next) => { //render the page res.render('board', { csrf: req.csrfToken(), - board: req.params.board, threads: threads || [] }); }); // thread view page -router.get('/:board/thread/:thread([a-f\\d]{24})', Posts.checkBoard, async (req, res, next) => { +router.get('/:board/thread/:thread([a-f\\d]{24})', Boards.exists, async (req, res, next) => { //get the recently bumped thread & preview posts let thread; @@ -55,11 +55,14 @@ router.get('/:board/thread/:thread([a-f\\d]{24})', Posts.checkBoard, async (req, //render the page res.render('thread', { csrf: req.csrfToken(), - board: req.params.board, thread: thread }); }); +router.get('/', async (req, res, next) => { + res.redirect('/b'); +}) + module.exports = router; diff --git a/models/boards.js b/models/boards.js new file mode 100644 index 00000000..83bec24c --- /dev/null +++ b/models/boards.js @@ -0,0 +1,41 @@ +'use strict'; + +const Mongo = require(__dirname+'/../helpers/db.js') + , db = Mongo.client.db('boards'); + +module.exports = { + + db, + + findOne: async (name) => { + return db.collection('boards').findOne({ '_id': name }); + }, + + insertOne: async (data) => { + return db.collection('boards').insertOne(data); + }, + + deleteOne: async (board, options) => { + + }, + + deleteMany: async (board, options) => { + + }, + + deleteAll: async (board) => { + return db.collection('boards').deleteMany({}); + }, + + exists: async (req, res, next) => { + + const board = await module.exports.findOne(req.params.board) + if (!board) { + return res.status(404).render('404') + } + res.locals.board = board; + next(); + + }, + +} diff --git a/models/posts.js b/models/posts.js index 532678c7..dc2be17d 100644 --- a/models/posts.js +++ b/models/posts.js @@ -1,10 +1,12 @@ 'use strict'; const Mongo = require(__dirname+'/../helpers/db.js') - , db = Mongo.client.db('chan-boards'); + , db = Mongo.client.db('posts'); module.exports = { + db, + //TODO: IMPLEMENT PAGINATION getRecent: async (board, page) => { @@ -100,14 +102,4 @@ module.exports = { return db.collection(board).deleteMany({}); }, - checkBoard: async (req, res, next) => { - - const boards = await db.listCollections({ 'name': req.params.board }, { 'nameOnly': true }).toArray(); - if (!boards || boards.length == 0) { - return res.status(404).render('404') - } - next(); - - }, - } diff --git a/views/mixins/post.pug b/views/mixins/post.pug index a49170c5..a16904bb 100644 --- a/views/mixins/post.pug +++ b/views/mixins/post.pug @@ -1,9 +1,10 @@ mixin post(board, post) .post-container if post.thread == null - a(href=`/${board}/thread/${post._id}`) #{post._id} + a(href=`/${board._id}/thread/${post._id}`) #{post._id} else - a(href=`/${board}/thread/${post.thread}#${post._id}`) #{post._id} + a(href=`/${board._id}/thread/${post.thread}#${post._id}`) #{post._id} span , #{post.author} span , #{post.date} + p #{post.title} p #{post.content} diff --git a/views/pages/board.pug b/views/pages/board.pug index 1c14166a..80f419bd 100644 --- a/views/pages/board.pug +++ b/views/pages/board.pug @@ -2,9 +2,10 @@ extends ../layout.pug include ../mixins/post.pug block head - title /#{board}/ + title /#{board._id}/ - Recent Posts block content + span #{board.description} hr for thread in threads h1 OP: diff --git a/views/pages/thread.pug b/views/pages/thread.pug index cbdb3b46..8f02f13a 100644 --- a/views/pages/thread.pug +++ b/views/pages/thread.pug @@ -2,7 +2,7 @@ extends ../layout.pug include ../mixins/post.pug block head - title /#{board}/ - title placeholder + title /#{board._id}/ - #{thread.title} block content h1 OP: