1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
5 isAbuseModerationCommentValid,
6 isAbusePredefinedReasonsValid,
7 isAbusePredefinedReasonValid,
10 isAbuseTimestampCoherent,
11 isAbuseTimestampValid,
13 } from '@server/helpers/custom-validators/abuses'
14 import { exists, isIdOrUUIDValid, isIdValid, toIntOrNull } from '@server/helpers/custom-validators/misc'
15 import { doesCommentIdExist } from '@server/helpers/custom-validators/video-comments'
16 import { logger } from '@server/helpers/logger'
17 import { doesAbuseExist, doesAccountIdExist, doesVideoAbuseExist, doesVideoExist } from '@server/helpers/middlewares'
18 import { AbuseCreate } from '@shared/models'
19 import { areValidationErrors } from './utils'
21 const abuseReportValidator = [
25 .withMessage('Should have a valid accountId'),
29 .custom(isIdOrUUIDValid)
30 .withMessage('Should have a valid videoId'),
33 .customSanitizer(toIntOrNull)
34 .custom(isAbuseTimestampValid)
35 .withMessage('Should have valid starting time value'),
38 .customSanitizer(toIntOrNull)
39 .custom(isAbuseTimestampValid)
40 .withMessage('Should have valid ending time value')
42 .custom(isAbuseTimestampCoherent)
43 .withMessage('Should have a startAt timestamp beginning before endAt'),
48 .withMessage('Should have a valid commentId'),
51 .custom(isAbuseReasonValid)
52 .withMessage('Should have a valid reason'),
54 body('predefinedReasons')
56 .custom(isAbusePredefinedReasonsValid)
57 .withMessage('Should have a valid list of predefined reasons'),
59 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
60 logger.debug('Checking abuseReport parameters', { parameters: req.body })
62 if (areValidationErrors(req, res)) return
64 const body: AbuseCreate = req.body
66 if (body.video?.id && !await doesVideoExist(body.video.id, res)) return
67 if (body.account?.id && !await doesAccountIdExist(body.account.id, res)) return
68 if (body.comment?.id && !await doesCommentIdExist(body.comment.id, res)) return
70 if (!body.video?.id && !body.account?.id && !body.comment?.id) {
72 .json({ error: 'video id or account id or comment id is required.' })
81 const abuseGetValidator = [
82 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
84 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
85 logger.debug('Checking abuseGetValidator parameters', { parameters: req.body })
87 if (areValidationErrors(req, res)) return
88 if (!await doesAbuseExist(req.params.id, res)) return
94 const abuseUpdateValidator = [
95 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
99 .custom(isAbuseStateValid).withMessage('Should have a valid abuse state'),
100 body('moderationComment')
102 .custom(isAbuseModerationCommentValid).withMessage('Should have a valid moderation comment'),
104 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
105 logger.debug('Checking abuseUpdateValidator parameters', { parameters: req.body })
107 if (areValidationErrors(req, res)) return
108 if (!await doesAbuseExist(req.params.id, res)) return
114 const abuseListValidator = [
117 .custom(isIdValid).withMessage('Should have a valid id'),
120 .custom(isAbuseFilterValid)
121 .withMessage('Should have a valid filter'),
122 query('predefinedReason')
124 .custom(isAbusePredefinedReasonValid)
125 .withMessage('Should have a valid predefinedReason'),
128 .custom(exists).withMessage('Should have a valid search'),
131 .custom(isAbuseStateValid).withMessage('Should have a valid abuse state'),
134 .custom(isAbuseVideoIsValid).withMessage('Should have a valid "video is" attribute'),
135 query('searchReporter')
137 .custom(exists).withMessage('Should have a valid reporter search'),
138 query('searchReportee')
140 .custom(exists).withMessage('Should have a valid reportee search'),
143 .custom(exists).withMessage('Should have a valid video search'),
144 query('searchVideoChannel')
146 .custom(exists).withMessage('Should have a valid video channel search'),
148 (req: express.Request, res: express.Response, next: express.NextFunction) => {
149 logger.debug('Checking abuseListValidator parameters', { parameters: req.body })
151 if (areValidationErrors(req, res)) return
157 // FIXME: deprecated in 2.3. Remove these validators
159 const videoAbuseReportValidator = [
161 .custom(isIdOrUUIDValid)
164 .withMessage('Should have a valid videoId'),
166 .custom(isAbuseReasonValid)
167 .withMessage('Should have a valid reason'),
168 body('predefinedReasons')
170 .custom(isAbusePredefinedReasonsValid)
171 .withMessage('Should have a valid list of predefined reasons'),
174 .customSanitizer(toIntOrNull)
175 .custom(isAbuseTimestampValid)
176 .withMessage('Should have valid starting time value'),
179 .customSanitizer(toIntOrNull)
180 .custom(isAbuseTimestampValid)
181 .withMessage('Should have valid ending time value'),
183 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
184 logger.debug('Checking videoAbuseReport parameters', { parameters: req.body })
186 if (areValidationErrors(req, res)) return
187 if (!await doesVideoExist(req.params.videoId, res)) return
193 const videoAbuseGetValidator = [
194 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
195 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
197 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
198 logger.debug('Checking videoAbuseGetValidator parameters', { parameters: req.body })
200 if (areValidationErrors(req, res)) return
201 if (!await doesVideoAbuseExist(req.params.id, req.params.videoId, res)) return
207 const videoAbuseUpdateValidator = [
208 param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'),
209 param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'),
212 .custom(isAbuseStateValid).withMessage('Should have a valid video abuse state'),
213 body('moderationComment')
215 .custom(isAbuseModerationCommentValid).withMessage('Should have a valid video moderation comment'),
217 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
218 logger.debug('Checking videoAbuseUpdateValidator parameters', { parameters: req.body })
220 if (areValidationErrors(req, res)) return
221 if (!await doesVideoAbuseExist(req.params.id, req.params.videoId, res)) return
227 const videoAbuseListValidator = [
230 .custom(isIdValid).withMessage('Should have a valid id'),
231 query('predefinedReason')
233 .custom(isAbusePredefinedReasonValid)
234 .withMessage('Should have a valid predefinedReason'),
237 .custom(exists).withMessage('Should have a valid search'),
240 .custom(isAbuseStateValid).withMessage('Should have a valid video abuse state'),
243 .custom(isAbuseVideoIsValid).withMessage('Should have a valid "video is" attribute'),
244 query('searchReporter')
246 .custom(exists).withMessage('Should have a valid reporter search'),
247 query('searchReportee')
249 .custom(exists).withMessage('Should have a valid reportee search'),
252 .custom(exists).withMessage('Should have a valid video search'),
253 query('searchVideoChannel')
255 .custom(exists).withMessage('Should have a valid video channel search'),
257 (req: express.Request, res: express.Response, next: express.NextFunction) => {
258 logger.debug('Checking videoAbuseListValidator parameters', { parameters: req.body })
260 if (areValidationErrors(req, res)) return
266 // ---------------------------------------------------------------------------
270 abuseReportValidator,
272 abuseUpdateValidator,
273 videoAbuseReportValidator,
274 videoAbuseGetValidator,
275 videoAbuseUpdateValidator,
276 videoAbuseListValidator