add boards with title, description, etc

merge-requests/208/head
fatchan 5 years ago
parent 8c858ba018
commit f92adeb610
  1. 24
      controllers/api.js
  2. 13
      controllers/frontend.js
  3. 41
      models/boards.js
  4. 14
      models/posts.js
  5. 5
      views/mixins/post.pug
  6. 3
      views/pages/board.pug
  7. 2
      views/pages/thread.pug

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

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

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

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

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

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

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

Loading…
Cancel
Save