jschan - Anonymous imageboard software. Classic look, modern features and feel. Works without JavaScript and supports Tor, I2P, Lokinet, etc.
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.

164 lines
3.9 KiB

6 years ago
'use strict';
const express = require('express')
, router = express.Router()
, utils = require('../utils.js')
, Posts = require(__dirname+'/../models/posts.js')
6 years ago
, Boards = require(__dirname+'/../models/boards.js')
, files = require(__dirname+'/../helpers/file.js')
6 years ago
// make new post
6 years ago
router.post('/board/:board', Boards.exists, async (req, res, next) => {
//ghetto setting to 0 so expres validator doesnt skip null value. needs looking into.
if (req.body.thread) {
let thread;
6 years ago
try {
thread = await Posts.getThread(req.params.board, req.body.thread);
} catch (err) {
return res.status(500).json({ 'message': 'Error fetching from DB' });
}
if (!thread) {
return res.status(400).json({ 'message': 'thread does not exist' })
}
}
6 years ago
try {
await files.uploadAndThumb(req, res);
} catch (err) {
console.error(err);
return res.status(500).json({ 'message': 'Error uploading file' });
}
//add the post
const post = await Posts.insertOne(req.params.board, {
'author': req.body.author || 'Anonymous',
'subject': req.body.subject || '',
'date': new Date(),
'content': req.body.content,
6 years ago
'thread': req.body.thread || null,
'file': req.file ? req.file.filename : ''
})
const redirect = '/' + req.params.board + '/thread/' + (req.body.thread || post.insertedId);
6 years ago
return res.redirect(redirect);
6 years ago
});
// delete a post
router.delete('/board/:board/post/:id(\\d+)', Boards.exists, async (req, res, next) => {
6 years ago
});
// get recent threads and preview posts
router.get('/board/:board/recent/:page(\\d+)?', Boards.exists, 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 res.status(500).json({ 'message': 'Error fetching from DB' });
6 years ago
}
if (!threads || threads.lenth === 0) {
return res.status(404).json({ 'message': 'Not found' });
6 years ago
}
return res.json(threads)
});
// get a thread
router.get('/board/:board/thread/:id(\\d+)', Boards.exists, 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.id);
6 years ago
} catch (err) {
return res.status(500).json({ 'message': 'Error fetching from DB' });
6 years ago
}
if (!thread) {
return res.status(404).json({ 'message': 'Not found' });
6 years ago
}
return res.json(thread)
});
// get array of threads (catalog)
router.get('/board/:board/catalog', Boards.exists, 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 res.status(500).json({ 'message': 'Error fetching from DB' });
6 years ago
}
if (!data) {
return res.status(404).json({ 'message': 'Not found' });
6 years ago
}
return res.json(data)
});
//get list of boards
router.get('/boards', Boards.exists, async (req, res, next) => {
//get a list of boards
let boards;
try {
boards = await Boards.find();
} catch (err) {
return res.status(500).json({ 'message': 'Error fetching from DB' })
}
//render the page
res.json(boards)
});
/*
6 years ago
(async () => {
await Boards.deleteIncrement('b');
await Boards.deleteAll();
await Boards.insertOne({
_id: 'b',
name: 'random',
description: 'post anything here',
})
6 years ago
await Posts.deleteAll('b');
for (let i = 0; i < 3; i++) {
6 years ago
const thread = await Posts.insertOne('b', {
'author': 'Anonymous',
'subject': 'subject',
6 years ago
'date': new Date(),
'content': Math.random().toString(36).replace(/[^a-z]+/g, ''),
'thread': null
})
for (let j = 0; j < 5; j++) {
6 years ago
await new Promise(resolve => {setTimeout(resolve, 500)})
const post = await Posts.insertOne('b', {
'author': 'Anonymous',
'subject': 'subject',
6 years ago
'date': new Date(),
'content': Math.random().toString(36).replace(/[^a-z]+/g, ''),
'thread': thread.insertedId + ''
6 years ago
})
}
}
})();
*/
6 years ago
module.exports = router;