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.
44 lines
1.6 KiB
44 lines
1.6 KiB
'use strict';
|
|
|
|
const transferBoard = require(__dirname+'/../../models/forms/transferboard.js')
|
|
, { Accounts } = require(__dirname+'/../../db/')
|
|
, dynamicResponse = require(__dirname+'/../../lib/misc/dynamic.js')
|
|
, paramConverter = require(__dirname+'/../../lib/middleware/input/paramconverter.js')
|
|
, { alphaNumericRegex, checkSchema, lengthBody, existsBody } = require(__dirname+'/../../lib/input/schema.js');
|
|
|
|
module.exports = {
|
|
|
|
paramConverter: paramConverter({
|
|
trimFields: ['username'],
|
|
}),
|
|
|
|
controller: async (req, res, next) => {
|
|
|
|
const errors = await checkSchema([
|
|
{ result: existsBody(req.body.username), expected: true, error: 'Missing new owner username' },
|
|
{ result: lengthBody(req.body.username, 1, 50), expected: false, error: 'New owner username must be 50 characters or less' },
|
|
{ result: (req.body.username === res.locals.board.owner), expected: false, error: 'New owner must be different from current owner' },
|
|
{ result: alphaNumericRegex.test(req.body.username), expected: true, error: 'New owner username must contain a-z 0-9 only' },
|
|
{ result: async () => {
|
|
res.locals.newOwner = await Accounts.findOne(req.body.username.toLowerCase());
|
|
return res.locals.newOwner != null;
|
|
}, expected: true, error: 'Cannot transfer to account that does not exist' },
|
|
]);
|
|
|
|
if (errors.length > 0) {
|
|
return dynamicResponse(req, res, 400, 'message', {
|
|
'title': 'Bad request',
|
|
'errors': errors,
|
|
'redirect': `/${req.params.board}/manage/settings.html`
|
|
});
|
|
}
|
|
|
|
try {
|
|
await transferBoard(req, res, next);
|
|
} catch (err) {
|
|
return next(err);
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|