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,
23 } from '../../middlewares'
25 UserVideoRate as FormattedUserVideoRate,
29 } from '../../../shared'
30 import { createUserAuthorAndChannel } from '../../lib'
31 import { UserInstance } from '../../models'
33 const usersRouter = express.Router()
35 usersRouter.get('/me',
37 asyncMiddleware(getUserInformation)
40 usersRouter.get('/me/videos/:videoId/rating',
42 usersVideoRatingValidator,
43 asyncMiddleware(getUserVideoRating)
51 asyncMiddleware(listUsers)
54 usersRouter.get('/:id',
63 createUserRetryWrapper
66 usersRouter.post('/register',
67 ensureUserRegistrationAllowed,
68 usersRegisterValidator,
69 asyncMiddleware(registerUser)
72 usersRouter.put('/me',
74 usersUpdateMeValidator,
75 asyncMiddleware(updateMe)
78 usersRouter.put('/:id',
82 asyncMiddleware(updateUser)
85 usersRouter.delete('/:id',
89 asyncMiddleware(removeUser)
92 usersRouter.post('/token', token, success)
93 // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
95 // ---------------------------------------------------------------------------
101 // ---------------------------------------------------------------------------
103 async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
105 arguments: [ req, res ],
106 errorMessage: 'Cannot insert the user with many retries.'
109 await retryTransactionWrapper(createUser, options)
111 // TODO : include Location of the new user -> 201
112 return res.type('json').status(204).end()
115 async function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
116 const body: UserCreate = req.body
117 const user = db.User.build({
118 username: body.username,
119 password: body.password,
122 role: USER_ROLES.USER,
123 videoQuota: body.videoQuota
126 await createUserAuthorAndChannel(user)
128 logger.info('User %s with its channel and author created.', body.username)
131 async function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) {
132 const body: UserCreate = req.body
134 const user = db.User.build({
135 username: body.username,
136 password: body.password,
139 role: USER_ROLES.USER,
140 videoQuota: CONFIG.USER.VIDEO_QUOTA
143 await createUserAuthorAndChannel(user)
144 return res.type('json').status(204).end()
147 async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
148 const user = await db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
150 return res.json(user.toFormattedJSON())
153 function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
154 return res.json(res.locals.user.toFormattedJSON())
157 async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
158 const videoId = +req.params.videoId
159 const userId = +res.locals.oauth.token.User.id
161 db.UserVideoRate.load(userId, videoId, null)
163 const rating = ratingObj ? ratingObj.type : 'none'
164 const json: FormattedUserVideoRate = {
170 .catch(err => next(err))
173 async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
174 const resultList = await db.User.listForApi(req.query.start, req.query.count, req.query.sort)
176 return res.json(getFormattedObjects(resultList.data, resultList.total))
179 async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
180 const user = await db.User.loadById(req.params.id)
184 return res.sendStatus(204)
187 async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
188 const body: UserUpdateMe = req.body
190 // FIXME: user is not already a Sequelize instance?
191 const user = res.locals.oauth.token.user
193 if (body.password !== undefined) user.password = body.password
194 if (body.email !== undefined) user.email = body.email
195 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
199 return await res.sendStatus(204)
202 async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
203 const body: UserUpdate = req.body
204 const user: UserInstance = res.locals.user
206 if (body.email !== undefined) user.email = body.email
207 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
211 return res.sendStatus(204)
214 function success (req: express.Request, res: express.Response, next: express.NextFunction) {