X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fusers.ts;h=89105691267786772d361956f22e92acbff9d327;hb=0c237b19fdf9c614293c1442f0ab95a81ce05735;hp=dcc4ef196b7444cef4f322deb7c8a8ef286e79f4;hpb=6b738c7a31591a83fdcd9c78b6b1f98e543c378b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index dcc4ef196..891056912 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -4,7 +4,6 @@ import { extname, join } from 'path' import * as uuidv4 from 'uuid/v4' import * as RateLimit from 'express-rate-limit' import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared' -import { retryTransactionWrapper } from '../../helpers/database-utils' import { processImage } from '../../helpers/image-utils' import { logger } from '../../helpers/logger' import { getFormattedObjects } from '../../helpers/utils' @@ -16,9 +15,11 @@ import { Redis } from '../../lib/redis' import { createUserAccountAndChannel } from '../../lib/user' import { asyncMiddleware, + asyncRetryTransactionMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, + ensureUserRegistrationAllowedForIP, paginationValidator, setDefaultPagination, setDefaultSort, @@ -44,6 +45,7 @@ import { OAuthTokenModel } from '../../models/oauth/oauth-token' import { VideoModel } from '../../models/video/video' import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type' import { createReqFiles } from '../../helpers/express-utils' +import { UserVideoQuota } from '../../../shared/models/users/user-video-quota.model' const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR }) const loginRateLimiter = new RateLimit({ @@ -100,13 +102,14 @@ usersRouter.post('/', authenticate, ensureUserHasRight(UserRight.MANAGE_USERS), asyncMiddleware(usersAddValidator), - asyncMiddleware(createUserRetryWrapper) + asyncRetryTransactionMiddleware(createUser) ) usersRouter.post('/register', asyncMiddleware(ensureUserRegistrationAllowed), + ensureUserRegistrationAllowedForIP, asyncMiddleware(usersRegisterValidator), - asyncMiddleware(registerUserRetryWrapper) + asyncRetryTransactionMiddleware(registerUser) ) usersRouter.put('/me', @@ -163,7 +166,7 @@ export { async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) { const user = res.locals.oauth.token.User as UserModel - const resultList = await VideoModel.listAccountVideosForApi( + const resultList = await VideoModel.listUserVideosForApi( user.Account.id, req.query.start as number, req.query.count as number, @@ -171,29 +174,15 @@ async function getUserVideos (req: express.Request, res: express.Response, next: false // Display my NSFW videos ) - return res.json(getFormattedObjects(resultList.data, resultList.total)) -} - -async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { - const options = { - arguments: [ req ], - errorMessage: 'Cannot insert the user with many retries.' + const additionalAttributes = { + waitTranscoding: true, + state: true, + scheduledUpdate: true } - - const { user, account } = await retryTransactionWrapper(createUser, options) - - return res.json({ - user: { - id: user.id, - account: { - id: account.id, - uuid: account.Actor.uuid - } - } - }).end() + return res.json(getFormattedObjects(resultList.data, resultList.total, { additionalAttributes })) } -async function createUser (req: express.Request) { +async function createUser (req: express.Request, res: express.Response) { const body: UserCreate = req.body const userToCreate = new UserModel({ username: body.username, @@ -209,21 +198,18 @@ async function createUser (req: express.Request) { logger.info('User %s with its channel and account created.', body.username) - return { user, account } -} - -async function registerUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) { - const options = { - arguments: [ req ], - errorMessage: 'Cannot insert the user with many retries.' - } - - await retryTransactionWrapper(registerUser, options) - - return res.type('json').status(204).end() + return res.json({ + user: { + id: user.id, + account: { + id: account.id, + uuid: account.Actor.uuid + } + } + }).end() } -async function registerUser (req: express.Request) { +async function registerUser (req: express.Request, res: express.Response) { const body: UserCreate = req.body const user = new UserModel({ @@ -239,6 +225,8 @@ async function registerUser (req: express.Request) { await createUserAccountAndChannel(user) logger.info('User %s with its channel and account registered.', body.username) + + return res.type('json').status(204).end() } async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) { @@ -253,9 +241,10 @@ async function getUserVideoQuotaUsed (req: express.Request, res: express.Respons const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username) const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user) - return res.json({ + const data: UserVideoQuota = { videoQuotaUsed - }) + } + return res.json(data) } function getUser (req: express.Request, res: express.Response, next: express.NextFunction) { @@ -303,6 +292,7 @@ async function updateMe (req: express.Request, res: express.Response, next: expr await sequelizeTypescript.transaction(async t => { await user.save({ transaction: t }) + if (body.displayName !== undefined) user.Account.name = body.displayName if (body.description !== undefined) user.Account.description = body.description await user.Account.save({ transaction: t }) @@ -313,7 +303,7 @@ async function updateMe (req: express.Request, res: express.Response, next: expr } async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) { - const avatarPhysicalFile = req.files['avatarfile'][0] + const avatarPhysicalFile = req.files[ 'avatarfile' ][ 0 ] const user = res.locals.oauth.token.user const actor = user.Account.Actor