mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
2.4 KiB
112 lines
2.4 KiB
6 years ago
|
'use strict';
|
||
|
|
||
|
const express = require('express')
|
||
|
, router = express.Router()
|
||
|
, utils = require('../utils.js')
|
||
|
, Posts = require(__dirname+'/../models/posts.js');
|
||
|
|
||
|
/*
|
||
|
roughly:
|
||
|
- GET /api/board/:board/catalog -> all threads (catalog)
|
||
|
- GET /api/board/:board/recent/:page? -> recent posts per page (board homepage)
|
||
|
- GET /api/board/:board/thread/:thread -> get all posts in a thread
|
||
|
|
||
|
- POST /api/board/:board -> make a new thread
|
||
|
- POST /api/board/:board/thread/:thread -> make a new post in a thread
|
||
|
|
||
|
- DELETE /api/board/:board/post/:id -> delete a post
|
||
|
*/
|
||
|
|
||
|
// make new post
|
||
6 years ago
|
router.post('/api/board/:board', Posts.checkBoard, async (req, res, next) => {
|
||
6 years ago
|
|
||
|
});
|
||
|
|
||
|
// delete a post
|
||
6 years ago
|
router.delete('/api/board/:board/post/:id', Posts.checkBoard, async (req, res, next) => {
|
||
6 years ago
|
|
||
|
});
|
||
|
|
||
|
// get recent threads and preview posts
|
||
6 years ago
|
router.get('/api/board/:board/recent/:page', Posts.checkBoard, async (req, res, next) => {
|
||
6 years ago
|
|
||
|
//get the recently bumped thread & preview posts
|
||
|
let threads;
|
||
|
try {
|
||
|
threads = await Posts.getRecent(req.params.board, req.params.page || 1);
|
||
|
} catch (err) {
|
||
|
return next(err);
|
||
|
}
|
||
|
|
||
|
if (!threads || threads.lenth === 0) {
|
||
|
return next();
|
||
|
}
|
||
|
|
||
|
return res.json(threads)
|
||
|
|
||
|
});
|
||
|
|
||
|
// get a thread
|
||
6 years ago
|
router.get('/api/board/:board/thread/:thread', Posts.checkBoard, async (req, res, next) => {
|
||
6 years ago
|
|
||
|
//get the recently bumped thread & preview posts
|
||
|
let thread;
|
||
|
try {
|
||
|
thread = await Posts.getThread(req.params.board, req.params.thread);
|
||
|
} catch (err) {
|
||
|
return next(err);
|
||
|
}
|
||
|
|
||
|
if (!thread) {
|
||
|
return next();
|
||
|
}
|
||
|
|
||
|
return res.json(thread)
|
||
|
|
||
|
});
|
||
|
|
||
|
// get array of threads (catalog)
|
||
6 years ago
|
router.get('/api/board/:board/catalog', Posts.checkBoard, async (req, res, next) => {
|
||
6 years ago
|
|
||
|
//get the recently bumped thread & preview posts
|
||
|
let data;
|
||
|
try {
|
||
|
data = await Posts.getCatalog(req.params.board);
|
||
|
} catch (err) {
|
||
|
return next(err);
|
||
|
}
|
||
|
|
||
|
if (!data) {
|
||
|
return next();
|
||
|
}
|
||
|
|
||
|
return res.json(data)
|
||
|
|
||
|
});
|
||
|
|
||
|
/*
|
||
|
(async () => {
|
||
|
await Posts.deleteAll('b');
|
||
|
for (let i = 0; i < 5; i++) {
|
||
|
const thread = await Posts.insertOne('b', {
|
||
|
'author': 'Anonymous',
|
||
|
'date': new Date(),
|
||
|
'content': Math.random().toString(36).replace(/[^a-z]+/g, ''),
|
||
|
'thread': null
|
||
|
})
|
||
|
for (let j = 0; j < 5; j++) {
|
||
|
await new Promise(resolve => {setTimeout(resolve, 500)})
|
||
|
const post = await Posts.insertOne('b', {
|
||
|
'author': 'Anonymous',
|
||
|
'date': new Date(),
|
||
|
'content': Math.random().toString(36).replace(/[^a-z]+/g, ''),
|
||
|
'thread': thread.insertedId
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
})();
|
||
|
*/
|
||
|
|
||
|
module.exports = router;
|
||
|
|