diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/helpers/custom-validators/users.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/helpers/custom-validators/users.ts')
-rw-r--r-- | server/helpers/custom-validators/users.ts | 123 |
1 files changed, 0 insertions, 123 deletions
diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts deleted file mode 100644 index f02b3ba65..000000000 --- a/server/helpers/custom-validators/users.ts +++ /dev/null | |||
@@ -1,123 +0,0 @@ | |||
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 | } | ||