2020-07-01 16:05:30 +02:00
|
|
|
import validator from 'validator'
|
2020-07-07 10:57:04 +02:00
|
|
|
import { AbuseFilter, abusePredefinedReasonsMap, AbusePredefinedReasonsString, AbuseVideoIs, AbuseCreate } from '@shared/models'
|
|
|
|
import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants'
|
2020-07-01 16:05:30 +02:00
|
|
|
import { exists, isArray } from './misc'
|
|
|
|
|
2020-07-08 15:51:46 +02:00
|
|
|
const ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES
|
2020-07-01 16:05:30 +02:00
|
|
|
|
|
|
|
function isAbuseReasonValid (value: string) {
|
2020-07-08 15:51:46 +02:00
|
|
|
return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.REASON)
|
2020-07-01 16:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isAbusePredefinedReasonValid (value: AbusePredefinedReasonsString) {
|
|
|
|
return exists(value) && value in abusePredefinedReasonsMap
|
|
|
|
}
|
|
|
|
|
2020-07-07 10:57:04 +02:00
|
|
|
function isAbuseFilterValid (value: AbuseFilter) {
|
|
|
|
return value === 'video' || value === 'comment' || value === 'account'
|
|
|
|
}
|
|
|
|
|
|
|
|
function areAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) {
|
2020-07-01 16:05:30 +02:00
|
|
|
return exists(value) && isArray(value) && value.every(v => v in abusePredefinedReasonsMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
function isAbuseTimestampValid (value: number) {
|
|
|
|
return value === null || (exists(value) && validator.isInt('' + value, { min: 0 }))
|
|
|
|
}
|
|
|
|
|
|
|
|
function isAbuseTimestampCoherent (endAt: number, { req }) {
|
2020-07-07 10:57:04 +02:00
|
|
|
const startAt = (req.body as AbuseCreate).video.startAt
|
|
|
|
|
|
|
|
return exists(startAt) && endAt > startAt
|
2020-07-01 16:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isAbuseModerationCommentValid (value: string) {
|
2020-07-08 15:51:46 +02:00
|
|
|
return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT)
|
2020-07-01 16:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isAbuseStateValid (value: string) {
|
|
|
|
return exists(value) && ABUSE_STATES[value] !== undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
function isAbuseVideoIsValid (value: AbuseVideoIs) {
|
|
|
|
return exists(value) && (
|
|
|
|
value === 'deleted' ||
|
|
|
|
value === 'blacklisted'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
isAbuseReasonValid,
|
2020-07-07 10:57:04 +02:00
|
|
|
isAbuseFilterValid,
|
2020-07-01 16:05:30 +02:00
|
|
|
isAbusePredefinedReasonValid,
|
2020-07-07 10:57:04 +02:00
|
|
|
areAbusePredefinedReasonsValid as isAbusePredefinedReasonsValid,
|
2020-07-01 16:05:30 +02:00
|
|
|
isAbuseTimestampValid,
|
|
|
|
isAbuseTimestampCoherent,
|
|
|
|
isAbuseModerationCommentValid,
|
|
|
|
isAbuseStateValid,
|
|
|
|
isAbuseVideoIsValid
|
|
|
|
}
|