mirror of https://gitgud.io/fatchan/jschan.git
reference #55 working on global settings page/config editor, start with global filters and options to force change sitewide captcha
parent
05667c52a4
commit
a5aaaa29a5
13 changed files with 220 additions and 23 deletions
@ -0,0 +1,43 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const changeGlobalSettings = require(__dirname+'/../../models/forms/changeglobalsettings.js') |
||||||
|
|
||||||
|
module.exports = async (req, res, next) => { |
||||||
|
|
||||||
|
const errors = []; |
||||||
|
|
||||||
|
if (req.body.filters && req.body.filters.length > 2000) { |
||||||
|
errors.push('Filters length must be less than 2000 characters'); |
||||||
|
} |
||||||
|
if (typeof req.body.captcha_mode === 'number' && (req.body.captcha_mode < 0 || req.body.captcha_mode > 2)) { |
||||||
|
errors.push('Invalid captcha mode.'); |
||||||
|
} |
||||||
|
if (typeof req.body.filter_mode === 'number' && (req.body.filter_mode < 0 || req.body.filter_mode > 2)) { |
||||||
|
errors.push('Invalid filter mode.'); |
||||||
|
} |
||||||
|
if (typeof req.body.ban_duration === 'number' && req.body.ban_duration <= 0) { |
||||||
|
errors.push('Invalid filter auto ban duration.'); |
||||||
|
} |
||||||
|
|
||||||
|
if (errors.length > 0) { |
||||||
|
return res.status(400).render('message', { |
||||||
|
'title': 'Bad request', |
||||||
|
'errors': errors, |
||||||
|
'redirect': '/globalmanage/settings.html' |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
//todo: implement this
|
||||||
|
return res.status(400).render('message', { |
||||||
|
'title': 'Bad request', |
||||||
|
'error': 'Not implemented', |
||||||
|
'redirect': '/globalmanage/settings.html' |
||||||
|
}); |
||||||
|
|
||||||
|
try { |
||||||
|
await changeGlobalSettings(req, res, next); |
||||||
|
} catch (err) { |
||||||
|
return next(err); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,74 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const { Boards, Posts, Accounts } = require(__dirname+'/../../db/') |
||||||
|
, uploadDirectory = require(__dirname+'/../../helpers/files/uploadDirectory.js') |
||||||
|
, buildQueue = require(__dirname+'/../../queue.js') |
||||||
|
, cache = require(__dirname+'/../../redis.js') |
||||||
|
, { remove } = require('fs-extra'); |
||||||
|
|
||||||
|
module.exports = async (req, res, next) => { |
||||||
|
|
||||||
|
const promises = []; |
||||||
|
const oldSettings = await cache.get('globalsettings'); |
||||||
|
const newSettings = { |
||||||
|
'captchaMode': typeof req.body.captcha_mode === 'number' && req.body.captcha_mode !== oldSettings.captchaMode ? req.body.captcha_mode : oldSettings.captchaMode, |
||||||
|
'filters': req.body.filters !== null ? req.body.filters.split('\r\n').filter(n => n).slice(0,50) : oldSettings.filters, |
||||||
|
'filterMode': typeof req.body.filter_mode === 'number' && req.body.filter_mode !== oldSettings.filterMode ? req.body.filter_mode : oldSettings.filterMode, |
||||||
|
'filterBanDuration': typeof req.body.ban_duration === 'number' && req.body.ban_duration !== oldSettings.filterBanDuration ? req.body.ban_duration : oldSettings.filterBanDuration, |
||||||
|
}; |
||||||
|
|
||||||
|
cache.set('globalsettings', newSettings); |
||||||
|
|
||||||
|
let rebuildThreads = false |
||||||
|
, rebuildBoard = false |
||||||
|
, rebuildCatalog = false; |
||||||
|
|
||||||
|
if (newSettings.captchaMode > oldSettings.captchaMode) { |
||||||
|
rebuildBoard = true; |
||||||
|
rebuildCatalog = true; |
||||||
|
if (newSettings.captchaMode == 2) { |
||||||
|
rebuildThreads = true; //thread captcha enabled, removes threads
|
||||||
|
} |
||||||
|
//todo: implement removing pages/rebuilding for all affected boards
|
||||||
|
//i.e. query for ones with settings.catchaMode < newSettings.captchaMode
|
||||||
|
/* |
||||||
|
const affectedBoards = //////
|
||||||
|
for (let i = 0; i < affectedBoards.length; i++) { |
||||||
|
const board = affectedBoards[i]; |
||||||
|
if (rebuildThreads) { |
||||||
|
promises.push(remove(`${uploadDirectory}/html/${board._id}/thread/`)); |
||||||
|
} |
||||||
|
if (rebuildBoard) { |
||||||
|
buildQueue.push({ |
||||||
|
'task': 'buildBoardMultiple', |
||||||
|
'options': { |
||||||
|
board, |
||||||
|
'startpage': 1, |
||||||
|
'endpage': null //no endpage will use whatver maxpage of board is
|
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
if (rebuildCatalog) { |
||||||
|
buildQueue.push({ |
||||||
|
'task': 'buildCatalog', |
||||||
|
'options': { |
||||||
|
board, |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
*/ |
||||||
|
} |
||||||
|
|
||||||
|
//finish the promises in parallel e.g. removing files
|
||||||
|
if (promises.length > 0) { |
||||||
|
await Promise.all(promises); |
||||||
|
} |
||||||
|
|
||||||
|
return res.render('message', { |
||||||
|
'title': 'Success', |
||||||
|
'message': 'Updated settings.', |
||||||
|
'redirect': '/globalmanage/settings.html' |
||||||
|
}); |
||||||
|
|
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const cache = require(__dirname+'/../../../redis.js'); |
||||||
|
|
||||||
|
module.exports = async (req, res, next) => { |
||||||
|
|
||||||
|
let settings = await cache.get('globalsettings'); |
||||||
|
if (!settings) { |
||||||
|
settings = { |
||||||
|
captchaMode: 0, |
||||||
|
filters: [], |
||||||
|
filterMode: 0, |
||||||
|
filterBanDuration: 0, |
||||||
|
} |
||||||
|
cache.set('globalsettings', settings); |
||||||
|
} |
||||||
|
|
||||||
|
res.render('globalmanagesettings', { |
||||||
|
csrf: req.csrfToken(), |
||||||
|
settings, |
||||||
|
}); |
||||||
|
|
||||||
|
} |
@ -0,0 +1,35 @@ |
|||||||
|
extends ../layout.pug |
||||||
|
include ../mixins/globalmanagenav.pug |
||||||
|
|
||||||
|
block head |
||||||
|
script(src='/js/all.js') |
||||||
|
title Manage |
||||||
|
|
||||||
|
block content |
||||||
|
h1.board-title Global Management |
||||||
|
br |
||||||
|
+globalmanagenav('settings') |
||||||
|
hr(size=1) |
||||||
|
h4.no-m-p Settings: |
||||||
|
.form-wrapper.flexleft |
||||||
|
form.form-post(action=`/forms/global/settings`, enctype='application/x-www-form-urlencoded', method='POST') |
||||||
|
input(type='hidden' name='_csrf' value=csrf) |
||||||
|
.row |
||||||
|
.label Captcha Mode |
||||||
|
select(name='captcha_mode') |
||||||
|
option(value='0', selected=settings.captchaMode === 0) No Captcha |
||||||
|
option(value='1', selected=settings.captchaMode === 1) Captcha for new thread |
||||||
|
option(value='2', selected=settings.captchaMode === 2) Captcha for all posts |
||||||
|
.row |
||||||
|
.label Filters |
||||||
|
textarea(name='filters' placeholder='newline separated, max 50') #{settings.filters.join('\n')} |
||||||
|
.row |
||||||
|
.label Filter Mode |
||||||
|
select(name='filter_mode') |
||||||
|
option(value='0', selected=settings.filterMode === 0) Do nothing |
||||||
|
option(value='1', selected=settings.filterMode === 1) Block post |
||||||
|
option(value='2', selected=settings.filterMode === 2) Ban |
||||||
|
.row |
||||||
|
.label Filter Auto Ban Duration |
||||||
|
input(type='text' name='ban_duration' placeholder='e.g. 1w' value=settings.filterBanDuration) |
||||||
|
input(type='submit', value='save settings') |
Loading…
Reference in new issue