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