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, retryTransactionWrapper } from '../../helpers'
9 ensureUserRegistrationAllowed,
11 usersRegisterValidator,
13 usersUpdateMeValidator,
15 usersVideoRatingValidator,
22 } from '../../middlewares'
24 UserVideoRate as FormattedUserVideoRate,
28 } from '../../../shared'
29 import { createUserAuthorAndChannel } from '../../lib'
30 import { UserInstance } from '../../models'
32 const usersRouter = express.Router()
34 usersRouter.get('/me',
39 usersRouter.get('/me/videos/:videoId/rating',
41 usersVideoRatingValidator,
53 usersRouter.get('/:id',
62 createUserRetryWrapper
65 usersRouter.post('/register',
66 ensureUserRegistrationAllowed,
67 usersRegisterValidator,
71 usersRouter.put('/me',
73 usersUpdateMeValidator,
77 usersRouter.put('/:id',
84 usersRouter.delete('/:id',
91 usersRouter.post('/token', token, success)
92 // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
94 // ---------------------------------------------------------------------------
100 // ---------------------------------------------------------------------------
102 function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
104 arguments: [ req, res ],
105 errorMessage: 'Cannot insert the user with many retries.'
108 retryTransactionWrapper(createUser, options)
110 // TODO : include Location of the new user -> 201
111 res.type('json').status(204).end()
113 .catch(err => next(err))
116 function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
117 const body: UserCreate = req.body
118 const user = db.User.build({
119 username: body.username,
120 password: body.password,
123 role: USER_ROLES.USER,
124 videoQuota: body.videoQuota
127 return createUserAuthorAndChannel(user)
128 .then(() => logger.info('User %s with its channel and author created.', body.username))
129 .catch((err: Error) => {
130 logger.debug('Cannot insert the user.', err)
135 function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) {
136 const body: UserCreate = req.body
138 const user = db.User.build({
139 username: body.username,
140 password: body.password,
143 role: USER_ROLES.USER,
144 videoQuota: CONFIG.USER.VIDEO_QUOTA
147 return createUserAuthorAndChannel(user)
148 .then(() => res.type('json').status(204).end())
149 .catch(err => next(err))
152 function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
153 db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
154 .then(user => res.json(user.toFormattedJSON()))
155 .catch(err => next(err))
158 function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
159 return res.json(res.locals.user.toFormattedJSON())
162 function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
163 const videoId = +req.params.videoId
164 const userId = +res.locals.oauth.token.User.id
166 db.UserVideoRate.load(userId, videoId, null)
168 const rating = ratingObj ? ratingObj.type : 'none'
169 const json: FormattedUserVideoRate = {
175 .catch(err => next(err))
178 function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
179 db.User.listForApi(req.query.start, req.query.count, req.query.sort)
180 .then(resultList => {
181 res.json(getFormattedObjects(resultList.data, resultList.total))
183 .catch(err => next(err))
186 function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
187 db.User.loadById(req.params.id)
188 .then(user => user.destroy())
189 .then(() => res.sendStatus(204))
191 logger.error('Errors when removed the user.', err)
196 function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
197 const body: UserUpdateMe = req.body
199 // FIXME: user is not already a Sequelize instance?
200 db.User.loadByUsername(res.locals.oauth.token.user.username)
202 if (body.password !== undefined) user.password = body.password
203 if (body.email !== undefined) user.email = body.email
204 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
208 .then(() => res.sendStatus(204))
209 .catch(err => next(err))
212 function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
213 const body: UserUpdate = req.body
214 const user: UserInstance = res.locals.user
216 if (body.email !== undefined) user.email = body.email
217 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
220 .then(() => res.sendStatus(204))
221 .catch(err => next(err))
224 function success (req: express.Request, res: express.Response, next: express.NextFunction) {