Skip to content

Commit

Permalink
update table AuditLog to ModAuditLog
Browse files Browse the repository at this point in the history
  • Loading branch information
SupertigerDev committed Oct 22, 2024
1 parent c6470f2 commit 99fbb45
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 228 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE "AuditLog" RENAME TO "ModAuditLog";


ALTER TABLE "ModAuditLog" RENAME CONSTRAINT "AuditLog_actionById_fkey" to "ModAuditLog_actionById_fkey";
ALTER TABLE "ModAuditLog" RENAME CONSTRAINT "AuditLog_pkey" to "ModAuditLog_pkey";


4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ model User {
connections UserConnection[]
notice ChatNotice?
auditActions AuditLog[]
auditActions ModAuditLog[]
openedTickets Ticket[]
Expand Down Expand Up @@ -837,7 +837,7 @@ model ChatNotice {
}


model AuditLog {
model ModAuditLog {
id String @id
createdAt DateTime @default(now())
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/routes/moderation/getAuditLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ async function route(req: Request, res: Response) {
limit = 30;
}

const logs = await prisma.auditLog.findMany({
const logs = await prisma.modAuditLog.findMany({
orderBy: {
createdAt: 'desc',
},
Expand Down
48 changes: 11 additions & 37 deletions src/routes/moderation/postBatchDelete.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { Request, Response, Router } from 'express';
import { body } from 'express-validator';
import { prisma } from '../../common/database';
import {
customExpressValidatorResult,
generateError,
} from '../../common/errorHandler';
import { customExpressValidatorResult, generateError } from '../../common/errorHandler';
import { generateId } from '../../common/flakeId';
import { removeDuplicates } from '../../common/utils';
import { authenticate } from '../../middleware/authenticate';
import { isModMiddleware } from './isModMiddleware';
import { AuditLogType } from '../../common/AuditLog';
import { AuditLogType } from '../../common/ModAuditLog';
import { checkUserPassword } from '../../services/UserAuthentication';
import { deleteFile } from '../../common/nerimityCDN';

Expand All @@ -18,21 +15,9 @@ export function postBatchSuspend(Router: Router) {
'/moderation/posts/delete',
authenticate(),
isModMiddleware,
body('postIds')
.not()
.isEmpty()
.withMessage('postIds is required')
.isArray()
.withMessage('postIds must be an array.'),
body('postIds').not().isEmpty().withMessage('postIds is required').isArray().withMessage('postIds must be an array.'),

body('password')
.isLength({ min: 4, max: 72 })
.withMessage('Password must be between 4 and 72 characters long.')
.isString()
.withMessage('Password must be a string!')
.not()
.isEmpty()
.withMessage('Password is required'),
body('password').isLength({ min: 4, max: 72 }).withMessage('Password must be between 4 and 72 characters long.').isString().withMessage('Password must be a string!').not().isEmpty().withMessage('Password is required'),
route
);
}
Expand All @@ -52,22 +37,12 @@ async function route(req: Request<unknown, unknown, Body>, res: Response) {
where: { id: req.userCache.account?.id },
select: { password: true },
});
if (!account)
return res
.status(404)
.json(generateError('Something went wrong. Try again later.'));
if (!account) return res.status(404).json(generateError('Something went wrong. Try again later.'));

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

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

const sanitizedPostIds = removeDuplicates(req.body.postIds) as string[];

Expand All @@ -94,8 +69,8 @@ async function route(req: Request<unknown, unknown, Body>, res: Response) {
prisma.announcementPost.deleteMany({ where: { postId: { in: validPostIds } } }),
]);

await prisma.auditLog.createMany({
data: posts.map(post => ({
await prisma.modAuditLog.createMany({
data: posts.map((post) => ({
id: generateId(),
actionType: AuditLogType.postDelete,
actionById: req.userCache.id,
Expand All @@ -112,7 +87,6 @@ async function route(req: Request<unknown, unknown, Body>, res: Response) {
if (!post?.attachments[0]) continue;
const attachment = post.attachments[0];
if (!attachment.path) continue;
await deleteFile(attachment.path).catch(() => { });
await deleteFile(attachment.path).catch(() => {});
}

}
34 changes: 8 additions & 26 deletions src/routes/moderation/serverDelete.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Request, Response, Router } from 'express';
import { body } from 'express-validator';
import { prisma } from '../../common/database';
import {
customExpressValidatorResult,
generateError,
} from '../../common/errorHandler';
import { customExpressValidatorResult, generateError } from '../../common/errorHandler';

import { authenticate } from '../../middleware/authenticate';
import { isModMiddleware } from './isModMiddleware';
import { deleteServer } from '../../services/Server';
import { generateId } from '../../common/flakeId';
import { AuditLogType } from '../../common/AuditLog';
import { AuditLogType } from '../../common/ModAuditLog';
import { checkUserPassword } from '../../services/UserAuthentication';

export function serverDelete(Router: Router) {
Expand All @@ -19,14 +16,7 @@ export function serverDelete(Router: Router) {
authenticate(),
isModMiddleware,

body('password')
.isLength({ min: 4, max: 72 })
.withMessage('Password must be between 4 and 72 characters long.')
.isString()
.withMessage('Password must be a string!')
.not()
.isEmpty()
.withMessage('Password is required'),
body('password').isLength({ min: 4, max: 72 }).withMessage('Password must be between 4 and 72 characters long.').isString().withMessage('Password must be a string!').not().isEmpty().withMessage('Password is required'),
route
);
}
Expand All @@ -49,31 +39,23 @@ async function route(req: Request<Params, unknown, Body>, res: Response) {
where: { id: req.userCache.account.id },
select: { password: true },
});
if (!account)
return res
.status(404)
.json(generateError('Something went wrong. Try again later.'));
if (!account) return res.status(404).json(generateError('Something went wrong. Try again later.'));

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

const server = await prisma.server.findUnique({
where: { id: req.params.serverId },
});

if (!server)
return res.status(404).json(generateError('Server does not exist.'));
if (!server) return res.status(404).json(generateError('Server does not exist.'));

const [, error] = await deleteServer(req.params.serverId);
if (error) {
return res.status(403).json(error);
}

await prisma.auditLog.create({
await prisma.modAuditLog.create({
data: {
id: generateId(),
actionType: AuditLogType.serverDelete,
Expand Down
45 changes: 7 additions & 38 deletions src/routes/moderation/updateServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,15 @@ import { prisma } from '../../common/database';
import { authenticate } from '../../middleware/authenticate';
import { isModMiddleware } from './isModMiddleware';
import { body } from 'express-validator';
import {
customExpressValidatorResult,
generateError,
} from '../../common/errorHandler';
import { customExpressValidatorResult, generateError } from '../../common/errorHandler';
import { addToObjectIfExists } from '../../common/addToObjectIfExists';
import { emitServerUpdated } from '../../emits/Server';
import { generateId } from '../../common/flakeId';
import { AuditLogType } from '../../common/AuditLog';
import { AuditLogType } from '../../common/ModAuditLog';
import { checkUserPassword } from '../../services/UserAuthentication';

export function updateServer(Router: Router) {
Router.post(
'/moderation/servers/:serverId',
authenticate(),
isModMiddleware,
body('name')
.isString()
.withMessage('Password must be a string!')
.optional(),
body('verified')
.isBoolean()
.withMessage('Verified must be a boolean!')
.optional(),
body('password')
.isLength({ min: 4, max: 72 })
.withMessage('Password must be between 4 and 72 characters long.')
.isString()
.withMessage('Password must be a string!')
.not()
.isEmpty()
.withMessage('Password is required'),
route
);
Router.post('/moderation/servers/:serverId', authenticate(), isModMiddleware, body('name').isString().withMessage('Password must be a string!').optional(), body('verified').isBoolean().withMessage('Verified must be a boolean!').optional(), body('password').isLength({ min: 4, max: 72 }).withMessage('Password must be between 4 and 72 characters long.').isString().withMessage('Password must be a string!').not().isEmpty().withMessage('Password is required'), route);
}

async function route(req: Request, res: Response) {
Expand All @@ -50,17 +26,10 @@ async function route(req: Request, res: Response) {
where: { id: req.userCache.account.id },
select: { password: true },
});
if (!account)
return res
.status(404)
.json(generateError('Something went wrong. Try again later.'));
if (!account) return res.status(404).json(generateError('Something went wrong. Try again later.'));

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

const update = {
...addToObjectIfExists('name', req.body.name),
Expand All @@ -83,7 +52,7 @@ async function route(req: Request, res: Response) {

emitServerUpdated(serverId, update);

await prisma.auditLog.create({
await prisma.modAuditLog.create({
data: {
id: generateId(),
actionType: AuditLogType.serverUpdate,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/moderation/updateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import bcrypt from 'bcrypt';
import { removeUserCacheByUserIds } from '../../cache/UserCache';
import { getIO } from '../../socket/socket';
import { AUTHENTICATE_ERROR } from '../../common/ClientEventNames';
import { AuditLogType } from '../../common/AuditLog';
import { AuditLogType } from '../../common/ModAuditLog';
import { generateId } from '../../common/flakeId';
import { checkUserPassword } from '../../services/UserAuthentication';
import { Prisma } from '@prisma/client';
Expand Down Expand Up @@ -126,7 +126,7 @@ async function route(req: Request, res: Response) {
broadcaster.disconnectSockets(true);
}

await prisma.auditLog.create({
await prisma.modAuditLog.create({
data: {
id: generateId(),
actionType: AuditLogType.userUpdate,
Expand Down
24 changes: 13 additions & 11 deletions src/routes/moderation/userBatchSuspend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { authenticate } from '../../middleware/authenticate';
import { disconnectUsers } from '../../services/Moderation';
import { isModMiddleware } from './isModMiddleware';
import { removeAllowedIPsCache } from '../../cache/UserCache';
import { AuditLogType } from '../../common/AuditLog';
import { AuditLogType } from '../../common/ModAuditLog';
import { checkUserPassword } from '../../services/UserAuthentication';
import { hasBit, USER_BADGES } from '../../common/Bitwise';

Expand Down Expand Up @@ -159,15 +159,17 @@ async function route(req: Request<unknown, unknown, Body>, res: Response) {
});

if (ips.length) {
await prisma.auditLog.create({
data: {
id: generateId(),
actionType: AuditLogType.ipBan,
actionById: req.userCache.id,
count: removeDuplicates(ips).length,
expireAt: dateToDateTime(expireAfter(7)),
},
}).catch(() => { });
await prisma.modAuditLog
.create({
data: {
id: generateId(),
actionType: AuditLogType.ipBan,
actionById: req.userCache.id,
count: removeDuplicates(ips).length,
expireAt: dateToDateTime(expireAfter(7)),
},
})
.catch(() => {});
}
}

Expand All @@ -176,7 +178,7 @@ async function route(req: Request<unknown, unknown, Body>, res: Response) {
select: { id: true, username: true },
});

await prisma.auditLog.createMany({
await prisma.modAuditLog.createMany({
data: newSuspendedUsers.map((user) => ({
id: generateId(),
actionType: AuditLogType.userSuspend,
Expand Down
Loading

0 comments on commit 99fbb45

Please sign in to comment.