2
1

users.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import 'express-validator'
  2. import * as validator from 'validator'
  3. import { UserRole } from '../../../shared'
  4. import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers/constants'
  5. import { exists, isArray, isBooleanValid, isFileValid } from './misc'
  6. import { values } from 'lodash'
  7. const USERS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.USERS
  8. function isUserPasswordValid (value: string) {
  9. return validator.isLength(value, USERS_CONSTRAINTS_FIELDS.PASSWORD)
  10. }
  11. function isUserVideoQuotaValid (value: string) {
  12. return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA)
  13. }
  14. function isUserVideoQuotaDailyValid (value: string) {
  15. return exists(value) && validator.isInt(value + '', USERS_CONSTRAINTS_FIELDS.VIDEO_QUOTA_DAILY)
  16. }
  17. function isUserUsernameValid (value: string) {
  18. const max = USERS_CONSTRAINTS_FIELDS.USERNAME.max
  19. const min = USERS_CONSTRAINTS_FIELDS.USERNAME.min
  20. return exists(value) && validator.matches(value, new RegExp(`^[a-z0-9._]{${min},${max}}$`))
  21. }
  22. function isUserDisplayNameValid (value: string) {
  23. return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.NAME))
  24. }
  25. function isUserDescriptionValid (value: string) {
  26. return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.DESCRIPTION))
  27. }
  28. function isUserEmailVerifiedValid (value: any) {
  29. return isBooleanValid(value)
  30. }
  31. const nsfwPolicies = values(NSFW_POLICY_TYPES)
  32. function isUserNSFWPolicyValid (value: any) {
  33. return exists(value) && nsfwPolicies.indexOf(value) !== -1
  34. }
  35. function isUserWebTorrentEnabledValid (value: any) {
  36. return isBooleanValid(value)
  37. }
  38. function isUserVideosHistoryEnabledValid (value: any) {
  39. return isBooleanValid(value)
  40. }
  41. function isUserAutoPlayVideoValid (value: any) {
  42. return isBooleanValid(value)
  43. }
  44. function isUserVideoLanguages (value: any) {
  45. return value === null || (isArray(value) && value.length < CONSTRAINTS_FIELDS.USERS.VIDEO_LANGUAGES.max)
  46. }
  47. function isUserAdminFlagsValid (value: any) {
  48. return exists(value) && validator.isInt('' + value)
  49. }
  50. function isUserBlockedValid (value: any) {
  51. return isBooleanValid(value)
  52. }
  53. function isUserBlockedReasonValid (value: any) {
  54. return value === null || (exists(value) && validator.isLength(value, CONSTRAINTS_FIELDS.USERS.BLOCKED_REASON))
  55. }
  56. function isUserRoleValid (value: any) {
  57. return exists(value) && validator.isInt('' + value) && UserRole[value] !== undefined
  58. }
  59. const avatarMimeTypes = CONSTRAINTS_FIELDS.ACTORS.AVATAR.EXTNAME
  60. .map(v => v.replace('.', ''))
  61. .join('|')
  62. const avatarMimeTypesRegex = `image/(${avatarMimeTypes})`
  63. function isAvatarFile (files: { [ fieldname: string ]: Express.Multer.File[] } | Express.Multer.File[]) {
  64. return isFileValid(files, avatarMimeTypesRegex, 'avatarfile', CONSTRAINTS_FIELDS.ACTORS.AVATAR.FILE_SIZE.max)
  65. }
  66. // ---------------------------------------------------------------------------
  67. export {
  68. isUserVideosHistoryEnabledValid,
  69. isUserBlockedValid,
  70. isUserPasswordValid,
  71. isUserVideoLanguages,
  72. isUserBlockedReasonValid,
  73. isUserRoleValid,
  74. isUserVideoQuotaValid,
  75. isUserVideoQuotaDailyValid,
  76. isUserUsernameValid,
  77. isUserAdminFlagsValid,
  78. isUserEmailVerifiedValid,
  79. isUserNSFWPolicyValid,
  80. isUserWebTorrentEnabledValid,
  81. isUserAutoPlayVideoValid,
  82. isUserDisplayNameValid,
  83. isUserDescriptionValid,
  84. isAvatarFile
  85. }