mirror of https://gitgud.io/fatchan/jschan.git
seemsgood, closes #194
parent
fb5adeba06
commit
97ed9a91f0
10 changed files with 225 additions and 5 deletions
@ -0,0 +1,50 @@ |
||||
'use strict'; |
||||
|
||||
const { globalLimits, ipHashPermLevel } = require(__dirname+'/../../configs/main.js') |
||||
, addBan = require(__dirname+'/../../models/forms/addban.js') |
||||
, dynamicResponse = require(__dirname+'/../../helpers/dynamic.js') |
||||
, { isIP } = require('net'); |
||||
|
||||
module.exports = async (req, res, next) => { |
||||
|
||||
const errors = []; |
||||
|
||||
if (!req.body.ip || req.body.ip.length === 0) { |
||||
errors.push('Missing IP/hash input'); |
||||
} else if (req.body.ip.length > 50) { |
||||
errors.push('IP/hash input must be less than 50 characters'); |
||||
} else if (res.locals.permLevel > ipHashPermLevel && (isIP(req.body.ip) || req.body.ip.length !== 10)) { |
||||
errors.push('Invalid hash input'); |
||||
} |
||||
if (req.body.ban_reason && req.body.ban_reason.length > globalLimits.fieldLength.ban_reason) { |
||||
errors.push(`Ban reason must be ${globalLimits.fieldLength.ban_reason} characters or less`); |
||||
} |
||||
if (req.body.log_message && req.body.log_message.length > globalLimits.fieldLength.log_message) { |
||||
errors.push(`Modlog message must be ${globalLimits.fieldLength.log_message} characters or less`); |
||||
} |
||||
|
||||
let redirect = req.headers.referer; |
||||
if (!redirect) { |
||||
if (!req.params.board) { |
||||
redirect = '/globalmanage/bans.html'; |
||||
} else { |
||||
redirect = `/${req.params.board}/manage/bans.html`; |
||||
} |
||||
} |
||||
|
||||
if (errors.length > 0) { |
||||
return dynamicResponse(req, res, 400, 'message', { |
||||
'title': 'Bad request', |
||||
'errors': errors, |
||||
redirect, |
||||
}); |
||||
} |
||||
|
||||
try { |
||||
await addBan(req, res, redirect); |
||||
} catch (err) { |
||||
return next(err); |
||||
} |
||||
|
||||
} |
||||
|
@ -0,0 +1,63 @@ |
||||
'use strict'; |
||||
|
||||
//modlog
|
||||
if (modlogActions.length > 0) { |
||||
const modlog = {}; |
||||
const logDate = new Date(); //all events current date
|
||||
const message = req.body.log_message || null; |
||||
let logUser; |
||||
if (res.locals.permLevel < 4) { //if staff
|
||||
logUser = req.session.user.username; |
||||
} else { |
||||
logUser = 'Unregistered User'; |
||||
} |
||||
for (let i = 0; i < res.locals.posts.length; i++) { |
||||
const post = res.locals.posts[i]; |
||||
if (!modlog[post.board]) { |
||||
//per board actions, all actions combined to one event
|
||||
modlog[post.board] = { |
||||
postIds: [], |
||||
actions: modlogActions, |
||||
date: logDate, |
||||
showUser: !req.body.hide_name || logUser === 'Unregistered User' ? true : false, |
||||
message: message, |
||||
user: logUser, |
||||
ip: { |
||||
single: res.locals.ip.single, |
||||
raw: res.locals.ip.raw |
||||
} |
||||
}; |
||||
} |
||||
//push each post id
|
||||
modlog[post.board].postIds.push(post.postId); |
||||
} |
||||
const modlogDocuments = []; |
||||
for (let i = 0; i < threadBoards.length; i++) { |
||||
const boardName = threadBoards[i]; |
||||
const boardLog = modlog[boardName]; |
||||
//make it into documents for the db
|
||||
modlogDocuments.push({ |
||||
...boardLog, |
||||
'board': boardName |
||||
}); |
||||
} |
||||
if (modlogDocuments.length > 0) { |
||||
//insert the modlog docs
|
||||
await Modlogs.insertMany(modlogDocuments); |
||||
for (let i = 0; i < threadBoards.length; i++) { |
||||
const board = buildBoards[threadBoards[i]]; |
||||
buildQueue.push({ |
||||
'task': 'buildModLog', |
||||
'options': { |
||||
'board': board, |
||||
} |
||||
}); |
||||
buildQueue.push({ |
||||
'task': 'buildModLogList', |
||||
'options': { |
||||
'board': board, |
||||
} |
||||
}); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,69 @@ |
||||
'use strict'; |
||||
|
||||
const { Bans, Modlogs } = require(__dirname+'/../../db/') |
||||
, dynamicResponse = require(__dirname+'/../../helpers/dynamic.js') |
||||
, hashIp = require(__dirname+'/../../helpers/dynamic.js') |
||||
, buildQueue = require(__dirname+'/../../queue.js') |
||||
, { isIP } = require('net') |
||||
, { ipHashPermLevel, defaultBanDuration } = require(__dirname+'/../../configs/main.js'); |
||||
|
||||
module.exports = async (req, res, redirect) => { |
||||
|
||||
const actionDate = new Date(); |
||||
|
||||
const banPromise = Bans.insertOne({ |
||||
//note: raw ip and type single because of
|
||||
'type': 'single', |
||||
'ip': { |
||||
'single': isIP(req.body.ip) ? hashIp(req.body.ip) : req.body.ip, |
||||
'raw': req.body.ip, |
||||
}, |
||||
'reason': req.body.ban_reason || req.body.log_message || 'No reason specified', |
||||
'board': req.params.board || null, |
||||
'posts': null, |
||||
'issuer': req.session.user.username, |
||||
'date': actionDate, |
||||
'expireAt': new Date(actionDate.getTime() + (req.body.ban_duration || defaultBanDuration)), |
||||
'allowAppeal': req.body.no_appeal ? false : true, |
||||
'appeal': null, |
||||
'seen': false, |
||||
}); |
||||
|
||||
const modlogPromise = Modlogs.insertOne({ |
||||
'board': req.params.board || null, |
||||
'postIds': [], |
||||
'actions': [(req.params.board ? 'Ban' : 'Global Ban')], |
||||
'date': actionDate, |
||||
'showUser': !req.body.hide_name || res.locals.permLevel >= 4 ? true : false, |
||||
'message': req.body.log_message || null, |
||||
'user': res.locals.permLevel < 4 ? req.session.user.username : 'Unregistered User', |
||||
'ip': { |
||||
'single': res.locals.ip.single, |
||||
'raw': res.locals.ip.raw |
||||
} |
||||
}); |
||||
|
||||
await Promise.all([banPromise, modlogPromise]); |
||||
|
||||
if (req.params.board) { |
||||
buildQueue.push({ |
||||
'task': 'buildModLog', |
||||
'options': { |
||||
'board': res.locals.board, |
||||
} |
||||
}); |
||||
buildQueue.push({ |
||||
'task': 'buildModLogList', |
||||
'options': { |
||||
'board': res.locals.board, |
||||
} |
||||
}); |
||||
} |
||||
|
||||
return dynamicResponse(req, res, 200, 'message', { |
||||
'title': 'Success', |
||||
'message': 'Added ban', |
||||
redirect, |
||||
}); |
||||
|
||||
} |
@ -0,0 +1,22 @@ |
||||
.row |
||||
.label IP/Hash |
||||
input(type='text' name='ip' required) |
||||
.row |
||||
.label Ban Reason |
||||
input(type='text' name='ban_reason') |
||||
.row |
||||
.label Modlog Message |
||||
input(type='text' name='log_message') |
||||
.row |
||||
.label Ban Duration |
||||
input(type='text' name='ban_duration' placeholder='e.g. 7d') |
||||
.row |
||||
.label Non-appealable Ban |
||||
label.postform-style.ph-5 |
||||
input(type='checkbox', name='no_appeal' value='1') |
||||
.row |
||||
.label Hide Username In Modlog |
||||
label.postform-style.ph-5 |
||||
input(type='checkbox', name='hide_name' value='1') |
||||
input(type='submit', value='submit') |
||||
|
Loading…
Reference in new issue