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