]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/custom-validators/users.ts
f02b3ba656b0417a9472df00ee163f03411185cf
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / users.ts
1 import validator from 'validator'
2 import { UserRole } from '@shared/models'
3 import { isEmailEnabled } from '../../initializers/config'
4 import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants'
5 import { exists, isArray, isBooleanValid } from './misc'
6
7 const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
8
9 function isUserPasswordValid (value: string) {
10 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
11 }
12
13 function isUserPasswordValidOrEmpty (value: string) {
14 // Empty password is only possible if emailing is enabled.
15 if (value === '') return isEmailEnabled()
16
17 return isUserPasswordValid(value)
18 }
19
20 function isUserVideoQuotaValid (value: string) {
21 return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA)
22 }
23
24 function isUserVideoQuotaDailyValid (value: string) {
25 return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA_DAILY)
26 }
27
28 function isUserUsernameValid (value: string) {
29 return exists(value) &&
30 validator.matches(value, new RegExp(`^[a-z0-9_]+([a-z0-9_.-]+[a-z0-9_]+)?$`)) &&
31 validator.isLength(value, USERS_CONSTRAINTS_FIELDS.USERNAME)
32 }
33
34 function isUserDisplayNameValid (value: string) {
35 return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.NAME))
36 }
37
38 function isUserDescriptionValid (value: string) {
39 return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.DESCRIPTION))
40 }
41
42 function isUserEmailVerifiedValid (value: any) {
43 return isBooleanValid(value)
44 }
45
46 const nsfwPolicies = new Set(Object.values(NSFW_POLICY_TYPES))
47 function isUserNSFWPolicyValid (value: any) {
48 return exists(value) && nsfwPolicies.has(value)
49 }
50
51 function isUserP2PEnabledValid (value: any) {
52 return isBooleanValid(value)
53 }
54
55 function isUserVideosHistoryEnabledValid (value: any) {
56 return isBooleanValid(value)
57 }
58
59 function isUserAutoPlayVideoValid (value: any) {
60 return isBooleanValid(value)
61 }
62
63 function isUserVideoLanguages (value: any) {
64 return value === null || (isArray(value) && value.length < CONSTRAINTS_FIELDS.USERS.VIDEO_LANGUAGES.max)
65 }
66
67 function isUserAdminFlagsValid (value: any) {
68 return exists(value) && validator.isInt('' + value)
69 }
70
71 function isUserBlockedValid (value: any) {
72 return isBooleanValid(value)
73 }
74
75 function isUserAutoPlayNextVideoValid (value: any) {
76 return isBooleanValid(value)
77 }
78
79 function isUserAutoPlayNextVideoPlaylistValid (value: any) {
80 return isBooleanValid(value)
81 }
82
83 function isUserEmailPublicValid (value: any) {
84 return isBooleanValid(value)
85 }
86
87 function isUserNoModal (value: any) {
88 return isBooleanValid(value)
89 }
90
91 function isUserBlockedReasonValid (value: any) {
92 return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.BLOCKED_REASON))
93 }
94
95 function isUserRoleValid (value: any) {
96 return exists(value) && validator.isInt('' + value) && [ UserRole.ADMINISTRATOR, UserRole.MODERATOR, UserRole.USER ].includes(value)
97 }
98
99 // ---------------------------------------------------------------------------
100
101 export {
102 isUserVideosHistoryEnabledValid,
103 isUserBlockedValid,
104 isUserPasswordValid,
105 isUserPasswordValidOrEmpty,
106 isUserVideoLanguages,
107 isUserBlockedReasonValid,
108 isUserRoleValid,
109 isUserVideoQuotaValid,
110 isUserVideoQuotaDailyValid,
111 isUserUsernameValid,
112 isUserAdminFlagsValid,
113 isUserEmailVerifiedValid,
114 isUserNSFWPolicyValid,
115 isUserP2PEnabledValid,
116 isUserAutoPlayVideoValid,
117 isUserAutoPlayNextVideoValid,
118 isUserAutoPlayNextVideoPlaylistValid,
119 isUserDisplayNameValid,
120 isUserDescriptionValid,
121 isUserEmailPublicValid,
122 isUserNoModal
123 }