PeerTube/server/controllers/api/users.ts

210 lines
5.2 KiB
TypeScript
Raw Normal View History

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