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,
11 usersRegisterValidator,
13 usersUpdateMeValidator,
15 usersVideoRatingValidator,
22 } from '../../middlewares'
24 UserVideoRate as FormattedUserVideoRate,
28 } from '../../../shared'
29 import { UserInstance } from '../../models'
31 const usersRouter = express.Router()
33 usersRouter.get('/me',
38 usersRouter.get('/me/videos/:videoId/rating',
40 usersVideoRatingValidator,
52 usersRouter.get('/:id',
64 usersRouter.post('/register',
65 ensureUserRegistrationAllowed,
66 usersRegisterValidator,
70 usersRouter.put('/me',
72 usersUpdateMeValidator,
76 usersRouter.put('/:id',
83 usersRouter.delete('/:id',
90 usersRouter.post('/token', token, success)
91 // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
93 // ---------------------------------------------------------------------------
99 // ---------------------------------------------------------------------------
101 function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
102 const body: UserCreate = req.body
104 const user = db.User.build({
105 username: body.username,
106 password: body.password,
109 role: USER_ROLES.USER,
110 videoQuota: body.videoQuota
114 .then(() => res.type('json').status(204).end())
115 .catch(err => next(err))
118 function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) {
119 const body: UserCreate = req.body
121 const user = db.User.build({
122 username: body.username,
123 password: body.password,
126 role: USER_ROLES.USER,
127 videoQuota: CONFIG.USER.VIDEO_QUOTA
131 .then(() => res.type('json').status(204).end())
132 .catch(err => next(err))
135 function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
136 db.User.loadByUsername(res.locals.oauth.token.user.username)
137 .then(user => res.json(user.toFormattedJSON()))
138 .catch(err => next(err))
141 function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
142 return res.json(res.locals.user.toFormattedJSON())
145 function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
146 const videoId = +req.params.videoId
147 const userId = +res.locals.oauth.token.User.id
149 db.UserVideoRate.load(userId, videoId, null)
151 const rating = ratingObj ? ratingObj.type : 'none'
152 const json: FormattedUserVideoRate = {
158 .catch(err => next(err))
161 function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
162 db.User.listForApi(req.query.start, req.query.count, req.query.sort)
163 .then(resultList => {
164 res.json(getFormattedObjects(resultList.data, resultList.total))
166 .catch(err => next(err))
169 function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
170 db.User.loadById(req.params.id)
171 .then(user => user.destroy())
172 .then(() => res.sendStatus(204))
174 logger.error('Errors when removed the user.', err)
179 function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
180 const body: UserUpdateMe = req.body
182 // FIXME: user is not already a Sequelize instance?
183 db.User.loadByUsername(res.locals.oauth.token.user.username)
185 if (body.password !== undefined) user.password = body.password
186 if (body.email !== undefined) user.email = body.email
187 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
191 .then(() => res.sendStatus(204))
192 .catch(err => next(err))
195 function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
196 const body: UserUpdate = req.body
197 const user: UserInstance = res.locals.user
199 if (body.email !== undefined) user.email = body.email
200 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
203 .then(() => res.sendStatus(204))
204 .catch(err => next(err))
207 function success (req: express.Request, res: express.Response, next: express.NextFunction) {