X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fhelpers%2Fcustom-validators%2Fabuses.ts;h=94719641abd7561ff6414c43c31c58d304d0e7ed;hb=a66c2e3252d6cca8958959966f42494ded564023;hp=a6a895c659edb130d67dbc584d5e6a43e3f93c23;hpb=d95d15598847c7f020aa056e7e6e0c02d2bbf732;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/helpers/custom-validators/abuses.ts b/server/helpers/custom-validators/abuses.ts index a6a895c65..94719641a 100644 --- a/server/helpers/custom-validators/abuses.ts +++ b/server/helpers/custom-validators/abuses.ts @@ -1,19 +1,25 @@ import validator from 'validator' -import { abusePredefinedReasonsMap, AbusePredefinedReasonsString, AbuseVideoIs } from '@shared/models' -import { CONSTRAINTS_FIELDS, ABUSE_STATES } from '../../initializers/constants' +import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse' +import { AbuseCreate, AbuseFilter, AbusePredefinedReasonsString, AbuseVideoIs } from '@shared/models' +import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants' import { exists, isArray } from './misc' -const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES +const ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES +const ABUSE_MESSAGES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSE_MESSAGES function isAbuseReasonValid (value: string) { - return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON) + return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.REASON) } function isAbusePredefinedReasonValid (value: AbusePredefinedReasonsString) { return exists(value) && value in abusePredefinedReasonsMap } -function isAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) { +function isAbuseFilterValid (value: AbuseFilter) { + return value === 'video' || value === 'comment' || value === 'account' +} + +function areAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) { return exists(value) && isArray(value) && value.every(v => v in abusePredefinedReasonsMap) } @@ -22,11 +28,13 @@ function isAbuseTimestampValid (value: number) { } function isAbuseTimestampCoherent (endAt: number, { req }) { - return exists(req.body.startAt) && endAt > req.body.startAt + const startAt = (req.body as AbuseCreate).video.startAt + + return exists(startAt) && endAt > startAt } function isAbuseModerationCommentValid (value: string) { - return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT) + return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT) } function isAbuseStateValid (value: string) { @@ -40,12 +48,18 @@ function isAbuseVideoIsValid (value: AbuseVideoIs) { ) } +function isAbuseMessageValid (value: string) { + return exists(value) && validator.isLength(value, ABUSE_MESSAGES_CONSTRAINTS_FIELDS.MESSAGE) +} + // --------------------------------------------------------------------------- export { isAbuseReasonValid, + isAbuseFilterValid, isAbusePredefinedReasonValid, - isAbusePredefinedReasonsValid, + isAbuseMessageValid, + areAbusePredefinedReasonsValid, isAbuseTimestampValid, isAbuseTimestampCoherent, isAbuseModerationCommentValid,