X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fcontrollers%2Fapi%2Fusers.ts;h=ce15353eff73e6232b5773920ccdeda8d5d0ef01;hb=ba44fa19531186944fef3e9da34f5e91f0c0dedb;hp=97a744f54391c56c9c7f77ee6b97c666d44f54bb;hpb=e02643f32e4c97ca307f8fc5b69be79c40d70a3b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/controllers/api/users.ts b/server/controllers/api/users.ts index 97a744f54..ce15353ef 100644 --- a/server/controllers/api/users.ts +++ b/server/controllers/api/users.ts @@ -1,4 +1,4 @@ -import express = require('express') +import * as express from 'express' import { waterfall } from 'async' import { database as db } from '../../initializers/database' @@ -7,6 +7,7 @@ import { logger, getFormatedObjects } from '../../helpers' import { authenticate, ensureIsAdmin, + ensureUserRegistrationEnabled, usersAddValidator, usersUpdateValidator, usersRemoveValidator, @@ -17,6 +18,7 @@ import { setUsersSort, token } from '../../middlewares' +import { UserVideoRate as FormatedUserVideoRate } from '../../../shared' const usersRouter = express.Router() @@ -47,7 +49,7 @@ usersRouter.post('/', ) usersRouter.post('/register', - ensureRegistrationEnabled, + ensureUserRegistrationEnabled, usersAddValidator, createUser ) @@ -76,17 +78,7 @@ export { // --------------------------------------------------------------------------- -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, @@ -95,14 +87,14 @@ function createUser (req, res, next) { 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) @@ -110,23 +102,24 @@ function getUserInformation (req, res, next) { }) } -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) @@ -134,7 +127,7 @@ function listUsers (req, res, next) { }) } -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) @@ -153,7 +146,7 @@ function removeUser (req, res, next) { }) } -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) @@ -168,6 +161,6 @@ function updateUser (req, res, next) { }) } -function success (req, res, next) { +function success (req: express.Request, res: express.Response, next: express.NextFunction) { res.end() }