1 import * as express from 'express'
3 import { database as db, CONFIG } from '../../initializers'
4 import { logger, getFormattedObjects, retryTransactionWrapper } from '../../helpers'
8 ensureUserRegistrationAllowed,
10 usersRegisterValidator,
12 usersUpdateMeValidator,
14 usersVideoRatingValidator,
22 } from '../../middlewares'
24 UserVideoRate as FormattedUserVideoRate,
30 } from '../../../shared'
31 import { createUserAuthorAndChannel } from '../../lib'
32 import { UserInstance } from '../../models'
33 import { videosSortValidator } from '../../middlewares/validators/sort'
34 import { setVideosSort } from '../../middlewares/sort'
36 const usersRouter = express.Router()
38 usersRouter.get('/me',
40 asyncMiddleware(getUserInformation)
43 usersRouter.get('/me/videos',
49 asyncMiddleware(getUserVideos)
52 usersRouter.get('/me/videos/:videoId/rating',
54 usersVideoRatingValidator,
55 asyncMiddleware(getUserVideoRating)
63 asyncMiddleware(listUsers)
66 usersRouter.get('/:id',
73 ensureUserHasRight(UserRight.MANAGE_USERS),
75 createUserRetryWrapper
78 usersRouter.post('/register',
79 ensureUserRegistrationAllowed,
80 usersRegisterValidator,
81 asyncMiddleware(registerUser)
84 usersRouter.put('/me',
86 usersUpdateMeValidator,
87 asyncMiddleware(updateMe)
90 usersRouter.put('/:id',
92 ensureUserHasRight(UserRight.MANAGE_USERS),
94 asyncMiddleware(updateUser)
97 usersRouter.delete('/:id',
99 ensureUserHasRight(UserRight.MANAGE_USERS),
100 usersRemoveValidator,
101 asyncMiddleware(removeUser)
104 usersRouter.post('/token', token, success)
105 // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
107 // ---------------------------------------------------------------------------
113 // ---------------------------------------------------------------------------
115 async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
116 const user = res.locals.oauth.token.User
117 const resultList = await db.Video.listUserVideosForApi(user.id ,req.query.start, req.query.count, req.query.sort)
119 return res.json(getFormattedObjects(resultList.data, resultList.total))
122 async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
124 arguments: [ req, res ],
125 errorMessage: 'Cannot insert the user with many retries.'
128 await retryTransactionWrapper(createUser, options)
130 // TODO : include Location of the new user -> 201
131 return res.type('json').status(204).end()
134 async function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
135 const body: UserCreate = req.body
136 const user = db.User.build({
137 username: body.username,
138 password: body.password,
142 videoQuota: body.videoQuota
145 await createUserAuthorAndChannel(user)
147 logger.info('User %s with its channel and author created.', body.username)
150 async function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) {
151 const body: UserCreate = req.body
153 const user = db.User.build({
154 username: body.username,
155 password: body.password,
159 videoQuota: CONFIG.USER.VIDEO_QUOTA
162 await createUserAuthorAndChannel(user)
163 return res.type('json').status(204).end()
166 async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
167 // We did not load channels in res.locals.user
168 const user = await db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
170 return res.json(user.toFormattedJSON())
173 function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
174 return res.json(res.locals.user.toFormattedJSON())
177 async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
178 const videoId = +req.params.videoId
179 const userId = +res.locals.oauth.token.User.id
181 const ratingObj = await db.UserVideoRate.load(userId, videoId, null)
182 const rating = ratingObj ? ratingObj.type : 'none'
184 const json: FormattedUserVideoRate = {
191 async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
192 const resultList = await db.User.listForApi(req.query.start, req.query.count, req.query.sort)
194 return res.json(getFormattedObjects(resultList.data, resultList.total))
197 async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
198 const user = await db.User.loadById(req.params.id)
202 return res.sendStatus(204)
205 async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
206 const body: UserUpdateMe = req.body
208 // FIXME: user is not already a Sequelize instance?
209 const user = res.locals.oauth.token.user
211 if (body.password !== undefined) user.password = body.password
212 if (body.email !== undefined) user.email = body.email
213 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
217 return res.sendStatus(204)
220 async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
221 const body: UserUpdate = req.body
222 const user: UserInstance = res.locals.user
224 if (body.email !== undefined) user.email = body.email
225 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
226 if (body.role !== undefined) user.role = body.role
230 return res.sendStatus(204)
233 function success (req: express.Request, res: express.Response, next: express.NextFunction) {