1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
4 areAbusePredefinedReasonsValid,
7 isAbuseModerationCommentValid,
8 isAbusePredefinedReasonValid,
11 isAbuseTimestampCoherent,
12 isAbuseTimestampValid,
14 } from '@server/helpers/custom-validators/abuses'
15 import { exists, isIdOrUUIDValid, isIdValid, toIntOrNull } from '@server/helpers/custom-validators/misc'
16 import { doesCommentIdExist } from '@server/helpers/custom-validators/video-comments'
17 import { logger } from '@server/helpers/logger'
18 import { doesAbuseExist, doesAccountIdExist, doesVideoExist } from '@server/helpers/middlewares'
19 import { AbuseMessageModel } from '@server/models/abuse/abuse-message'
20 import { AbuseCreate, UserRight } from '@shared/models'
21 import { areValidationErrors } from './utils'
22 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
24 const abuseReportValidator = [
28 .withMessage('Should have a valid accountId'),
32 .custom(isIdOrUUIDValid)
33 .withMessage('Should have a valid videoId'),
36 .customSanitizer(toIntOrNull)
37 .custom(isAbuseTimestampValid)
38 .withMessage('Should have valid starting time value'),
41 .customSanitizer(toIntOrNull)
42 .custom(isAbuseTimestampValid)
43 .withMessage('Should have valid ending time value')
45 .custom(isAbuseTimestampCoherent)
46 .withMessage('Should have a startAt timestamp beginning before endAt'),
51 .withMessage('Should have a valid commentId'),
54 .custom(isAbuseReasonValid)
55 .withMessage('Should have a valid reason'),
57 body('predefinedReasons')
59 .custom(areAbusePredefinedReasonsValid)
60 .withMessage('Should have a valid list of predefined reasons'),
62 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
63 logger.debug('Checking abuseReport parameters', { parameters: req.body })
65 if (areValidationErrors(req, res)) return
67 const body: AbuseCreate = req.body
69 if (body.video?.id && !await doesVideoExist(body.video.id, res)) return
70 if (body.account?.id && !await doesAccountIdExist(body.account.id, res)) return
71 if (body.comment?.id && !await doesCommentIdExist(body.comment.id, res)) return
73 if (!body.video?.id && !body.account?.id && !body.comment?.id) {
74 res.fail({ message: 'video id or account id or comment id is required.' })
82 const abuseGetValidator = [
83 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
85 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
86 logger.debug('Checking abuseGetValidator parameters', { parameters: req.body })
88 if (areValidationErrors(req, res)) return
89 if (!await doesAbuseExist(req.params.id, res)) return
95 const abuseUpdateValidator = [
96 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
100 .custom(isAbuseStateValid).withMessage('Should have a valid abuse state'),
101 body('moderationComment')
103 .custom(isAbuseModerationCommentValid).withMessage('Should have a valid moderation comment'),
105 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
106 logger.debug('Checking abuseUpdateValidator parameters', { parameters: req.body })
108 if (areValidationErrors(req, res)) return
109 if (!await doesAbuseExist(req.params.id, res)) return
115 const abuseListForAdminsValidator = [
118 .custom(isIdValid).withMessage('Should have a valid id'),
121 .custom(isAbuseFilterValid)
122 .withMessage('Should have a valid filter'),
123 query('predefinedReason')
125 .custom(isAbusePredefinedReasonValid)
126 .withMessage('Should have a valid predefinedReason'),
129 .custom(exists).withMessage('Should have a valid search'),
132 .custom(isAbuseStateValid).withMessage('Should have a valid abuse state'),
135 .custom(isAbuseVideoIsValid).withMessage('Should have a valid "video is" attribute'),
136 query('searchReporter')
138 .custom(exists).withMessage('Should have a valid reporter search'),
139 query('searchReportee')
141 .custom(exists).withMessage('Should have a valid reportee search'),
144 .custom(exists).withMessage('Should have a valid video search'),
145 query('searchVideoChannel')
147 .custom(exists).withMessage('Should have a valid video channel search'),
149 (req: express.Request, res: express.Response, next: express.NextFunction) => {
150 logger.debug('Checking abuseListForAdminsValidator parameters', { parameters: req.body })
152 if (areValidationErrors(req, res)) return
158 const abuseListForUserValidator = [
161 .custom(isIdValid).withMessage('Should have a valid id'),
165 .custom(exists).withMessage('Should have a valid search'),
169 .custom(isAbuseStateValid).withMessage('Should have a valid abuse state'),
171 (req: express.Request, res: express.Response, next: express.NextFunction) => {
172 logger.debug('Checking abuseListForUserValidator parameters', { parameters: req.body })
174 if (areValidationErrors(req, res)) return
180 const getAbuseValidator = [
181 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
183 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
184 logger.debug('Checking getAbuseValidator parameters', { parameters: req.body })
186 if (areValidationErrors(req, res)) return
187 if (!await doesAbuseExist(req.params.id, res)) return
189 const user = res.locals.oauth.token.user
190 const abuse = res.locals.abuse
192 if (user.hasRight(UserRight.MANAGE_ABUSES) !== true && abuse.reporterAccountId !== user.Account.id) {
193 const message = `User ${user.username} does not have right to get abuse ${abuse.id}`
197 status: HttpStatusCode.FORBIDDEN_403,
206 const checkAbuseValidForMessagesValidator = [
207 (req: express.Request, res: express.Response, next: express.NextFunction) => {
208 logger.debug('Checking checkAbuseValidForMessagesValidator parameters', { parameters: req.body })
210 const abuse = res.locals.abuse
211 if (abuse.ReporterAccount.isOwned() === false) {
212 return res.fail({ message: 'This abuse was created by a user of your instance.' })
219 const addAbuseMessageValidator = [
220 body('message').custom(isAbuseMessageValid).not().isEmpty().withMessage('Should have a valid abuse message'),
222 (req: express.Request, res: express.Response, next: express.NextFunction) => {
223 logger.debug('Checking addAbuseMessageValidator parameters', { parameters: req.body })
225 if (areValidationErrors(req, res)) return
231 const deleteAbuseMessageValidator = [
232 param('messageId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid message id'),
234 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
235 logger.debug('Checking deleteAbuseMessageValidator parameters', { parameters: req.body })
237 if (areValidationErrors(req, res)) return
239 const user = res.locals.oauth.token.user
240 const abuse = res.locals.abuse
242 const messageId = parseInt(req.params.messageId + '', 10)
243 const abuseMessage = await AbuseMessageModel.loadByIdAndAbuseId(messageId, abuse.id)
247 status: HttpStatusCode.NOT_FOUND_404,
248 message: 'Abuse message not found'
252 if (user.hasRight(UserRight.MANAGE_ABUSES) !== true && abuseMessage.accountId !== user.Account.id) {
254 status: HttpStatusCode.FORBIDDEN_403,
255 message: 'Cannot delete this abuse message'
259 res.locals.abuseMessage = abuseMessage
265 // ---------------------------------------------------------------------------
268 abuseListForAdminsValidator,
269 abuseReportValidator,
271 addAbuseMessageValidator,
272 checkAbuseValidForMessagesValidator,
273 abuseUpdateValidator,
274 deleteAbuseMessageValidator,
275 abuseListForUserValidator,