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