aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/users.ts
blob: d6e91ad35df62351fe66720caf8c3b7b977ba030 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import validator from 'validator'
import { UserRole } from '../../../shared'
import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants'
import { exists, isArray, isBooleanValid, isFileValid } from './misc'
import { values } from 'lodash'
import { isEmailEnabled } from '../../initializers/config'

const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS

function isUserPasswordValid (value: string) {
  return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
}

function isUserPasswordValidOrEmpty (value: string) {
  // Empty password is only possible if emailing is enabled.
  if (value === '') return isEmailEnabled()

  return isUserPasswordValid(value)
}

function isUserVideoQuotaValid (value: string) {
  return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA)
}

function isUserVideoQuotaDailyValid (value: string) {
  return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA_DAILY)
}

function isUserUsernameValid (value: string) {
  const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
  const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
  return exists(value) && validator.matches(value, new RegExp(`^[a-z0-9._]{${min},${max}}$`))
}

function isUserDisplayNameValid (value: string) {
  return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.NAME))
}

function isUserDescriptionValid (value: string) {
  return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.DESCRIPTION))
}

function isUserEmailVerifiedValid (value: any) {
  return isBooleanValid(value)
}

const nsfwPolicies = values(NSFW_POLICY_TYPES)
function isUserNSFWPolicyValid (value: any) {
  return exists(value) && nsfwPolicies.includes(value)
}

function isUserWebTorrentEnabledValid (value: any) {
  return isBooleanValid(value)
}

function isUserVideosHistoryEnabledValid (value: any) {
  return isBooleanValid(value)
}

function isUserAutoPlayVideoValid (value: any) {
  return isBooleanValid(value)
}

function isUserVideoLanguages (value: any) {
  return value === null || (isArray(value) && value.length < CONSTRAINTS_FIELDS.USERS.VIDEO_LANGUAGES.max)
}

function isUserAdminFlagsValid (value: any) {
  return exists(value) && validator.isInt('' + value)
}

function isUserBlockedValid (value: any) {
  return isBooleanValid(value)
}

function isUserAutoPlayNextVideoValid (value: any) {
  return isBooleanValid(value)
}

function isUserAutoPlayNextVideoPlaylistValid (value: any) {
  return isBooleanValid(value)
}

function isNoInstanceConfigWarningModal (value: any) {
  return isBooleanValid(value)
}

function isNoWelcomeModal (value: any) {
  return isBooleanValid(value)
}

function isUserBlockedReasonValid (value: any) {
  return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.BLOCKED_REASON))
}

function isUserRoleValid (value: any) {
  return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined
}

const avatarMimeTypes = CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
  .map(v => v.replace('.', ''))
  .join('|')
const avatarMimeTypesRegex = `image/(${avatarMimeTypes})`
function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
  return isFileValid(files, avatarMimeTypesRegex, 'avatarfile', CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max)
}

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

export {
  isUserVideosHistoryEnabledValid,
  isUserBlockedValid,
  isUserPasswordValid,
  isUserPasswordValidOrEmpty,
  isUserVideoLanguages,
  isUserBlockedReasonValid,
  isUserRoleValid,
  isUserVideoQuotaValid,
  isUserVideoQuotaDailyValid,
  isUserUsernameValid,
  isUserAdminFlagsValid,
  isUserEmailVerifiedValid,
  isUserNSFWPolicyValid,
  isUserWebTorrentEnabledValid,
  isUserAutoPlayVideoValid,
  isUserAutoPlayNextVideoValid,
  isUserAutoPlayNextVideoPlaylistValid,
  isUserDisplayNameValid,
  isUserDescriptionValid,
  isNoInstanceConfigWarningModal,
  isNoWelcomeModal,
  isAvatarFile
}