mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 19:46:01 +01:00
refactor: move API banned response handler to separate internal method
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const colors = require('colors/safe');
|
||||
const nconf = require('nconf');
|
||||
const validator = require('validator');
|
||||
const querystring = require('querystring');
|
||||
@@ -361,21 +362,9 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
|
||||
|
||||
// Update status code based on some common error codes
|
||||
switch (payload.message) {
|
||||
case '[[error:user-banned]]': {
|
||||
const [reason, expiry] = await Promise.all([
|
||||
user.bans.getReason(res.req.uid),
|
||||
user.getUserField(res.req.uid, 'banned:expire'),
|
||||
]);
|
||||
|
||||
response.reason = reason;
|
||||
if (expiry) {
|
||||
Object.assign(response, {
|
||||
expiry,
|
||||
expiryISO: new Date(expiry).toISOString(),
|
||||
expiryLocaleString: new Date(expiry).toLocaleString(),
|
||||
});
|
||||
}
|
||||
} // intentional fall through
|
||||
case '[[error:user-banned]]':
|
||||
Object.assign(response, await generateBannedResponse(res));
|
||||
// intentional fall through
|
||||
|
||||
case '[[error:no-privileges]]':
|
||||
statusCode = 403;
|
||||
@@ -391,6 +380,7 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
|
||||
|
||||
if (global.env === 'development') {
|
||||
returnPayload.stack = payload.stack;
|
||||
process.stdout.write(`[${colors.yellow('api')}] Exception caught, error with stack trace follows:\n`);
|
||||
process.stdout.write(payload.stack);
|
||||
}
|
||||
res.status(statusCode).json(returnPayload);
|
||||
@@ -400,6 +390,25 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
|
||||
}
|
||||
};
|
||||
|
||||
async function generateBannedResponse(res) {
|
||||
const response = {};
|
||||
const [reason, expiry] = await Promise.all([
|
||||
user.bans.getReason(res.req.uid),
|
||||
user.getUserField(res.req.uid, 'banned:expire'),
|
||||
]);
|
||||
|
||||
response.reason = reason;
|
||||
if (expiry) {
|
||||
Object.assign(response, {
|
||||
expiry,
|
||||
expiryISO: new Date(expiry).toISOString(),
|
||||
expiryLocaleString: new Date(expiry).toLocaleString(),
|
||||
});
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
helpers.generateError = (statusCode, message) => {
|
||||
var payload = {
|
||||
status: {
|
||||
|
||||
Reference in New Issue
Block a user