From d3d3deaa7aae49d6d949aebcba015af75d292ccc Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 4 Mar 2022 10:57:36 +0100 Subject: Refactor user build and express file middlewares --- server/controllers/api/users/index.ts | 37 ++++++++++++----------------------- server/controllers/api/users/me.ts | 2 +- 2 files changed, 13 insertions(+), 26 deletions(-) (limited to 'server/controllers/api/users') 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' import { tokensRouter } from '@server/controllers/api/users/token' import { Hooks } from '@server/lib/plugins/hooks' import { OAuthTokenModel } from '@server/models/oauth/oauth-token' -import { MUser, MUserAccountDefault } from '@server/types/models' -import { HttpStatusCode, UserAdminFlag, UserCreate, UserCreateResult, UserRegister, UserRight, UserRole, UserUpdate } from '@shared/models' +import { MUserAccountDefault } from '@server/types/models' +import { pick } from '@shared/core-utils' +import { HttpStatusCode, UserCreate, UserCreateResult, UserRegister, UserRight, UserUpdate } from '@shared/models' import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' import { logger } from '../../../helpers/logger' import { generateRandomString, getFormattedObjects } from '../../../helpers/utils' @@ -14,7 +15,7 @@ import { sequelizeTypescript } from '../../../initializers/database' import { Emailer } from '../../../lib/emailer' import { Notifier } from '../../../lib/notifier' import { Redis } from '../../../lib/redis' -import { createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user' +import { buildUser, createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user' import { asyncMiddleware, asyncRetryTransactionMiddleware, @@ -175,18 +176,11 @@ export { async function createUser (req: express.Request, res: express.Response) { const body: UserCreate = req.body - const userToCreate = new UserModel({ - username: body.username, - password: body.password, - email: body.email, - nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, - p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, - autoPlayVideo: true, - role: body.role, - videoQuota: body.videoQuota, - videoQuotaDaily: body.videoQuotaDaily, - adminFlags: body.adminFlags || UserAdminFlag.NONE - }) as MUser + const userToCreate = buildUser({ + ...pick(body, [ 'username', 'password', 'email', 'role', 'videoQuota', 'videoQuotaDaily', 'adminFlags' ]), + + emailVerified: null + }) // NB: due to the validator usersAddValidator, password==='' can only be true if we can send the mail. const createPassword = userToCreate.password === '' @@ -225,16 +219,9 @@ async function createUser (req: express.Request, res: express.Response) { async function registerUser (req: express.Request, res: express.Response) { const body: UserRegister = req.body - const userToCreate = new UserModel({ - username: body.username, - password: body.password, - email: body.email, - nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, - p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, - autoPlayVideo: true, - role: UserRole.USER, - videoQuota: CONFIG.USER.VIDEO_QUOTA, - videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY, + const userToCreate = buildUser({ + ...pick(body, [ 'username', 'password', 'email' ]), + emailVerified: CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION ? false : null }) 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' const auditLogger = auditLoggerFactory('users') -const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.TMP_DIR }) +const reqAvatarFile = createReqFiles([ 'avatarfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) const meRouter = express.Router() -- cgit v1.2.3