aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-04 10:57:36 +0100
committerChocobozzz <me@florianbigard.com>2022-03-04 10:57:36 +0100
commitd3d3deaa7aae49d6d949aebcba015af75d292ccc (patch)
tree42e487810f5b7362c28f744371d160a4effc7566 /server/controllers/api/users
parent90370f7cc85ee4b45eb0d1bafed2d9d68610df9f (diff)
downloadPeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.gz
PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.zst
PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.zip
Refactor user build and express file middlewares
Diffstat (limited to 'server/controllers/api/users')
-rw-r--r--server/controllers/api/users/index.ts37
-rw-r--r--server/controllers/api/users/me.ts2
2 files changed, 13 insertions, 26 deletions
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts
index 7efc3a137..8a06bfe93 100644
--- a/server/controllers/api/users/index.ts
+++ b/server/controllers/api/users/index.ts
@@ -3,8 +3,9 @@ import RateLimit from 'express-rate-limit'
3import { tokensRouter } from '@server/controllers/api/users/token' 3import { tokensRouter } from '@server/controllers/api/users/token'
4import { Hooks } from '@server/lib/plugins/hooks' 4import { Hooks } from '@server/lib/plugins/hooks'
5import { OAuthTokenModel } from '@server/models/oauth/oauth-token' 5import { OAuthTokenModel } from '@server/models/oauth/oauth-token'
6import { MUser, MUserAccountDefault } from '@server/types/models' 6import { MUserAccountDefault } from '@server/types/models'
7import { HttpStatusCode, UserAdminFlag, UserCreate, UserCreateResult, UserRegister, UserRight, UserRole, UserUpdate } from '@shared/models' 7import { pick } from '@shared/core-utils'
8import { HttpStatusCode, UserCreate, UserCreateResult, UserRegister, UserRight, UserUpdate } from '@shared/models'
8import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' 9import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger'
9import { logger } from '../../../helpers/logger' 10import { logger } from '../../../helpers/logger'
10import { generateRandomString, getFormattedObjects } from '../../../helpers/utils' 11import { generateRandomString, getFormattedObjects } from '../../../helpers/utils'
@@ -14,7 +15,7 @@ import { sequelizeTypescript } from '../../../initializers/database'
14import { Emailer } from '../../../lib/emailer' 15import { Emailer } from '../../../lib/emailer'
15import { Notifier } from '../../../lib/notifier' 16import { Notifier } from '../../../lib/notifier'
16import { Redis } from '../../../lib/redis' 17import { Redis } from '../../../lib/redis'
17import { createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user' 18import { buildUser, createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user'
18import { 19import {
19 asyncMiddleware, 20 asyncMiddleware,
20 asyncRetryTransactionMiddleware, 21 asyncRetryTransactionMiddleware,
@@ -175,18 +176,11 @@ export {
175async function createUser (req: express.Request, res: express.Response) { 176async function createUser (req: express.Request, res: express.Response) {
176 const body: UserCreate = req.body 177 const body: UserCreate = req.body
177 178
178 const userToCreate = new UserModel({ 179 const userToCreate = buildUser({
179 username: body.username, 180 ...pick(body, [ 'username', 'password', 'email', 'role', 'videoQuota', 'videoQuotaDaily', 'adminFlags' ]),
180 password: body.password, 181
181 email: body.email, 182 emailVerified: null
182 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, 183 })
183 p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
184 autoPlayVideo: true,
185 role: body.role,
186 videoQuota: body.videoQuota,
187 videoQuotaDaily: body.videoQuotaDaily,
188 adminFlags: body.adminFlags || UserAdminFlag.NONE
189 }) as MUser
190 184
191 // NB: due to the validator usersAddValidator, password==='' can only be true if we can send the mail. 185 // NB: due to the validator usersAddValidator, password==='' can only be true if we can send the mail.
192 const createPassword = userToCreate.password === '' 186 const createPassword = userToCreate.password === ''
@@ -225,16 +219,9 @@ async function createUser (req: express.Request, res: express.Response) {
225async function registerUser (req: express.Request, res: express.Response) { 219async function registerUser (req: express.Request, res: express.Response) {
226 const body: UserRegister = req.body 220 const body: UserRegister = req.body
227 221
228 const userToCreate = new UserModel({ 222 const userToCreate = buildUser({
229 username: body.username, 223 ...pick(body, [ 'username', 'password', 'email' ]),
230 password: body.password, 224
231 email: body.email,
232 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
233 p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED,
234 autoPlayVideo: true,
235 role: UserRole.USER,
236 videoQuota: CONFIG.USER.VIDEO_QUOTA,
237 videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY,
238 emailVerified: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION ? false : null 225 emailVerified: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION ? false : null
239 }) 226 })
240 227
diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts
index a1d621152..595abcf95 100644
--- a/server/controllers/api/users/me.ts
+++ b/server/controllers/api/users/me.ts
@@ -35,7 +35,7 @@ import { VideoImportModel } from '../../../models/video/video-import'
35 35
36const auditLogger = auditLoggerFactory('users') 36const auditLogger = auditLoggerFactory('users')
37 37
38const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) 38const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT)
39 39
40const meRouter = express.Router() 40const meRouter = express.Router()
41 41