Skip to content

Commit

Permalink
Added batch unsuspend moderation route
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Jul 29, 2023
1 parent bb38b87 commit 6186a18
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/routes/moderation/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { getServer } from './getServer';
import { getUser } from './getUser';
import { updateUser } from './updateUser';
import { getStats } from './getStats';
import { userBatchUnsuspend } from './userBatchUnsuspend';

const ModerationRouter = Router();

getStats(ModerationRouter);
userBatchUnsuspend(ModerationRouter);
userBatchSuspend(ModerationRouter);
getUser(ModerationRouter);
updateUser(ModerationRouter);
Expand Down
1 change: 1 addition & 0 deletions src/routes/moderation/getUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async function route(req: Request, res: Response) {
profile: true,
account: {
select: {
suspendCount: true,
email: true,
},
},
Expand Down
78 changes: 78 additions & 0 deletions src/routes/moderation/userBatchUnsuspend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { Request, Response, Router } from 'express';
import { body } from 'express-validator';
import { dateToDateTime, prisma } from '../../common/database';
import {
customExpressValidatorResult,
generateError,
} from '../../common/errorHandler';
import { generateId } from '../../common/flakeId';
import { removeDuplicates } from '../../common/utils';
import { authenticate } from '../../middleware/authenticate';
import { disconnectUsers } from '../../services/Moderation';
import { checkUserPassword } from '../../services/User';
import { isModMiddleware } from './isModMiddleware';
import { removeAccountCacheByUserIds } from '../../cache/UserCache';

export function userBatchUnsuspend(Router: Router) {
Router.delete(
'/moderation/users/suspend',
authenticate(),
isModMiddleware,
body('userIds')
.not()
.isEmpty()
.withMessage('userIds is required')
.isArray()
.withMessage('userIds must be an array.'),
body('password')
.isString()
.withMessage('Password must be a string!')
.not()
.isEmpty()
.withMessage('Password is required'),
route
);
}

interface Body {
userIds: string[];
password: string;
}

async function route(req: Request<unknown, unknown, Body>, res: Response) {
const validateError = customExpressValidatorResult(req);
if (validateError) {
return res.status(400).json(validateError);
}

const account = await prisma.account.findFirst({
where: { id: req.accountCache.id },
select: { password: true },
});
if (!account)
return res
.status(404)
.json(generateError('Something went wrong. Try again later.'));

const isPasswordValid = await checkUserPassword(
req.body.password,
account.password!
);
if (!isPasswordValid)
return res.status(403).json(generateError('Invalid password.', 'password'));

if (req.body.userIds.length >= 5000)
return res
.status(403)
.json(generateError('user ids must contain less than 5000 ids.'));

const sanitizedUserIds = removeDuplicates(req.body.userIds) as string[];

await prisma.suspension.deleteMany({
where: { userId: { in: sanitizedUserIds } },
});

await removeAccountCacheByUserIds(sanitizedUserIds);

res.status(200).json({ success: true });
}

0 comments on commit 6186a18

Please sign in to comment.