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
3.0 KiB
112 lines
3.0 KiB
'use strict';
|
|
|
|
const { Bans } = require(__dirname+'/../../db/')
|
|
, config = require(__dirname+'/../../config.js');
|
|
|
|
module.exports = async (req, res, next) => {
|
|
|
|
const { defaultBanDuration } = config.get;
|
|
const banDate = new Date();
|
|
const banExpiry = new Date(banDate.getTime() + (req.body.ban_duration || defaultBanDuration)); //uses config default if missing or malformed
|
|
const banReason = req.body.ban_reason || req.body.log_message || 'No reason specified';
|
|
const allowAppeal = (req.body.no_appeal || req.body.ban_q || req.body.ban_h) ? false : true; //dont allow appeals for range bans
|
|
|
|
const bans = [];
|
|
|
|
if (req.body.ban || req.body.global_ban) {
|
|
const banBoard = req.body.global_ban ? null : req.params.board;
|
|
const ipPosts = res.locals.posts.reduce((acc, post) => {
|
|
if (!acc[post.ip.single]) {
|
|
acc[post.ip.single] = [];
|
|
}
|
|
acc[post.ip.single].push(post);
|
|
return acc;
|
|
}, {});
|
|
for (let ip in ipPosts) {
|
|
const thisIpPosts = ipPosts[ip];
|
|
let type = 'single';
|
|
let banIp = {
|
|
single: ip,
|
|
raw: thisIpPosts[0].ip.raw
|
|
};
|
|
if (req.body.ban_h) {
|
|
type = 'half';
|
|
banIp.single = thisIpPosts[0].ip.hrange;
|
|
} else if (req.body.ban_q) {
|
|
type = 'quarter';
|
|
banIp.single = thisIpPosts[0].ip.qrange;
|
|
}
|
|
bans.push({
|
|
type,
|
|
'ip': banIp,
|
|
'reason': banReason,
|
|
'board': banBoard,
|
|
'posts': req.body.preserve_post ? thisIpPosts : null,
|
|
'issuer': req.session.user,
|
|
'date': banDate,
|
|
'expireAt': banExpiry,
|
|
allowAppeal,
|
|
'appeal': null,
|
|
'showUser': !req.body.hide_name,
|
|
'seen': false,
|
|
});
|
|
}
|
|
}
|
|
|
|
if (req.body.report_ban || req.body.global_report_ban){
|
|
const banBoard = req.body.global_report_ban ? null : req.params.board;
|
|
res.locals.posts.forEach(post => {
|
|
let ips = [];
|
|
if (req.body.report_ban) {
|
|
const matches = post.reports.map(r => {
|
|
if (req.body.checkedreports.includes(r.id.toString())) {
|
|
return r.ip;
|
|
}
|
|
});
|
|
ips = ips.concat(matches);
|
|
}
|
|
if (req.body.global_report_ban) {
|
|
const matches = post.globalreports.map(r => {
|
|
if (req.body.checkedreports.includes(r.id.toString())) {
|
|
return r.ip;
|
|
}
|
|
});
|
|
ips = ips.concat(matches);
|
|
}
|
|
ips = ips.filter(n => n);
|
|
[...new Set(ips)].forEach(ip => {
|
|
bans.push({
|
|
'ip': ip,
|
|
'type': 'single',
|
|
'reason': banReason,
|
|
'board': banBoard,
|
|
'posts': null,
|
|
'issuer': req.session.user,
|
|
'date': banDate,
|
|
'expireAt': banExpiry,
|
|
allowAppeal,
|
|
'appeal': null,
|
|
'showUser': !req.body.hide_name,
|
|
'seen': false
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
const numBans = await Bans.insertMany(bans).then(result => result.insertedCount);
|
|
|
|
const query = {
|
|
message: `Added ${numBans} bans`,
|
|
};
|
|
|
|
if ((req.body.ban || req.body.global_ban ) && req.body.ban_reason) {
|
|
res.locals.actions.numBuild++;//there was a ban reason, so we may need to rebuild some pages, since banning is usually not a building action
|
|
query['action'] = '$set';
|
|
query['query'] = {
|
|
'banmessage': req.body.ban_reason
|
|
};
|
|
}
|
|
|
|
return query;
|
|
|
|
}
|
|
|