]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users.js
Server: Fix video propagation with transcoding enabled
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.js
CommitLineData
9457bf88
C
1'use strict'
2
0c1cbbfe 3const express = require('express')
9bd26629 4const waterfall = require('async/waterfall')
fc51fde0 5
f253b1c1 6const constants = require('../../initializers/constants')
feb4bdfd 7const db = require('../../initializers/database')
f253b1c1 8const logger = require('../../helpers/logger')
55fa55a9 9const utils = require('../../helpers/utils')
f253b1c1 10const middlewares = require('../../middlewares')
9bd26629
C
11const admin = middlewares.admin
12const oAuth = middlewares.oauth
5c39adb7
C
13const pagination = middlewares.pagination
14const sort = middlewares.sort
15const validatorsPagination = middlewares.validators.pagination
16const validatorsSort = middlewares.validators.sort
9bd26629 17const validatorsUsers = middlewares.validators.users
9457bf88 18
9457bf88
C
19const router = express.Router()
20
d38b8281
C
21router.get('/me',
22 oAuth.authenticate,
23 getUserInformation
24)
25
26router.get('/me/videos/:videoId/rating',
27 oAuth.authenticate,
28 validatorsUsers.usersVideoRating,
29 getUserVideoRating
30)
9bd26629 31
5c39adb7
C
32router.get('/',
33 validatorsPagination.pagination,
34 validatorsSort.usersSort,
35 sort.setUsersSort,
36 pagination.setPagination,
37 listUsers
38)
39
9bd26629
C
40router.post('/',
41 oAuth.authenticate,
42 admin.ensureIsAdmin,
43 validatorsUsers.usersAdd,
44 createUser
45)
46
2c2e9092
C
47router.post('/register',
48 ensureRegistrationEnabled,
49 validatorsUsers.usersAdd,
50 createUser
51)
52
9bd26629
C
53router.put('/:id',
54 oAuth.authenticate,
55 validatorsUsers.usersUpdate,
56 updateUser
57)
58
68a3b9f2 59router.delete('/:id',
9bd26629
C
60 oAuth.authenticate,
61 admin.ensureIsAdmin,
62 validatorsUsers.usersRemove,
63 removeUser
64)
6606150c 65
69b0a27c 66router.post('/token', oAuth.token, success)
9bd26629 67// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
9457bf88
C
68
69// ---------------------------------------------------------------------------
70
71module.exports = router
72
73// ---------------------------------------------------------------------------
74
2c2e9092
C
75function ensureRegistrationEnabled (req, res, next) {
76 const registrationEnabled = constants.CONFIG.SIGNUP.ENABLED
77
78 if (registrationEnabled === true) {
79 return next()
80 }
81
82 return res.status(400).send('User registration is not enabled.')
83}
84
9bd26629 85function createUser (req, res, next) {
feb4bdfd 86 const user = db.User.build({
9bd26629
C
87 username: req.body.username,
88 password: req.body.password,
ad4a8a1c 89 email: req.body.email,
1d49e1e2 90 displayNSFW: false,
9bd26629
C
91 role: constants.USER_ROLES.USER
92 })
93
feb4bdfd 94 user.save().asCallback(function (err, createdUser) {
9bd26629
C
95 if (err) return next(err)
96
97 return res.type('json').status(204).end()
98 })
99}
100
99a64bfe 101function getUserInformation (req, res, next) {
feb4bdfd 102 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
99a64bfe
C
103 if (err) return next(err)
104
105 return res.json(user.toFormatedJSON())
106 })
107}
108
d38b8281
C
109function getUserVideoRating (req, res, next) {
110 const videoId = req.params.videoId
111 const userId = res.locals.oauth.token.User.id
112
113 db.UserVideoRate.load(userId, videoId, function (err, ratingObj) {
114 if (err) return next(err)
115
116 const rating = ratingObj ? ratingObj.type : 'none'
117
118 res.json({
119 videoId,
120 rating
121 })
122 })
123}
124
9bd26629 125function listUsers (req, res, next) {
feb4bdfd 126 db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
9bd26629
C
127 if (err) return next(err)
128
55fa55a9 129 res.json(utils.getFormatedObjects(usersList, usersTotal))
9bd26629
C
130 })
131}
132
133function removeUser (req, res, next) {
134 waterfall([
98ac898a 135 function loadUser (callback) {
feb4bdfd 136 db.User.loadById(req.params.id, callback)
9bd26629
C
137 },
138
98ac898a 139 function deleteUser (user, callback) {
feb4bdfd 140 user.destroy().asCallback(callback)
9bd26629
C
141 }
142 ], function andFinally (err) {
143 if (err) {
144 logger.error('Errors when removed the user.', { error: err })
145 return next(err)
146 }
147
10431358 148 return res.sendStatus(204)
9bd26629
C
149 })
150}
151
152function updateUser (req, res, next) {
feb4bdfd 153 db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
9bd26629
C
154 if (err) return next(err)
155
1d49e1e2
C
156 if (req.body.password) user.password = req.body.password
157 if (req.body.displayNSFW !== undefined) user.displayNSFW = req.body.displayNSFW
158
feb4bdfd 159 user.save().asCallback(function (err) {
9bd26629
C
160 if (err) return next(err)
161
10431358 162 return res.sendStatus(204)
9bd26629
C
163 })
164 })
165}
166
9457bf88
C
167function success (req, res, next) {
168 res.end()
169}