]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/users.ts
Fix images size limit
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.ts
index 2342c23dd683b809ba9db028907033cb382ed04d..89105691267786772d361956f22e92acbff9d327 100644 (file)
@@ -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) {
@@ -314,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