]>
Commit | Line | Data |
---|---|---|
d95d1559 | 1 | import validator from 'validator' |
bd45d503 C |
2 | import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse' |
3 | import { AbuseCreate, AbuseFilter, AbusePredefinedReasonsString, AbuseVideoIs } from '@shared/models' | |
57f6896f | 4 | import { ABUSE_STATES, CONSTRAINTS_FIELDS } from '../../initializers/constants' |
d95d1559 C |
5 | import { exists, isArray } from './misc' |
6 | ||
310b5219 | 7 | const ABUSES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSES |
edbc9325 | 8 | const ABUSE_MESSAGES_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.ABUSE_MESSAGES |
d95d1559 C |
9 | |
10 | function isAbuseReasonValid (value: string) { | |
310b5219 | 11 | return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.REASON) |
d95d1559 C |
12 | } |
13 | ||
14 | function isAbusePredefinedReasonValid (value: AbusePredefinedReasonsString) { | |
15 | return exists(value) && value in abusePredefinedReasonsMap | |
16 | } | |
17 | ||
57f6896f C |
18 | function isAbuseFilterValid (value: AbuseFilter) { |
19 | return value === 'video' || value === 'comment' || value === 'account' | |
20 | } | |
21 | ||
22 | function areAbusePredefinedReasonsValid (value: AbusePredefinedReasonsString[]) { | |
d95d1559 C |
23 | return exists(value) && isArray(value) && value.every(v => v in abusePredefinedReasonsMap) |
24 | } | |
25 | ||
26 | function isAbuseTimestampValid (value: number) { | |
27 | return value === null || (exists(value) && validator.isInt('' + value, { min: 0 })) | |
28 | } | |
29 | ||
30 | function 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 | ||
36 | function isAbuseModerationCommentValid (value: string) { | |
310b5219 | 37 | return exists(value) && validator.isLength(value, ABUSES_CONSTRAINTS_FIELDS.MODERATION_COMMENT) |
d95d1559 C |
38 | } |
39 | ||
40 | function isAbuseStateValid (value: string) { | |
41 | return exists(value) && ABUSE_STATES[value] !== undefined | |
42 | } | |
43 | ||
44 | function isAbuseVideoIsValid (value: AbuseVideoIs) { | |
45 | return exists(value) && ( | |
46 | value === 'deleted' || | |
47 | value === 'blacklisted' | |
48 | ) | |
49 | } | |
50 | ||
edbc9325 C |
51 | function isAbuseMessageValid (value: string) { |
52 | return exists(value) && validator.isLength(value, ABUSE_MESSAGES_CONSTRAINTS_FIELDS.MESSAGE) | |
53 | } | |
54 | ||
d95d1559 C |
55 | // --------------------------------------------------------------------------- |
56 | ||
57 | export { | |
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 | } |