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.

114 lines
2.2 KiB

6 years ago
'use strict';
const Mongo = require(__dirname+'/../helpers/db.js')
, db = Mongo.client.db('chan-boards');
6 years ago
module.exports = {
6 years ago
//TODO: IMPLEMENT PAGINATION
getRecent: async (board, page) => {
6 years ago
// get all thread posts (posts with null thread id)
const threads = await db.collection(board).find({
6 years ago
'thread': null
}).sort({
'bumped': -1
}).limit(10).toArray();
// add posts to all threads in parallel
await Promise.all(threads.map(async thread => {
const replies = await db.collection(board).find({
6 years ago
'thread': thread._id
}).sort({
'_id': -1
6 years ago
}).limit(3).toArray();
thread.replies = replies.reverse();
6 years ago
}));
return threads;
},
6 years ago
getThread: async (board, id) => {
6 years ago
// get thread post and potential replies concurrently
const data = await Promise.all([
db.collection(board).findOne({
6 years ago
'_id': Mongo.ObjectId(id)
}),
db.collection(board).find({
6 years ago
'thread': Mongo.ObjectId(id)
}).sort({
'_id': 1
}).toArray()
])
// attach the replies to the thread post
const thread = data[0];
if (thread) {
thread.replies = data[1];
}
return thread;
},
6 years ago
getCatalog: async (board) => {
6 years ago
// get all threads for catalog
return db.collection(board).find({
6 years ago
'thread': null
}).toArray();
},
6 years ago
getPost: async (board, id) => {
6 years ago
// get a post
return db.collection(board).findOne({
6 years ago
'_id': Mongo.ObjectId(id)
});
},
6 years ago
insertOne: async (board, data) => {
6 years ago
// bump thread if name not sage
if (data.thread !== null && data.author !== 'sage') {
await db.collection(board).updateOne({
6 years ago
'_id': data.thread
}, {
$set: {
'bumped': Date.now()
}
})
}
return db.collection(board).insertOne(data);
},
6 years ago
deleteOne: async (board, options) => {
return db.collection(board).deleteOne(options);
},
6 years ago
deleteMany: async (board, options) => {
return db.collection(board).deleteMany(options);
},
6 years ago
deleteAll: async (board) => {
return db.collection(board).deleteMany({});
},
6 years ago
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();
6 years ago
},
6 years ago
}