2017-06-05 14:53:49 -05:00
|
|
|
import * as express from 'express'
|
2016-03-21 05:56:33 -05:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-07-05 06:26:25 -05:00
|
|
|
import { USER_ROLES } from '../../initializers'
|
2017-05-15 15:22:03 -05:00
|
|
|
import { logger, getFormatedObjects } from '../../helpers'
|
|
|
|
import {
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
2017-06-25 10:48:51 -05:00
|
|
|
ensureUserRegistrationEnabled,
|
2017-05-15 15:22:03 -05:00
|
|
|
usersAddValidator,
|
|
|
|
usersUpdateValidator,
|
|
|
|
usersRemoveValidator,
|
|
|
|
usersVideoRatingValidator,
|
|
|
|
paginationValidator,
|
|
|
|
setPagination,
|
|
|
|
usersSortValidator,
|
|
|
|
setUsersSort,
|
|
|
|
token
|
|
|
|
} from '../../middlewares'
|
2017-07-10 12:43:21 -05:00
|
|
|
import { UserVideoRate as FormatedUserVideoRate, UserCreate, UserUpdate } from '../../../shared'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
const usersRouter = express.Router()
|
|
|
|
|
|
|
|
usersRouter.get('/me',
|
|
|
|
authenticate,
|
2017-03-08 14:35:43 -06:00
|
|
|
getUserInformation
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.get('/me/videos/:videoId/rating',
|
|
|
|
authenticate,
|
|
|
|
usersVideoRatingValidator,
|
2017-03-08 14:35:43 -06:00
|
|
|
getUserVideoRating
|
|
|
|
)
|
2016-08-04 15:32:36 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.get('/',
|
|
|
|
paginationValidator,
|
|
|
|
usersSortValidator,
|
|
|
|
setUsersSort,
|
|
|
|
setPagination,
|
2016-08-16 15:31:45 -05:00
|
|
|
listUsers
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.post('/',
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
usersAddValidator,
|
2016-08-04 15:32:36 -05:00
|
|
|
createUser
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.post('/register',
|
2017-06-25 10:48:51 -05:00
|
|
|
ensureUserRegistrationEnabled,
|
2017-05-15 15:22:03 -05:00
|
|
|
usersAddValidator,
|
2017-04-09 05:08:36 -05:00
|
|
|
createUser
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.put('/:id',
|
|
|
|
authenticate,
|
|
|
|
usersUpdateValidator,
|
2016-08-04 15:32:36 -05:00
|
|
|
updateUser
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.delete('/:id',
|
|
|
|
authenticate,
|
|
|
|
ensureIsAdmin,
|
|
|
|
usersRemoveValidator,
|
2016-08-04 15:32:36 -05:00
|
|
|
removeUser
|
|
|
|
)
|
2016-08-05 09:09:39 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
usersRouter.post('/token', token, success)
|
2016-08-04 15:32:36 -05:00
|
|
|
// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
|
2016-03-21 05:56:33 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
usersRouter
|
|
|
|
}
|
2016-03-21 05:56:33 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-10 12:43:21 -05:00
|
|
|
const body: UserCreate = req.body
|
|
|
|
|
2016-12-11 14:50:51 -06:00
|
|
|
const user = db.User.build({
|
2017-07-10 12:43:21 -05:00
|
|
|
username: body.username,
|
|
|
|
password: body.password,
|
|
|
|
email: body.email,
|
2017-04-03 14:24:36 -05:00
|
|
|
displayNSFW: false,
|
2017-05-15 15:22:03 -05:00
|
|
|
role: USER_ROLES.USER
|
2016-08-04 15:32:36 -05:00
|
|
|
})
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
user.save()
|
|
|
|
.then(() => res.type('json').status(204).end())
|
|
|
|
.catch(err => next(err))
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-05 06:26:25 -05:00
|
|
|
db.User.loadByUsername(res.locals.oauth.token.user.username)
|
|
|
|
.then(user => res.json(user.toFormatedJSON()))
|
|
|
|
.catch(err => next(err))
|
2016-08-05 10:19:08 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-11 09:01:56 -05:00
|
|
|
const videoId = +req.params.videoId
|
2017-06-10 15:15:25 -05:00
|
|
|
const userId = +res.locals.oauth.token.User.id
|
2017-03-08 14:35:43 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
db.UserVideoRate.load(userId, videoId, null)
|
|
|
|
.then(ratingObj => {
|
|
|
|
const rating = ratingObj ? ratingObj.type : 'none'
|
|
|
|
const json: FormatedUserVideoRate = {
|
|
|
|
videoId,
|
|
|
|
rating
|
|
|
|
}
|
|
|
|
res.json(json)
|
|
|
|
})
|
|
|
|
.catch(err => next(err))
|
2017-03-08 14:35:43 -06:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-05 06:26:25 -05:00
|
|
|
db.User.listForApi(req.query.start, req.query.count, req.query.sort)
|
|
|
|
.then(resultList => {
|
|
|
|
res.json(getFormatedObjects(resultList.data, resultList.total))
|
|
|
|
})
|
|
|
|
.catch(err => next(err))
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-05 06:26:25 -05:00
|
|
|
db.User.loadById(req.params.id)
|
|
|
|
.then(user => user.destroy())
|
|
|
|
.then(() => res.sendStatus(204))
|
|
|
|
.catch(err => {
|
2017-07-07 11:26:12 -05:00
|
|
|
logger.error('Errors when removed the user.', err)
|
2016-08-04 15:32:36 -05:00
|
|
|
return next(err)
|
2017-07-05 06:26:25 -05:00
|
|
|
})
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-10 12:43:21 -05:00
|
|
|
const body: UserUpdate = req.body
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
db.User.loadByUsername(res.locals.oauth.token.user.username)
|
|
|
|
.then(user => {
|
2017-07-10 12:43:21 -05:00
|
|
|
if (body.password) user.password = body.password
|
|
|
|
if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
|
2017-04-03 14:24:36 -05:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return user.save()
|
2016-08-04 15:32:36 -05:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(() => res.sendStatus(204))
|
|
|
|
.catch(err => next(err))
|
2016-08-04 15:32:36 -05:00
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function success (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2016-03-21 05:56:33 -05:00
|
|
|
res.end()
|
|
|
|
}
|