]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/users.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.ts
index 1b5b7f9031db14cf9bdb98f783f6ffba7e652aea..1ecaaf93f7a04c60dd0180b46be92933dab0f891 100644 (file)
@@ -8,16 +8,25 @@ import {
   ensureIsAdmin,
   ensureUserRegistrationAllowed,
   usersAddValidator,
+  usersRegisterValidator,
   usersUpdateValidator,
+  usersUpdateMeValidator,
   usersRemoveValidator,
   usersVideoRatingValidator,
+  usersGetValidator,
   paginationValidator,
   setPagination,
   usersSortValidator,
   setUsersSort,
   token
 } from '../../middlewares'
-import { UserVideoRate as FormattedUserVideoRate, UserCreate, UserUpdate } from '../../../shared'
+import {
+  UserVideoRate as FormattedUserVideoRate,
+  UserCreate,
+  UserUpdate,
+  UserUpdateMe
+} from '../../../shared'
+import { UserInstance } from '../../models'
 
 const usersRouter = express.Router()
 
@@ -40,6 +49,11 @@ usersRouter.get('/',
   listUsers
 )
 
+usersRouter.get('/:id',
+  usersGetValidator,
+  getUser
+)
+
 usersRouter.post('/',
   authenticate,
   ensureIsAdmin,
@@ -49,12 +63,19 @@ usersRouter.post('/',
 
 usersRouter.post('/register',
   ensureUserRegistrationAllowed,
-  usersAddValidator,
-  createUser
+  usersRegisterValidator,
+  registerUser
+)
+
+usersRouter.put('/me',
+  authenticate,
+  usersUpdateMeValidator,
+  updateMe
 )
 
 usersRouter.put('/:id',
   authenticate,
+  ensureIsAdmin,
   usersUpdateValidator,
   updateUser
 )
@@ -80,11 +101,6 @@ export {
 function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
   const body: UserCreate = req.body
 
-  // On registration, we set the user video quota
-  if (body.videoQuota === undefined) {
-    body.videoQuota = CONFIG.USER.VIDEO_QUOTA
-  }
-
   const user = db.User.build({
     username: body.username,
     password: body.password,
@@ -99,12 +115,33 @@ function createUser (req: express.Request, res: express.Response, next: express.
     .catch(err => next(err))
 }
 
+function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const body: UserCreate = req.body
+
+  const user = db.User.build({
+    username: body.username,
+    password: body.password,
+    email: body.email,
+    displayNSFW: false,
+    role: USER_ROLES.USER,
+    videoQuota: CONFIG.USER.VIDEO_QUOTA
+  })
+
+  user.save()
+    .then(() => res.type('json').status(204).end())
+    .catch(err => next(err))
+}
+
 function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
   db.User.loadByUsername(res.locals.oauth.token.user.username)
     .then(user => res.json(user.toFormattedJSON()))
     .catch(err => next(err))
 }
 
+function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
+  return res.json(res.locals.user.toFormattedJSON())
+}
+
 function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
   const videoId = +req.params.videoId
   const userId = +res.locals.oauth.token.User.id
@@ -139,14 +176,15 @@ function removeUser (req: express.Request, res: express.Response, next: express.
     })
 }
 
-function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const body: UserUpdate = req.body
+function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const body: UserUpdateMe = req.body
 
+  // FIXME: user is not already a Sequelize instance?
   db.User.loadByUsername(res.locals.oauth.token.user.username)
     .then(user => {
-      if (body.password) user.password = body.password
+      if (body.password !== undefined) user.password = body.password
+      if (body.email !== undefined) user.email = body.email
       if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
-      if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
 
       return user.save()
     })
@@ -154,6 +192,18 @@ function updateUser (req: express.Request, res: express.Response, next: express.
     .catch(err => next(err))
 }
 
+function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
+  const body: UserUpdate = req.body
+  const user: UserInstance = res.locals.user
+
+  if (body.email !== undefined) user.email = body.email
+  if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
+
+  return user.save()
+    .then(() => res.sendStatus(204))
+    .catch(err => next(err))
+}
+
 function success (req: express.Request, res: express.Response, next: express.NextFunction) {
   res.end()
 }