aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/abuses.ts
blob: 94719641abd7561ff6414c43c31c58d304d0e7ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import validator from 'validator'
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 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, ABUSES_CONSTRAINTS_FIELDS.REASON)
}

function isAbusePredefinedReasonValid (value: AbusePredefinedReasonsString) {
  return exists(value) && value in abusePredefinedReasonsMap
}

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)
}

function isAbuseTimestampValid (value: number) {
  return value === null || (exists(value) && validator.isInt('' + value, { min: 0 }))
}

function isAbuseTimestampCoherent (endAt: number, { req }) {
  const startAt = (req.body as AbuseCreate).video.startAt

  return exists(startAt) && endAt > startAt
}

function isAbuseModerationCommentValid (value: string) {
  return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT)
}

function isAbuseStateValid (value: string) {
  return exists(value) && ABUSE_STATES[value] !== undefined
}

function isAbuseVideoIsValid (value: AbuseVideoIs) {
  return exists(value) && (
    value === 'deleted' ||
    value === 'blacklisted'
  )
}

function isAbuseMessageValid (value: string) {
  return exists(value) && validator.isLength(value, ABUSE_MESSAGES_CONSTRAINTS_FIELDS.MESSAGE)
}

// ---------------------------------------------------------------------------

export {
  isAbuseReasonValid,
  isAbuseFilterValid,
  isAbusePredefinedReasonValid,
  isAbuseMessageValid,
  areAbusePredefinedReasonsValid,
  isAbuseTimestampValid,
  isAbuseTimestampCoherent,
  isAbuseModerationCommentValid,
  isAbuseStateValid,
  isAbuseVideoIsValid
}