1
0
Fork 0
peertube/server/controllers/api/users.ts

215 lines
5.6 KiB
TypeScript
Raw Normal View History

2017-06-05 19:53:49 +00:00
import * as express from 'express'
2016-03-21 10:56:33 +00:00
2017-05-22 18:58:25 +00:00
import { database as db } from '../../initializers/database'
2017-09-04 18:07:54 +00:00
import { USER_ROLES, CONFIG } from '../../initializers'
2017-10-24 17:41:09 +00:00
import { logger, getFormattedObjects, retryTransactionWrapper } from '../../helpers'
2017-05-15 20:22:03 +00:00
import {
authenticate,
ensureIsAdmin,
ensureUserRegistrationAllowed,
2017-05-15 20:22:03 +00:00
usersAddValidator,
2017-09-06 14:35:40 +00:00
usersRegisterValidator,
2017-05-15 20:22:03 +00:00
usersUpdateValidator,
2017-09-05 19:29:39 +00:00
usersUpdateMeValidator,
2017-05-15 20:22:03 +00:00
usersRemoveValidator,
usersVideoRatingValidator,
2017-09-05 19:29:39 +00:00
usersGetValidator,
2017-05-15 20:22:03 +00:00
paginationValidator,
setPagination,
usersSortValidator,
setUsersSort,
2017-10-25 09:55:06 +00:00
token,
asyncMiddleware
2017-05-15 20:22:03 +00:00
} from '../../middlewares'
2017-09-05 19:29:39 +00:00
import {
UserVideoRate as FormattedUserVideoRate,
UserCreate,
UserUpdate,
UserUpdateMe
} from '../../../shared'
2017-10-24 17:41:09 +00:00
import { createUserAuthorAndChannel } from '../../lib'
2017-09-06 14:35:40 +00:00
import { UserInstance } from '../../models'
2017-05-15 20:22:03 +00:00
const usersRouter = express.Router()
usersRouter.get('/me',
authenticate,
2017-10-25 09:55:06 +00:00
asyncMiddleware(getUserInformation)
2017-03-08 20:35:43 +00:00
)
2017-05-15 20:22:03 +00:00
usersRouter.get('/me/videos/:videoId/rating',
authenticate,
usersVideoRatingValidator,
2017-10-25 09:55:06 +00:00
asyncMiddleware(getUserVideoRating)
2017-03-08 20:35:43 +00:00
)
2017-05-15 20:22:03 +00:00
usersRouter.get('/',
paginationValidator,
usersSortValidator,
setUsersSort,
setPagination,
2017-10-25 09:55:06 +00:00
asyncMiddleware(listUsers)
2016-08-16 20:31:45 +00:00
)
2017-09-05 19:29:39 +00:00
usersRouter.get('/:id',
usersGetValidator,
getUser
)
2017-05-15 20:22:03 +00:00
usersRouter.post('/',
authenticate,
ensureIsAdmin,
usersAddValidator,
2017-10-24 17:41:09 +00:00
createUserRetryWrapper
)
2017-05-15 20:22:03 +00:00
usersRouter.post('/register',
ensureUserRegistrationAllowed,
2017-09-06 14:35:40 +00:00
usersRegisterValidator,
2017-10-25 09:55:06 +00:00
asyncMiddleware(registerUser)
)
2017-09-05 19:29:39 +00:00
usersRouter.put('/me',
authenticate,
usersUpdateMeValidator,
2017-10-25 09:55:06 +00:00
asyncMiddleware(updateMe)
2017-09-05 19:29:39 +00:00
)
2017-05-15 20:22:03 +00:00
usersRouter.put('/:id',
authenticate,
2017-09-05 19:29:39 +00:00
ensureIsAdmin,
2017-05-15 20:22:03 +00:00
usersUpdateValidator,
2017-10-25 09:55:06 +00:00
asyncMiddleware(updateUser)
)
2017-05-15 20:22:03 +00:00
usersRouter.delete('/:id',
authenticate,
ensureIsAdmin,
usersRemoveValidator,
2017-10-25 09:55:06 +00:00
asyncMiddleware(removeUser)
)
2016-08-05 14:09:39 +00:00
2017-05-15 20:22:03 +00: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 10:56:33 +00:00
// ---------------------------------------------------------------------------
2017-05-15 20:22:03 +00:00
export {
usersRouter
}
2016-03-21 10:56:33 +00:00
// ---------------------------------------------------------------------------
2017-10-25 09:55:06 +00:00
async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-10-24 17:41:09 +00:00
const options = {
arguments: [ req, res ],
errorMessage: 'Cannot insert the user with many retries.'
}
2017-10-25 09:55:06 +00:00
await retryTransactionWrapper(createUser, options)
// TODO : include Location of the new user -> 201
return res.type('json').status(204).end()
2017-10-24 17:41:09 +00:00
}
2017-10-25 09:55:06 +00:00
async function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
const body: UserCreate = req.body
2016-12-11 20:50:51 +00:00
const user = db.User.build({
username: body.username,
password: body.password,
email: body.email,
2017-04-03 19:24:36 +00:00
displayNSFW: false,
2017-09-04 18:07:54 +00:00
role: USER_ROLES.USER,
videoQuota: body.videoQuota
})
2017-10-25 09:55:06 +00:00
await createUserAuthorAndChannel(user)
logger.info('User %s with its channel and author created.', body.username)
}
2017-10-25 09:55:06 +00:00
async function registerUser (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-09-06 14:35:40 +00:00
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
})
2017-10-25 09:55:06 +00:00
await createUserAuthorAndChannel(user)
return res.type('json').status(204).end()
2017-09-06 14:35:40 +00:00
}
2017-10-25 09:55:06 +00:00
async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
const user = await db.User.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
return res.json(user.toFormattedJSON())
}
2017-09-05 19:29:39 +00:00
function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
return res.json(res.locals.user.toFormattedJSON())
}
2017-10-25 09:55:06 +00:00
async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoId = +req.params.videoId
2017-06-10 20:15:25 +00:00
const userId = +res.locals.oauth.token.User.id
2017-03-08 20:35:43 +00:00
2017-10-26 06:51:11 +00:00
const ratingObj = await db.UserVideoRate.load(userId, videoId, null)
const rating = ratingObj ? ratingObj.type : 'none'
const json: FormattedUserVideoRate = {
videoId,
rating
}
res.json(json)
2017-03-08 20:35:43 +00:00
}
2017-10-25 09:55:06 +00:00
async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
const resultList = await db.User.listForApi(req.query.start, req.query.count, req.query.sort)
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
2017-10-25 09:55:06 +00:00
async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
const user = await db.User.loadById(req.params.id)
await user.destroy()
return res.sendStatus(204)
}
2017-10-25 09:55:06 +00:00
async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-09-05 19:29:39 +00:00
const body: UserUpdateMe = req.body
2017-09-05 19:29:39 +00:00
// FIXME: user is not already a Sequelize instance?
2017-10-25 09:55:06 +00:00
const user = res.locals.oauth.token.user
2017-04-03 19:24:36 +00:00
2017-10-25 09:55:06 +00: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
await user.save()
2017-10-25 14:52:01 +00:00
return res.sendStatus(204)
}
2017-10-25 09:55:06 +00:00
async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
2017-09-05 19:29:39 +00:00
const body: UserUpdate = req.body
2017-09-06 14:35:40 +00:00
const user: UserInstance = res.locals.user
2017-09-05 19:29:39 +00:00
if (body.email !== undefined) user.email = body.email
if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
2017-10-25 09:55:06 +00:00
await user.save()
return res.sendStatus(204)
2017-09-05 19:29:39 +00:00
}
2017-06-10 20:15:25 +00:00
function success (req: express.Request, res: express.Response, next: express.NextFunction) {
2016-03-21 10:56:33 +00:00
res.end()
}