]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/users.ts
Add video channel tests
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
9457bf88 2
e02643f3 3import { database as db } from '../../initializers/database'
b0f9f39e 4import { USER_ROLES, CONFIG } from '../../initializers'
72c7248b 5import { logger, getFormattedObjects, retryTransactionWrapper } from '../../helpers'
65fcc311
C
6import {
7 authenticate,
8 ensureIsAdmin,
291e8d3e 9 ensureUserRegistrationAllowed,
65fcc311 10 usersAddValidator,
77a5501f 11 usersRegisterValidator,
65fcc311 12 usersUpdateValidator,
8094a898 13 usersUpdateMeValidator,
65fcc311
C
14 usersRemoveValidator,
15 usersVideoRatingValidator,
8094a898 16 usersGetValidator,
65fcc311
C
17 paginationValidator,
18 setPagination,
19 usersSortValidator,
20 setUsersSort,
21 token
22} from '../../middlewares'
8094a898
C
23import {
24 UserVideoRate as FormattedUserVideoRate,
25 UserCreate,
26 UserUpdate,
27 UserUpdateMe
28} from '../../../shared'
72c7248b 29import { createUserAuthorAndChannel } from '../../lib'
77a5501f 30import { UserInstance } from '../../models'
65fcc311
C
31
32const usersRouter = express.Router()
33
34usersRouter.get('/me',
35 authenticate,
d38b8281
C
36 getUserInformation
37)
38
65fcc311
C
39usersRouter.get('/me/videos/:videoId/rating',
40 authenticate,
41 usersVideoRatingValidator,
d38b8281
C
42 getUserVideoRating
43)
9bd26629 44
65fcc311
C
45usersRouter.get('/',
46 paginationValidator,
47 usersSortValidator,
48 setUsersSort,
49 setPagination,
5c39adb7
C
50 listUsers
51)
52
8094a898
C
53usersRouter.get('/:id',
54 usersGetValidator,
55 getUser
56)
57
65fcc311
C
58usersRouter.post('/',
59 authenticate,
60 ensureIsAdmin,
61 usersAddValidator,
72c7248b 62 createUserRetryWrapper
9bd26629
C
63)
64
65fcc311 65usersRouter.post('/register',
291e8d3e 66 ensureUserRegistrationAllowed,
77a5501f
C
67 usersRegisterValidator,
68 registerUser
2c2e9092
C
69)
70
8094a898
C
71usersRouter.put('/me',
72 authenticate,
73 usersUpdateMeValidator,
74 updateMe
75)
76
65fcc311
C
77usersRouter.put('/:id',
78 authenticate,
8094a898 79 ensureIsAdmin,
65fcc311 80 usersUpdateValidator,
9bd26629
C
81 updateUser
82)
83
65fcc311
C
84usersRouter.delete('/:id',
85 authenticate,
86 ensureIsAdmin,
87 usersRemoveValidator,
9bd26629
C
88 removeUser
89)
6606150c 90
65fcc311 91usersRouter.post('/token', token, success)
9bd26629 92// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
9457bf88
C
93
94// ---------------------------------------------------------------------------
95
65fcc311
C
96export {
97 usersRouter
98}
9457bf88
C
99
100// ---------------------------------------------------------------------------
101
72c7248b
C
102function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
103 const options = {
104 arguments: [ req, res ],
105 errorMessage: 'Cannot insert the user with many retries.'
106 }
107
108 retryTransactionWrapper(createUser, options)
109 .then(() => {
110 // TODO : include Location of the new user -> 201
111 res.type('json').status(204).end()
112 })
113 .catch(err => next(err))
114}
115
69818c93 116function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
4771e000 117 const body: UserCreate = req.body
feb4bdfd 118 const user = db.User.build({
4771e000
C
119 username: body.username,
120 password: body.password,
121 email: body.email,
1d49e1e2 122 displayNSFW: false,
b0f9f39e
C
123 role: USER_ROLES.USER,
124 videoQuota: body.videoQuota
9bd26629
C
125 })
126
72c7248b
C
127 return createUserAuthorAndChannel(user)
128 .then(() => logger.info('User %s with its channel and author created.', body.username))
129 .catch((err: Error) => {
130 logger.debug('Cannot insert the user.', err)
131 throw err
132 })
9bd26629
C
133}
134
77a5501f
C
135function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) {
136 const body: UserCreate = req.body
137
138 const user = db.User.build({
139 username: body.username,
140 password: body.password,
141 email: body.email,
142 displayNSFW: false,
143 role: USER_ROLES.USER,
144 videoQuota: CONFIG.USER.VIDEO_QUOTA
145 })
146
72c7248b 147 return createUserAuthorAndChannel(user)
77a5501f
C
148 .then(() => res.type('json').status(204).end())
149 .catch(err => next(err))
150}
151
69818c93 152function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
72c7248b 153 db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
0aef76c4 154 .then(user => res.json(user.toFormattedJSON()))
6fcd19ba 155 .catch(err => next(err))
99a64bfe
C
156}
157
8094a898
C
158function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
159 return res.json(res.locals.user.toFormattedJSON())
160}
161
69818c93 162function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
0a6658fd 163 const videoId = +req.params.videoId
69818c93 164 const userId = +res.locals.oauth.token.User.id
d38b8281 165
6fcd19ba
C
166 db.UserVideoRate.load(userId, videoId, null)
167 .then(ratingObj => {
168 const rating = ratingObj ? ratingObj.type : 'none'
0aef76c4 169 const json: FormattedUserVideoRate = {
6fcd19ba
C
170 videoId,
171 rating
172 }
173 res.json(json)
174 })
175 .catch(err => next(err))
d38b8281
C
176}
177
69818c93 178function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
6fcd19ba
C
179 db.User.listForApi(req.query.start, req.query.count, req.query.sort)
180 .then(resultList => {
0aef76c4 181 res.json(getFormattedObjects(resultList.data, resultList.total))
6fcd19ba
C
182 })
183 .catch(err => next(err))
9bd26629
C
184}
185
69818c93 186function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
6fcd19ba
C
187 db.User.loadById(req.params.id)
188 .then(user => user.destroy())
189 .then(() => res.sendStatus(204))
190 .catch(err => {
ad0997ad 191 logger.error('Errors when removed the user.', err)
9bd26629 192 return next(err)
6fcd19ba 193 })
9bd26629
C
194}
195
8094a898
C
196function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
197 const body: UserUpdateMe = req.body
4771e000 198
8094a898 199 // FIXME: user is not already a Sequelize instance?
6fcd19ba
C
200 db.User.loadByUsername(res.locals.oauth.token.user.username)
201 .then(user => {
8094a898
C
202 if (body.password !== undefined) user.password = body.password
203 if (body.email !== undefined) user.email = body.email
4771e000 204 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
1d49e1e2 205
6fcd19ba 206 return user.save()
9bd26629 207 })
6fcd19ba
C
208 .then(() => res.sendStatus(204))
209 .catch(err => next(err))
9bd26629
C
210}
211
8094a898
C
212function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
213 const body: UserUpdate = req.body
77a5501f 214 const user: UserInstance = res.locals.user
8094a898
C
215
216 if (body.email !== undefined) user.email = body.email
217 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
218
219 return user.save()
220 .then(() => res.sendStatus(204))
221 .catch(err => next(err))
222}
223
69818c93 224function success (req: express.Request, res: express.Response, next: express.NextFunction) {
9457bf88
C
225 res.end()
226}