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.
78 lines
3.4 KiB
78 lines
3.4 KiB
'use strict';
|
|
|
|
const Boards = require(__dirname+'/../../db/boards.js')
|
|
, Posts = require(__dirname+'/../../db/posts.js')
|
|
, uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js')
|
|
, { buildHomepage, buildCatalog, buildBoardMultiple } = require(__dirname+'/../../helpers/build.js')
|
|
, { remove } = require('fs-extra')
|
|
, deletePosts = require(__dirname+'/deletepost.js')
|
|
|
|
module.exports = async (req, res, next) => {
|
|
|
|
const oldSettings = res.locals.board.settings;
|
|
|
|
const newSettings = {
|
|
name: req.body.name && req.body.name.trim().length > 0 ? req.body.name : oldSettings.name,
|
|
description: req.body.description && req.body.description.trim().length > 0 ? req.body.description : oldSettings.description,
|
|
ids: req.body.ids ? true : false,
|
|
captcha: req.body.captcha ? true : false,
|
|
forceAnon: req.body.force_anon ? true : false,
|
|
userPostDelete: req.body.user_post_delete ? true : false,
|
|
userPostSpoiler: req.body.user_post_spoiler ? true : false,
|
|
userPostUnlink: req.body.user_post_unlink ? true : false,
|
|
threadLimit: typeof req.body.thread_limit === 'number' && req.body.thread_limit !== oldSettings.threadLimit ? req.body.thread_limit : oldSettings.threadLimit,
|
|
replyLimit: typeof req.body.reply_limit === 'number' && req.body.reply_limit !== oldSettings.replyLimit ? req.body.reply_limit : oldSettings.replyLimit,
|
|
maxFiles: typeof req.body.max_files === 'number' && req.body.max_files !== oldSettings.maxFiles ? req.body.max_files : oldSettings.maxFiles,
|
|
minMessageLength: typeof req.body.min_message_length === 'number' && req.body.min_message_length !== oldSettings.maxFiles ? req.body.min_message_length : oldSettings.minMessageLength,
|
|
forceOPSubject: req.body.force_op_subject ? true : false,
|
|
forceOPMessage: req.body.force_op_message ? true : false,
|
|
forceOPFile: req.body.force_op_file ? true : false,
|
|
defaultName: req.body.default_name && req.body.default_name.trim().length > 0 ? req.body.default_name : oldSettings.defaultName,
|
|
};
|
|
|
|
//settings changed in the db
|
|
await Boards.db.updateOne({ _id: req.params.board }, {
|
|
'$set': {
|
|
'settings': newSettings
|
|
}
|
|
});
|
|
|
|
//update this in locals incase is used in later parts
|
|
res.locals.board.settings = newSettings;
|
|
|
|
//array of promises we might need
|
|
const promises = [];
|
|
|
|
//do rebuilding and pruning if max number of pages is changed and any threads are pruned
|
|
const oldMaxPage = Math.ceil(oldSettings.threadLimit/10);
|
|
const newMaxPage = Math.ceil(newSettings.threadLimit/10);
|
|
if (newMaxPage < oldMaxPage) {
|
|
//prune old threads
|
|
const prunedThreads = await Posts.pruneOldThreads(req.params.board, res.locals.board.settings.threadLimit);
|
|
if (prunedThreads.length > 0) {
|
|
await deletePosts(prunedThreads, req.params.board);
|
|
//remove board page html for pages > newMaxPage
|
|
for (let i = newMaxPage+1; i <= oldMaxPage; i++) {
|
|
promises.push(remove(`${uploadDirectory}html/${req.params.board}/${i}.html`));
|
|
}
|
|
//rebuild valid board pages for page numbers, and catalog for prunedthreads
|
|
promises.push(buildBoardMultiple(res.locals.board, 1, newMaxPage));
|
|
promises.push(buildCatalog(res.locals.board));
|
|
}
|
|
}
|
|
|
|
if (oldSettings.captcha !== newSettings.captcha) {
|
|
promises.push(remove(`${uploadDirectory}html/${req.params.board}/`));
|
|
}
|
|
|
|
if (promises.length > 0) {
|
|
await Promise.all(promises);
|
|
}
|
|
|
|
return res.render('message', {
|
|
'title': 'Success',
|
|
'message': 'Updated settings.',
|
|
'redirect': `/${req.params.board}/manage.html`
|
|
});
|
|
|
|
}
|
|
|