mirror of https://gitgud.io/fatchan/jschan.git
ability to resign from a board you mod/own. previously you could not leave a mod position, and you could not leave owner position (unless you transfer) closes #196
parent
5aba64f743
commit
b597ac8159
7 changed files with 106 additions and 3 deletions
@ -0,0 +1,44 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const { Boards } = require(__dirname+'/../../db/') |
||||||
|
, resignFromBoard = require(__dirname+'/../../models/forms/resign.js') |
||||||
|
, dynamicResponse = require(__dirname+'/../../helpers/dynamic.js') |
||||||
|
, alphaNumericRegex = require(__dirname+'/../../helpers/checks/alphanumregex.js'); |
||||||
|
|
||||||
|
module.exports = async (req, res, next) => { |
||||||
|
|
||||||
|
const errors = []; |
||||||
|
|
||||||
|
if (!req.body.confirm) { |
||||||
|
errors.push('Missing confirmation'); |
||||||
|
} |
||||||
|
if (!req.body.board || req.body.board.length === 0) { |
||||||
|
errors.push('You did not select a board'); |
||||||
|
} else if (alphaNumericRegex.test(req.body.board) !== true) { |
||||||
|
errors.push('URI must contain a-z 0-9 only'); |
||||||
|
} else { |
||||||
|
try { |
||||||
|
res.locals.board = await Boards.findOne(req.body.board); |
||||||
|
} catch (err) { |
||||||
|
return next(err); |
||||||
|
} |
||||||
|
if (!res.locals.board) { |
||||||
|
errors.push(`Board /${req.body.board}/ does not exist`); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if (errors.length > 0) { |
||||||
|
return dynamicResponse(req, res, 400, 'message', { |
||||||
|
'title': 'Bad request', |
||||||
|
'errors': errors, |
||||||
|
'redirect': `/account.html` |
||||||
|
}) |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
await resignFromBoard(req, res, next); |
||||||
|
} catch (err) { |
||||||
|
return next(err); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,36 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const { Boards, Accounts } = require(__dirname+'/../../db/') |
||||||
|
, dynamicResponse = require(__dirname+'/../../helpers/dynamic.js'); |
||||||
|
|
||||||
|
module.exports = async (req, res, next) => { |
||||||
|
|
||||||
|
const moderatesBoard = res.locals.user.modBoards.includes(req.body.board); |
||||||
|
const ownsBoard = res.locals.user.ownedBoards.includes(req.body.board); |
||||||
|
if (!ownsBoard && !moderatesBoard) { |
||||||
|
return dynamicResponse(req, res, 400, 'message', { |
||||||
|
'title': 'Bad request', |
||||||
|
'message': 'You do not own or moderate that board', |
||||||
|
'redirect': `/account.html` |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
if (ownsBoard) { |
||||||
|
await Promise.all([ |
||||||
|
Accounts.removeOwnedBoard(res.locals.user.username, req.body.board), |
||||||
|
Boards.setOwner(req.body.board, null), |
||||||
|
]); |
||||||
|
} else if (moderatesBoard) { |
||||||
|
await Promise.all([ |
||||||
|
Boards.removeModerator(req.body.board, res.locals.user.username), |
||||||
|
Accounts.removeModBoard([res.locals.user.username], req.body.board), |
||||||
|
]); |
||||||
|
} |
||||||
|
|
||||||
|
return dynamicResponse(req, res, 200, 'message', { |
||||||
|
'title': 'Success', |
||||||
|
'message': `Resigned from ${ownsBoard ? 'owner' : 'moderator'} position on /${req.body.board}/`, |
||||||
|
'redirect': `/account.html` |
||||||
|
}); |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue