]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/users.ts
emit more specific status codes on video upload (#3423)
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / users.ts
CommitLineData
7cde3b9c 1import validator from 'validator'
2b4dd7e2 2import { UserRole } from '../../../shared'
74dc3bca 3import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants'
3caf77d3 4import { exists, isArray, isBooleanValid, isFileValid } from './misc'
0883b324 5import { values } from 'lodash'
4c1c1709 6import { isEmailEnabled } from '../../initializers/config'
ee9e7b61 7
65fcc311 8const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
e4c55619 9
69818c93 10function isUserPasswordValid (value: string) {
9bd26629
C
11 return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
12}
13
45f1bd72
JL
14function isUserPasswordValidOrEmpty (value: string) {
15 // Empty password is only possible if emailing is enabled.
4c1c1709
C
16 if (value === '') return isEmailEnabled()
17
45f1bd72
JL
18 return isUserPasswordValid(value)
19}
20
b0f9f39e
C
21function isUserVideoQuotaValid (value: string) {
22 return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA)
23}
24
bee0abff
FA
25function isUserVideoQuotaDailyValid (value: string) {
26 return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA_DAILY)
27}
28
69818c93 29function isUserUsernameValid (value: string) {
9bd26629
C
30 const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
31 const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
563d032e 32 return exists(value) && validator.matches(value, new RegExp(`^[a-z0-9._]{${min},${max}}$`))
e4c55619
C
33}
34
ed56ad11
C
35function isUserDisplayNameValid (value: string) {
36 return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.NAME))
37}
38
2422c46b
C
39function isUserDescriptionValid (value: string) {
40 return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.DESCRIPTION))
41}
42
d9eaee39
JM
43function isUserEmailVerifiedValid (value: any) {
44 return isBooleanValid(value)
45}
46
0883b324
C
47const nsfwPolicies = values(NSFW_POLICY_TYPES)
48function isUserNSFWPolicyValid (value: any) {
bdd428a6 49 return exists(value) && nsfwPolicies.includes(value)
7efe153b
AL
50}
51
ed638e53
RK
52function isUserWebTorrentEnabledValid (value: any) {
53 return isBooleanValid(value)
64cc5e85
RK
54}
55
8b9a525a
C
56function isUserVideosHistoryEnabledValid (value: any) {
57 return isBooleanValid(value)
58}
59
7efe153b 60function isUserAutoPlayVideoValid (value: any) {
e6921918
C
61 return isBooleanValid(value)
62}
63
3caf77d3
C
64function isUserVideoLanguages (value: any) {
65 return value === null || (isArray(value) && value.length < CONSTRAINTS_FIELDS.USERS.VIDEO_LANGUAGES.max)
66}
67
1eddc9a7
C
68function isUserAdminFlagsValid (value: any) {
69 return exists(value) && validator.isInt('' + value)
70}
71
e6921918
C
72function isUserBlockedValid (value: any) {
73 return isBooleanValid(value)
7efe153b
AL
74}
75
6aa54148
L
76function isUserAutoPlayNextVideoValid (value: any) {
77 return isBooleanValid(value)
78}
79
bee29df8
RK
80function isUserAutoPlayNextVideoPlaylistValid (value: any) {
81 return isBooleanValid(value)
82}
83
43d0ea7f
C
84function isNoInstanceConfigWarningModal (value: any) {
85 return isBooleanValid(value)
86}
87
88function isNoWelcomeModal (value: any) {
89 return isBooleanValid(value)
90}
91
eacb25c4
C
92function isUserBlockedReasonValid (value: any) {
93 return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.BLOCKED_REASON))
94}
95
954605a8
C
96function isUserRoleValid (value: any) {
97 return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined
98}
99
ac81d1a0
C
100const avatarMimeTypes = CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
101 .map(v => v.replace('.', ''))
102 .join('|')
103const avatarMimeTypesRegex = `image/(${avatarMimeTypes})`
c5911fd3 104function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
0c237b19 105 return isFileValid(files, avatarMimeTypesRegex, 'avatarfile', CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max)
c5911fd3
C
106}
107
e4c55619
C
108// ---------------------------------------------------------------------------
109
65fcc311 110export {
8b9a525a 111 isUserVideosHistoryEnabledValid,
e6921918 112 isUserBlockedValid,
65fcc311 113 isUserPasswordValid,
45f1bd72 114 isUserPasswordValidOrEmpty,
3caf77d3 115 isUserVideoLanguages,
eacb25c4 116 isUserBlockedReasonValid,
65fcc311 117 isUserRoleValid,
b0f9f39e 118 isUserVideoQuotaValid,
bee0abff 119 isUserVideoQuotaDailyValid,
65fcc311 120 isUserUsernameValid,
1eddc9a7 121 isUserAdminFlagsValid,
d9eaee39 122 isUserEmailVerifiedValid,
0883b324 123 isUserNSFWPolicyValid,
ed638e53 124 isUserWebTorrentEnabledValid,
c5911fd3 125 isUserAutoPlayVideoValid,
6aa54148 126 isUserAutoPlayNextVideoValid,
bee29df8 127 isUserAutoPlayNextVideoPlaylistValid,
ed56ad11 128 isUserDisplayNameValid,
2422c46b 129 isUserDescriptionValid,
43d0ea7f
C
130 isNoInstanceConfigWarningModal,
131 isNoWelcomeModal,
c5911fd3 132 isAvatarFile
65fcc311 133}