]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/abuses.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / abuses.ts
CommitLineData
d95d1559 1import validator from 'validator'
bd45d503
C
2import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
3import { AbuseCreate, AbuseFilter, AbusePredefinedReasonsString, AbuseVideoIs } from '@shared/models'
57f6896f 4import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants'
d95d1559
C
5import { exists, isArray } from './misc'
6
310b5219 7const ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES
edbc9325 8const ABUSE_MESSAGES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSE_MESSAGES
d95d1559
C
9
10function isAbuseReasonValid (value: string) {
310b5219 11 return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.REASON)
d95d1559
C
12}
13
14function isAbusePredefinedReasonValid (value: AbusePredefinedReasonsString) {
15 return exists(value) && value in abusePredefinedReasonsMap
16}
17
57f6896f
C
18function isAbuseFilterValid (value: AbuseFilter) {
19 return value === 'video' || value === 'comment' || value === 'account'
20}
21
22function areAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) {
d95d1559
C
23 return exists(value) && isArray(value) && value.every(v => v in abusePredefinedReasonsMap)
24}
25
26function isAbuseTimestampValid (value: number) {
27 return value === null || (exists(value) && validator.isInt('' + value, { min: 0 }))
28}
29
30function isAbuseTimestampCoherent (endAt: number, { req }) {
57f6896f
C
31 const startAt = (req.body as AbuseCreate).video.startAt
32
33 return exists(startAt) && endAt > startAt
d95d1559
C
34}
35
36function isAbuseModerationCommentValid (value: string) {
310b5219 37 return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT)
d95d1559
C
38}
39
40function isAbuseStateValid (value: string) {
41 return exists(value) && ABUSE_STATES[value] !== undefined
42}
43
44function isAbuseVideoIsValid (value: AbuseVideoIs) {
45 return exists(value) && (
46 value === 'deleted' ||
47 value === 'blacklisted'
48 )
49}
50
edbc9325
C
51function isAbuseMessageValid (value: string) {
52 return exists(value) && validator.isLength(value, ABUSE_MESSAGES_CONSTRAINTS_FIELDS.MESSAGE)
53}
54
d95d1559
C
55// ---------------------------------------------------------------------------
56
57export {
58 isAbuseReasonValid,
57f6896f 59 isAbuseFilterValid,
d95d1559 60 isAbusePredefinedReasonValid,
edbc9325
C
61 isAbuseMessageValid,
62 areAbusePredefinedReasonsValid,
d95d1559
C
63 isAbuseTimestampValid,
64 isAbuseTimestampCoherent,
65 isAbuseModerationCommentValid,
66 isAbuseStateValid,
67 isAbuseVideoIsValid
68}