-import express = require('express')
+import * as express from 'express'
import { waterfall } from 'async'
import { database as db } from '../../initializers/database'
import {
authenticate,
ensureIsAdmin,
+ ensureUserRegistrationEnabled,
usersAddValidator,
usersUpdateValidator,
usersRemoveValidator,
setUsersSort,
token
} from '../../middlewares'
+import { UserVideoRate as FormatedUserVideoRate } from '../../../shared'
const usersRouter = express.Router()
)
usersRouter.post('/register',
- ensureRegistrationEnabled,
+ ensureUserRegistrationEnabled,
usersAddValidator,
createUser
)
// ---------------------------------------------------------------------------
-function ensureRegistrationEnabled (req, res, next) {
- const registrationEnabled = CONFIG.SIGNUP.ENABLED
-
- if (registrationEnabled === true) {
- return next()
- }
-
- return res.status(400).send('User registration is not enabled.')
-}
-
-function createUser (req, res, next) {
+function createUser (req: express.Request, res: express.Response, next: express.NextFunction) {
const user = db.User.build({
username: req.body.username,
password: req.body.password,
role: USER_ROLES.USER
})
- user.save().asCallback(function (err, createdUser) {
+ user.save().asCallback(function (err) {
if (err) return next(err)
return res.type('json').status(204).end()
})
}
-function getUserInformation (req, res, next) {
+function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
if (err) return next(err)
})
}
-function getUserVideoRating (req, res, next) {
- const videoId = req.params.videoId
- const userId = res.locals.oauth.token.User.id
+function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const videoId = '' + req.params.videoId
+ const userId = +res.locals.oauth.token.User.id
db.UserVideoRate.load(userId, videoId, null, function (err, ratingObj) {
if (err) return next(err)
const rating = ratingObj ? ratingObj.type : 'none'
- res.json({
+ const json: FormatedUserVideoRate = {
videoId,
rating
- })
+ }
+ res.json(json)
})
}
-function listUsers (req, res, next) {
+function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
db.User.listForApi(req.query.start, req.query.count, req.query.sort, function (err, usersList, usersTotal) {
if (err) return next(err)
})
}
-function removeUser (req, res, next) {
+function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
waterfall([
function loadUser (callback) {
db.User.loadById(req.params.id, callback)
})
}
-function updateUser (req, res, next) {
+function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
db.User.loadByUsername(res.locals.oauth.token.user.username, function (err, user) {
if (err) return next(err)
})
}
-function success (req, res, next) {
+function success (req: express.Request, res: express.Response, next: express.NextFunction) {
res.end()
}