1 import * as express from 'express'
2 import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
3 import { getFormattedObjects, logger, retryTransactionWrapper } from '../../helpers'
4 import { CONFIG, database as db } from '../../initializers'
5 import { createUserAccountAndChannel } from '../../lib'
10 ensureUserRegistrationAllowed,
17 usersRegisterValidator,
20 usersUpdateMeValidator,
22 usersVideoRatingValidator
23 } from '../../middlewares'
24 import { setVideosSort } from '../../middlewares/sort'
25 import { videosSortValidator } from '../../middlewares/validators/sort'
26 import { UserInstance } from '../../models'
28 const usersRouter = express.Router()
30 usersRouter.get('/me',
32 asyncMiddleware(getUserInformation)
35 usersRouter.get('/me/videos',
41 asyncMiddleware(getUserVideos)
44 usersRouter.get('/me/videos/:videoId/rating',
46 usersVideoRatingValidator,
47 asyncMiddleware(getUserVideoRating)
55 asyncMiddleware(listUsers)
58 usersRouter.get('/:id',
65 ensureUserHasRight(UserRight.MANAGE_USERS),
67 createUserRetryWrapper
70 usersRouter.post('/register',
71 ensureUserRegistrationAllowed,
72 usersRegisterValidator,
73 asyncMiddleware(registerUser)
76 usersRouter.put('/me',
78 usersUpdateMeValidator,
79 asyncMiddleware(updateMe)
82 usersRouter.put('/:id',
84 ensureUserHasRight(UserRight.MANAGE_USERS),
86 asyncMiddleware(updateUser)
89 usersRouter.delete('/:id',
91 ensureUserHasRight(UserRight.MANAGE_USERS),
93 asyncMiddleware(removeUser)
96 usersRouter.post('/token', token, success)
97 // TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
99 // ---------------------------------------------------------------------------
105 // ---------------------------------------------------------------------------
107 async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
108 const user = res.locals.oauth.token.User
109 const resultList = await db.Video.listUserVideosForApi(user.id ,req.query.start, req.query.count, req.query.sort)
111 return res.json(getFormattedObjects(resultList.data, resultList.total))
114 async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
116 arguments: [ req, res ],
117 errorMessage: 'Cannot insert the user with many retries.'
120 await retryTransactionWrapper(createUser, options)
122 // TODO : include Location of the new user -> 201
123 return res.type('json').status(204).end()
126 async function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
127 const body: UserCreate = req.body
128 const user = db.User.build({
129 username: body.username,
130 password: body.password,
134 videoQuota: body.videoQuota
137 await createUserAccountAndChannel(user)
139 logger.info('User %s with its channel and account created.', body.username)
142 async function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) {
143 const body: UserCreate = req.body
145 const user = db.User.build({
146 username: body.username,
147 password: body.password,
151 videoQuota: CONFIG.USER.VIDEO_QUOTA
154 await createUserAccountAndChannel(user)
155 return res.type('json').status(204).end()
158 async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
159 // We did not load channels in res.locals.user
160 const user = await db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
162 return res.json(user.toFormattedJSON())
165 function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
166 return res.json(res.locals.user.toFormattedJSON())
169 async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
170 const videoId = +req.params.videoId
171 const accountId = +res.locals.oauth.token.User.Account.id
173 const ratingObj = await db.AccountVideoRate.load(accountId, videoId, null)
174 const rating = ratingObj ? ratingObj.type : 'none'
176 const json: FormattedUserVideoRate = {
183 async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
184 const resultList = await db.User.listForApi(req.query.start, req.query.count, req.query.sort)
186 return res.json(getFormattedObjects(resultList.data, resultList.total))
189 async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
190 const user = await db.User.loadById(req.params.id)
194 return res.sendStatus(204)
197 async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
198 const body: UserUpdateMe = req.body
200 // FIXME: user is not already a Sequelize instance?
201 const user = res.locals.oauth.token.user
203 if (body.password !== undefined) user.password = body.password
204 if (body.email !== undefined) user.email = body.email
205 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
209 return res.sendStatus(204)
212 async 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
218 if (body.role !== undefined) user.role = body.role
222 return res.sendStatus(204)
225 function success (req: express.Request, res: express.Response, next: express.NextFunction) {