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.

49 lines
1.8 KiB

5 years ago
'use strict';
const uploadDirectory = require(__dirname+'/../../helpers/uploadDirectory.js')
, remove = require('fs-extra').remove
, Mongo = require(__dirname+'/../../db/db.js')
, Posts = require(__dirname+'/../../db/posts.js');
5 years ago
module.exports = async (req, res, next, posts, board) => {
5 years ago
//filter to threads
const threads = posts.filter(x => x.thread == null);
5 years ago
//delete the html for threads
const deleteHTML = []
for (let i = 0; i < threads.length; i++) {
deleteHTML.push(remove(`${uploadDirectory}html/${threads[i].board}/thread/${threads[i].postId}.html`));
}
await Promise.all(deleteHTML);
//get posts from all threads
let threadPosts = []
if (threads.length > 0) {
if (board) {
//if this is board-specific, we can use a single query
const threadPostIds = threads.map(thread => thread.postId);
threadPosts = await Posts.getMultipleThreadPosts(board, threadPostIds);
} else {
//otherwise we fetch posts from threads on different boards separarely
//TODO: use bulkwrite or construct a large $or query so this can be tackled in a single db query
await Promise.all(threads.map(async thread => {
//for each thread, fetch all posts from the matching board and thread matching the threads postId
const currentThreadPosts = await Posts.getThreadPosts(thread.board, thread.postId);
threadPosts = threadPosts.concat(currentThreadPosts);
}));
}
}
//combine them all into one array, there may be duplicates but it shouldnt matter
const allPosts = posts.concat(threadPosts)
5 years ago
//get all mongoids and delete posts from
const postMongoIds = allPosts.map(post => Mongo.ObjectId(post._id))
const deletedPosts = await Posts.deleteMany(postMongoIds).then(result => result.deletedCount);
5 years ago
//hooray!
return { action: deletedPosts > 0, message:`Deleted ${threads.length} threads and ${deletedPosts-threads.length} posts` };
5 years ago
}