aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/video-abuses.ts
blob: 0c2c342681ea3aae4de8d2e547d8778bc7e04502 (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
import validator from 'validator'

import { CONSTRAINTS_FIELDS, VIDEO_ABUSE_STATES } from '../../initializers/constants'
import { exists, isArray } from './misc'
import { VideoAbuseVideoIs } from '@shared/models/videos/abuse/video-abuse-video-is.type'
import { VideoAbusePredefinedReasonsString, videoAbusePredefinedReasonsMap } from '@shared/models/videos/abuse/video-abuse-reason.model'

const VIDEO_ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_ABUSES

function isVideoAbuseReasonValid (value: string) {
  return exists(value) && validator.isLength(value, VIDEO_ABUSES_CONSTRAINTS_FIELDS.REASON)
}

function isVideoAbusePredefinedReasonValid (value: VideoAbusePredefinedReasonsString) {
  return exists(value) && value in videoAbusePredefinedReasonsMap
}

function isVideoAbusePredefinedReasonsValid (value: VideoAbusePredefinedReasonsString[]) {
  return exists(value) && isArray(value) && value.every(v => v in videoAbusePredefinedReasonsMap)
}

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

function isVideoAbuseTimestampCoherent (endAt: number, { req }) {
  return exists(req.body.startAt) && endAt > req.body.startAt
}

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

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

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

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

export {
  isVideoAbuseReasonValid,
  isVideoAbusePredefinedReasonValid,
  isVideoAbusePredefinedReasonsValid,
  isVideoAbuseTimestampValid,
  isVideoAbuseTimestampCoherent,
  isVideoAbuseModerationCommentValid,
  isVideoAbuseStateValid,
  isAbuseVideoIsValid
}