1 import * as express from 'express'
2 import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
3 import { getFormattedObjects, logger, retryTransactionWrapper } from '../../helpers'
4 import { CONFIG } from '../../initializers'
5 import { createUserAccountAndChannel } from '../../lib/user'
10 ensureUserRegistrationAllowed,
18 usersRegisterValidator,
21 usersUpdateMeValidator,
23 usersVideoRatingValidator
24 } from '../../middlewares'
25 import { videosSortValidator } from '../../middlewares/validators'
26 import { AccountVideoRateModel } from '../../models/account/account-video-rate'
27 import { UserModel } from '../../models/account/user'
28 import { VideoModel } from '../../models/video/video'
30 const usersRouter = express.Router()
32 usersRouter.get('/me',
34 asyncMiddleware(getUserInformation)
37 usersRouter.get('/me/videos',
43 asyncMiddleware(getUserVideos)
46 usersRouter.get('/me/videos/:videoId/rating',
48 asyncMiddleware(usersVideoRatingValidator),
49 asyncMiddleware(getUserVideoRating)
54 ensureUserHasRight(UserRight.MANAGE_USERS),
59 asyncMiddleware(listUsers)
62 usersRouter.get('/:id',
63 asyncMiddleware(usersGetValidator),
69 ensureUserHasRight(UserRight.MANAGE_USERS),
70 asyncMiddleware(usersAddValidator),
71 asyncMiddleware(createUserRetryWrapper)
74 usersRouter.post('/register',
75 asyncMiddleware(ensureUserRegistrationAllowed),
76 asyncMiddleware(usersRegisterValidator),
77 asyncMiddleware(registerUserRetryWrapper)
80 usersRouter.put('/me',
82 usersUpdateMeValidator,
83 asyncMiddleware(updateMe)
86 usersRouter.put('/:id',
88 ensureUserHasRight(UserRight.MANAGE_USERS),
89 asyncMiddleware(usersUpdateValidator),
90 asyncMiddleware(updateUser)
93 usersRouter.delete('/:id',
95 ensureUserHasRight(UserRight.MANAGE_USERS),
96 asyncMiddleware(usersRemoveValidator),
97 asyncMiddleware(removeUser)
100 usersRouter.post('/token', token, success)
101 // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
103 // ---------------------------------------------------------------------------
109 // ---------------------------------------------------------------------------
111 async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
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)
115 return res.json(getFormattedObjects(resultList.data, resultList.total))
118 async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
121 errorMessage: 'Cannot insert the user with many retries.'
124 await retryTransactionWrapper(createUser, options)
126 // TODO : include Location of the new user -> 201
127 return res.type('json').status(204).end()
130 async function createUser (req: express.Request) {
131 const body: UserCreate = req.body
132 const user = new UserModel({
133 username: body.username,
134 password: body.password,
139 videoQuota: body.videoQuota
142 await createUserAccountAndChannel(user)
144 logger.info('User %s with its channel and account created.', body.username)
147 async function registerUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
150 errorMessage: 'Cannot insert the user with many retries.'
153 await retryTransactionWrapper(registerUser, options)
155 return res.type('json').status(204).end()
158 async function registerUser (req: express.Request) {
159 const body: UserCreate = req.body
161 const user = new UserModel({
162 username: body.username,
163 password: body.password,
168 videoQuota: CONFIG.USER.VIDEO_QUOTA
171 await createUserAccountAndChannel(user)
173 logger.info('User %s with its channel and account registered.', body.username)
176 async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
177 // We did not load channels in res.locals.user
178 const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
180 return res.json(user.toFormattedJSON())
183 function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
184 return res.json(res.locals.user.toFormattedJSON())
187 async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
188 const videoId = +req.params.videoId
189 const accountId = +res.locals.oauth.token.User.Account.id
191 const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null)
192 const rating = ratingObj ? ratingObj.type : 'none'
194 const json: FormattedUserVideoRate = {
201 async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
202 const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort)
204 return res.json(getFormattedObjects(resultList.data, resultList.total))
207 async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
208 const user = await UserModel.loadById(req.params.id)
212 return res.sendStatus(204)
215 async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
216 const body: UserUpdateMe = req.body
218 // FIXME: user is not already a Sequelize instance?
219 const user = res.locals.oauth.token.user
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
224 if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
228 return res.sendStatus(204)
231 async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
232 const body: UserUpdate = req.body
233 const user = res.locals.user as UserModel
235 if (body.email !== undefined) user.email = body.email
236 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
237 if (body.role !== undefined) user.role = body.role
241 return res.sendStatus(204)
244 function success (req: express.Request, res: express.Response, next: express.NextFunction) {