1 import validator from 'validator'
2 import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
3 import { AbuseCreate, AbuseFilter, AbusePredefinedReasonsString, AbuseVideoIs } from '@shared/models'
4 import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants'
5 import { exists, isArray } from './misc'
7 const ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES
8 const ABUSE_MESSAGES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSE_MESSAGES
10 function isAbuseReasonValid (value: string) {
11 return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.REASON)
14 function isAbusePredefinedReasonValid (value: AbusePredefinedReasonsString) {
15 return exists(value) && value in abusePredefinedReasonsMap
18 function isAbuseFilterValid (value: AbuseFilter) {
19 return value === 'video' || value === 'comment' || value === 'account'
22 function areAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) {
23 return exists(value) && isArray(value) && value.every(v => v in abusePredefinedReasonsMap)
26 function isAbuseTimestampValid (value: number) {
27 return value === null || (exists(value) && validator.isInt('' + value, { min: 0 }))
30 function isAbuseTimestampCoherent (endAt: number, { req }) {
31 const startAt = (req.body as AbuseCreate).video.startAt
33 return exists(startAt) && endAt > startAt
36 function isAbuseModerationCommentValid (value: string) {
37 return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT)
40 function isAbuseStateValid (value: string) {
41 return exists(value) && ABUSE_STATES[value] !== undefined
44 function isAbuseVideoIsValid (value: AbuseVideoIs) {
45 return exists(value) && (
46 value === 'deleted' ||
47 value === 'blacklisted'
51 function isAbuseMessageValid (value: string) {
52 return exists(value) && validator.isLength(value, ABUSE_MESSAGES_CONSTRAINTS_FIELDS.MESSAGE)
55 // ---------------------------------------------------------------------------
60 isAbusePredefinedReasonValid,
62 areAbusePredefinedReasonsValid,
63 isAbuseTimestampValid,
64 isAbuseTimestampCoherent,
65 isAbuseModerationCommentValid,