From d95d15598847c7f020aa056e7e6e0c02d2bbf732 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 1 Jul 2020 16:05:30 +0200 Subject: Use 3 tables to represent abuses --- server/middlewares/validators/abuse.ts | 253 +++++++++++++++++++++ server/middlewares/validators/index.ts | 1 + server/middlewares/validators/sort.ts | 6 +- server/middlewares/validators/videos/index.ts | 1 - .../middlewares/validators/videos/video-abuses.ts | 135 ----------- 5 files changed, 257 insertions(+), 139 deletions(-) create mode 100644 server/middlewares/validators/abuse.ts delete mode 100644 server/middlewares/validators/videos/video-abuses.ts (limited to 'server/middlewares') diff --git a/server/middlewares/validators/abuse.ts b/server/middlewares/validators/abuse.ts new file mode 100644 index 000000000..f098e2ff9 --- /dev/null +++ b/server/middlewares/validators/abuse.ts @@ -0,0 +1,253 @@ +import * as express from 'express' +import { body, param, query } from 'express-validator' +import { + isAbuseModerationCommentValid, + isAbusePredefinedReasonsValid, + isAbusePredefinedReasonValid, + isAbuseReasonValid, + isAbuseStateValid, + isAbuseTimestampCoherent, + isAbuseTimestampValid, + isAbuseVideoIsValid +} from '@server/helpers/custom-validators/abuses' +import { exists, isIdOrUUIDValid, isIdValid, toIntOrNull } from '@server/helpers/custom-validators/misc' +import { logger } from '@server/helpers/logger' +import { doesAbuseExist, doesVideoAbuseExist, doesVideoExist } from '@server/helpers/middlewares' +import { areValidationErrors } from './utils' + +const abuseReportValidator = [ + param('videoId') + .custom(isIdOrUUIDValid) + .not() + .isEmpty() + .withMessage('Should have a valid videoId'), + body('reason') + .custom(isAbuseReasonValid) + .withMessage('Should have a valid reason'), + body('predefinedReasons') + .optional() + .custom(isAbusePredefinedReasonsValid) + .withMessage('Should have a valid list of predefined reasons'), + body('startAt') + .optional() + .customSanitizer(toIntOrNull) + .custom(isAbuseTimestampValid) + .withMessage('Should have valid starting time value'), + body('endAt') + .optional() + .customSanitizer(toIntOrNull) + .custom(isAbuseTimestampValid) + .withMessage('Should have valid ending time value') + .bail() + .custom(isAbuseTimestampCoherent) + .withMessage('Should have a startAt timestamp beginning before endAt'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking abuseReport parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await doesVideoExist(req.params.videoId, res)) return + + // TODO: check comment or video (exlusive) + + return next() + } +] + +const abuseGetValidator = [ + param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking abuseGetValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + // if (!await doesAbuseExist(req.params.id, req.params.videoId, res)) return + + return next() + } +] + +const abuseUpdateValidator = [ + param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'), + body('state') + .optional() + .custom(isAbuseStateValid).withMessage('Should have a valid video abuse state'), + body('moderationComment') + .optional() + .custom(isAbuseModerationCommentValid).withMessage('Should have a valid video moderation comment'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking abuseUpdateValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + // if (!await doesAbuseExist(req.params.id, req.params.videoId, res)) return + + return next() + } +] + +const abuseListValidator = [ + query('id') + .optional() + .custom(isIdValid).withMessage('Should have a valid id'), + query('predefinedReason') + .optional() + .custom(isAbusePredefinedReasonValid) + .withMessage('Should have a valid predefinedReason'), + query('search') + .optional() + .custom(exists).withMessage('Should have a valid search'), + query('state') + .optional() + .custom(isAbuseStateValid).withMessage('Should have a valid video abuse state'), + query('videoIs') + .optional() + .custom(isAbuseVideoIsValid).withMessage('Should have a valid "video is" attribute'), + query('searchReporter') + .optional() + .custom(exists).withMessage('Should have a valid reporter search'), + query('searchReportee') + .optional() + .custom(exists).withMessage('Should have a valid reportee search'), + query('searchVideo') + .optional() + .custom(exists).withMessage('Should have a valid video search'), + query('searchVideoChannel') + .optional() + .custom(exists).withMessage('Should have a valid video channel search'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking abuseListValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + +// FIXME: deprecated in 2.3. Remove these validators + +const videoAbuseReportValidator = [ + param('videoId') + .custom(isIdOrUUIDValid) + .not() + .isEmpty() + .withMessage('Should have a valid videoId'), + body('reason') + .custom(isAbuseReasonValid) + .withMessage('Should have a valid reason'), + body('predefinedReasons') + .optional() + .custom(isAbusePredefinedReasonsValid) + .withMessage('Should have a valid list of predefined reasons'), + body('startAt') + .optional() + .customSanitizer(toIntOrNull) + .custom(isAbuseTimestampValid) + .withMessage('Should have valid starting time value'), + body('endAt') + .optional() + .customSanitizer(toIntOrNull) + .custom(isAbuseTimestampValid) + .withMessage('Should have valid ending time value') + .bail() + .custom(isAbuseTimestampCoherent) + .withMessage('Should have a startAt timestamp beginning before endAt'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoAbuseReport parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await doesVideoExist(req.params.videoId, res)) return + + return next() + } +] + +const videoAbuseGetValidator = [ + param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoAbuseGetValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await doesVideoAbuseExist(req.params.id, req.params.videoId, res)) return + + return next() + } +] + +const videoAbuseUpdateValidator = [ + param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), + param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'), + body('state') + .optional() + .custom(isAbuseStateValid).withMessage('Should have a valid video abuse state'), + body('moderationComment') + .optional() + .custom(isAbuseModerationCommentValid).withMessage('Should have a valid video moderation comment'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoAbuseUpdateValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + if (!await doesVideoAbuseExist(req.params.id, req.params.videoId, res)) return + + return next() + } +] + +const videoAbuseListValidator = [ + query('id') + .optional() + .custom(isIdValid).withMessage('Should have a valid id'), + query('predefinedReason') + .optional() + .custom(isAbusePredefinedReasonValid) + .withMessage('Should have a valid predefinedReason'), + query('search') + .optional() + .custom(exists).withMessage('Should have a valid search'), + query('state') + .optional() + .custom(isAbuseStateValid).withMessage('Should have a valid video abuse state'), + query('videoIs') + .optional() + .custom(isAbuseVideoIsValid).withMessage('Should have a valid "video is" attribute'), + query('searchReporter') + .optional() + .custom(exists).withMessage('Should have a valid reporter search'), + query('searchReportee') + .optional() + .custom(exists).withMessage('Should have a valid reportee search'), + query('searchVideo') + .optional() + .custom(exists).withMessage('Should have a valid video search'), + query('searchVideoChannel') + .optional() + .custom(exists).withMessage('Should have a valid video channel search'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking videoAbuseListValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + abuseListValidator, + abuseReportValidator, + abuseGetValidator, + abuseUpdateValidator, + videoAbuseReportValidator, + videoAbuseGetValidator, + videoAbuseUpdateValidator, + videoAbuseListValidator +} diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index 65dd00335..4086d77aa 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts @@ -1,3 +1,4 @@ +export * from './abuse' export * from './account' export * from './blocklist' export * from './oembed' diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index b76dab722..29aba0436 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts @@ -5,7 +5,7 @@ import { checkSort, createSortableColumns } from './utils' const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS) const SORTABLE_ACCOUNTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ACCOUNTS) const SORTABLE_JOBS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.JOBS) -const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES) +const SORTABLE_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ABUSES) const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS) const SORTABLE_VIDEOS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS_SEARCH) const SORTABLE_VIDEO_CHANNELS_SEARCH_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS_SEARCH) @@ -28,7 +28,7 @@ const SORTABLE_VIDEO_REDUNDANCIES_COLUMNS = createSortableColumns(SORTABLE_COLUM const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS) const jobsSortValidator = checkSort(SORTABLE_JOBS_COLUMNS) -const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS) +const abusesSortValidator = checkSort(SORTABLE_ABUSES_COLUMNS) const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS) const videoImportsSortValidator = checkSort(SORTABLE_VIDEO_IMPORTS_COLUMNS) const videosSearchSortValidator = checkSort(SORTABLE_VIDEOS_SEARCH_COLUMNS) @@ -52,7 +52,7 @@ const videoRedundanciesSortValidator = checkSort(SORTABLE_VIDEO_REDUNDANCIES_COL export { usersSortValidator, - videoAbusesSortValidator, + abusesSortValidator, videoChannelsSortValidator, videoImportsSortValidator, videosSearchSortValidator, diff --git a/server/middlewares/validators/videos/index.ts b/server/middlewares/validators/videos/index.ts index a0d585b93..1eabada0a 100644 --- a/server/middlewares/validators/videos/index.ts +++ b/server/middlewares/validators/videos/index.ts @@ -1,4 +1,3 @@ -export * from './video-abuses' export * from './video-blacklist' export * from './video-captions' export * from './video-channels' diff --git a/server/middlewares/validators/videos/video-abuses.ts b/server/middlewares/validators/videos/video-abuses.ts deleted file mode 100644 index 5bbd1e3c6..000000000 --- a/server/middlewares/validators/videos/video-abuses.ts +++ /dev/null @@ -1,135 +0,0 @@ -import * as express from 'express' -import { body, param, query } from 'express-validator' -import { exists, isIdOrUUIDValid, isIdValid, toIntOrNull } from '../../../helpers/custom-validators/misc' -import { - isAbuseVideoIsValid, - isVideoAbuseModerationCommentValid, - isVideoAbuseReasonValid, - isVideoAbuseStateValid, - isVideoAbusePredefinedReasonsValid, - isVideoAbusePredefinedReasonValid, - isVideoAbuseTimestampValid, - isVideoAbuseTimestampCoherent -} from '../../../helpers/custom-validators/video-abuses' -import { logger } from '../../../helpers/logger' -import { doesVideoAbuseExist, doesVideoExist } from '../../../helpers/middlewares' -import { areValidationErrors } from '../utils' - -const videoAbuseReportValidator = [ - param('videoId') - .custom(isIdOrUUIDValid) - .not() - .isEmpty() - .withMessage('Should have a valid videoId'), - body('reason') - .custom(isVideoAbuseReasonValid) - .withMessage('Should have a valid reason'), - body('predefinedReasons') - .optional() - .custom(isVideoAbusePredefinedReasonsValid) - .withMessage('Should have a valid list of predefined reasons'), - body('startAt') - .optional() - .customSanitizer(toIntOrNull) - .custom(isVideoAbuseTimestampValid) - .withMessage('Should have valid starting time value'), - body('endAt') - .optional() - .customSanitizer(toIntOrNull) - .custom(isVideoAbuseTimestampValid) - .withMessage('Should have valid ending time value') - .bail() - .custom(isVideoAbuseTimestampCoherent) - .withMessage('Should have a startAt timestamp beginning before endAt'), - - async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoAbuseReport parameters', { parameters: req.body }) - - if (areValidationErrors(req, res)) return - if (!await doesVideoExist(req.params.videoId, res)) return - - return next() - } -] - -const videoAbuseGetValidator = [ - param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), - param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'), - - async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoAbuseGetValidator parameters', { parameters: req.body }) - - if (areValidationErrors(req, res)) return - if (!await doesVideoAbuseExist(req.params.id, req.params.videoId, res)) return - - return next() - } -] - -const videoAbuseUpdateValidator = [ - param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), - param('id').custom(isIdValid).not().isEmpty().withMessage('Should have a valid id'), - body('state') - .optional() - .custom(isVideoAbuseStateValid).withMessage('Should have a valid video abuse state'), - body('moderationComment') - .optional() - .custom(isVideoAbuseModerationCommentValid).withMessage('Should have a valid video moderation comment'), - - async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoAbuseUpdateValidator parameters', { parameters: req.body }) - - if (areValidationErrors(req, res)) return - if (!await doesVideoAbuseExist(req.params.id, req.params.videoId, res)) return - - return next() - } -] - -const videoAbuseListValidator = [ - query('id') - .optional() - .custom(isIdValid).withMessage('Should have a valid id'), - query('predefinedReason') - .optional() - .custom(isVideoAbusePredefinedReasonValid) - .withMessage('Should have a valid predefinedReason'), - query('search') - .optional() - .custom(exists).withMessage('Should have a valid search'), - query('state') - .optional() - .custom(isVideoAbuseStateValid).withMessage('Should have a valid video abuse state'), - query('videoIs') - .optional() - .custom(isAbuseVideoIsValid).withMessage('Should have a valid "video is" attribute'), - query('searchReporter') - .optional() - .custom(exists).withMessage('Should have a valid reporter search'), - query('searchReportee') - .optional() - .custom(exists).withMessage('Should have a valid reportee search'), - query('searchVideo') - .optional() - .custom(exists).withMessage('Should have a valid video search'), - query('searchVideoChannel') - .optional() - .custom(exists).withMessage('Should have a valid video channel search'), - - (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoAbuseListValidator parameters', { parameters: req.body }) - - if (areValidationErrors(req, res)) return - - return next() - } -] - -// --------------------------------------------------------------------------- - -export { - videoAbuseListValidator, - videoAbuseReportValidator, - videoAbuseGetValidator, - videoAbuseUpdateValidator -} -- cgit v1.2.3