1 import * as express from 'express'
3 import { database as db } from '../../initializers/database'
4 import { USER_ROLES, CONFIG } from '../../initializers'
5 import { logger, getFormattedObjects } from '../../helpers'
9 ensureUserRegistrationAllowed,
13 usersVideoRatingValidator,
19 } from '../../middlewares'
20 import { UserVideoRate as FormattedUserVideoRate, UserCreate, UserUpdate } from '../../../shared'
22 const usersRouter = express.Router()
24 usersRouter.get('/me',
29 usersRouter.get('/me/videos/:videoId/rating',
31 usersVideoRatingValidator,
50 usersRouter.post('/register',
51 ensureUserRegistrationAllowed,
56 usersRouter.put('/:id',
62 usersRouter.delete('/:id',
69 usersRouter.post('/token', token, success)
70 // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
72 // ---------------------------------------------------------------------------
78 // ---------------------------------------------------------------------------
80 function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
81 const body: UserCreate = req.body
83 // On registration, we set the user video quota
84 if (body.videoQuota === undefined) {
85 body.videoQuota = CONFIG.USER.VIDEO_QUOTA
88 const user = db.User.build({
89 username: body.username,
90 password: body.password,
93 role: USER_ROLES.USER,
94 videoQuota: body.videoQuota
98 .then(() => res.type('json').status(204).end())
99 .catch(err => next(err))
102 function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
103 db.User.loadByUsername(res.locals.oauth.token.user.username)
104 .then(user => res.json(user.toFormattedJSON()))
105 .catch(err => next(err))
108 function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
109 const videoId = +req.params.videoId
110 const userId = +res.locals.oauth.token.User.id
112 db.UserVideoRate.load(userId, videoId, null)
114 const rating = ratingObj ? ratingObj.type : 'none'
115 const json: FormattedUserVideoRate = {
121 .catch(err => next(err))
124 function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
125 db.User.listForApi(req.query.start, req.query.count, req.query.sort)
126 .then(resultList => {
127 res.json(getFormattedObjects(resultList.data, resultList.total))
129 .catch(err => next(err))
132 function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
133 db.User.loadById(req.params.id)
134 .then(user => user.destroy())
135 .then(() => res.sendStatus(204))
137 logger.error('Errors when removed the user.', err)
142 function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
143 const body: UserUpdate = req.body
145 db.User.loadByUsername(res.locals.oauth.token.user.username)
147 if (body.password) user.password = body.password
148 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
149 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
153 .then(() => res.sendStatus(204))
154 .catch(err => next(err))
157 function success (req: express.Request, res: express.Response, next: express.NextFunction) {