]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/users.js
Format video blacklist
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.js
index 6cd0e84f7861046b999dea563bc6eaaabefdff9a..c7fe7bf85fa12f2639fb79a5f369e33e70634f24 100644 (file)
@@ -18,7 +18,16 @@ const validatorsUsers = middlewares.validators.users
 
 const router = express.Router()
 
-router.get('/me', oAuth.authenticate, getUserInformation)
+router.get('/me',
+  oAuth.authenticate,
+  getUserInformation
+)
+
+router.get('/me/videos/:videoId/rating',
+  oAuth.authenticate,
+  validatorsUsers.usersVideoRating,
+  getUserVideoRating
+)
 
 router.get('/',
   validatorsPagination.pagination,
@@ -35,6 +44,12 @@ router.post('/',
   createUser
 )
 
+router.post('/register',
+  ensureRegistrationEnabled,
+  validatorsUsers.usersAdd,
+  createUser
+)
+
 router.put('/:id',
   oAuth.authenticate,
   validatorsUsers.usersUpdate,
@@ -57,10 +72,22 @@ module.exports = router
 
 // ---------------------------------------------------------------------------
 
+function ensureRegistrationEnabled (req, res, next) {
+  const registrationEnabled = constants.CONFIG.SIGNUP.ENABLED
+
+  if (registrationEnabled === true) {
+    return next()
+  }
+
+  return res.status(400).send('User registration is not enabled.')
+}
+
 function createUser (req, res, next) {
   const user = db.User.build({
     username: req.body.username,
     password: req.body.password,
+    email: req.body.email,
+    displayNSFW: false,
     role: constants.USER_ROLES.USER
   })
 
@@ -79,6 +106,22 @@ function getUserInformation (req, res, next) {
   })
 }
 
+function getUserVideoRating (req, res, next) {
+  const videoId = req.params.videoId
+  const userId = res.locals.oauth.token.User.id
+
+  db.UserVideoRate.load(userId, videoId, function (err, ratingObj) {
+    if (err) return next(err)
+
+    const rating = ratingObj ? ratingObj.type : 'none'
+
+    res.json({
+      videoId,
+      rating
+    })
+  })
+}
+
 function listUsers (req, res, next) {
   db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
     if (err) return next(err)
@@ -110,7 +153,9 @@ function updateUser (req, res, next) {
   db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
     if (err) return next(err)
 
-    user.password = req.body.password
+    if (req.body.password) user.password = req.body.password
+    if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW
+
     user.save().asCallback(function (err) {
       if (err) return next(err)