1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-06 03:50:26 +02:00

Add pod list endpoint with pagination, sort...

This commit is contained in:
Chocobozzz 2017-10-19 09:43:01 +02:00
parent 9fd540562c
commit 8a02bd0433
No known key found for this signature in database
GPG key ID: 583A612D890159BE
19 changed files with 232 additions and 94 deletions

View file

@ -3,7 +3,7 @@ import * as express from 'express'
import { database as db } from '../../initializers/database'
import { checkErrors } from './utils'
import { logger, isEachUniqueHostValid, isHostValid } from '../../helpers'
import { logger, isEachUniqueHostValid } from '../../helpers'
import { CONFIG } from '../../initializers'
import { hasFriends } from '../../lib'
import { isTestInstance } from '../../helpers'
@ -41,32 +41,6 @@ const makeFriendsValidator = [
}
]
const podsAddValidator = [
body('host').custom(isHostValid).withMessage('Should have a host'),
body('email').isEmail().withMessage('Should have an email'),
body('publicKey').not().isEmpty().withMessage('Should have a public key'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking podsAdd parameters', { parameters: req.body })
checkErrors(req, res, () => {
db.Pod.loadByHost(req.body.host)
.then(pod => {
// Pod with this host already exists
if (pod) {
return res.sendStatus(409)
}
return next()
})
.catch(err => {
logger.error('Cannot load pod by host.', err)
res.sendStatus(500)
})
})
}
]
const podRemoveValidator = [
param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'),
@ -96,6 +70,5 @@ const podRemoveValidator = [
export {
makeFriendsValidator,
podsAddValidator,
podRemoveValidator
}

View file

@ -1,2 +1,3 @@
export * from './pods'
export * from './signature'
export * from './videos'

View file

@ -0,0 +1,38 @@
import { body } from 'express-validator/check'
import * as express from 'express'
import { database as db } from '../../../initializers'
import { isHostValid, logger } from '../../../helpers'
import { checkErrors } from '../utils'
const remotePodsAddValidator = [
body('host').custom(isHostValid).withMessage('Should have a host'),
body('email').isEmail().withMessage('Should have an email'),
body('publicKey').not().isEmpty().withMessage('Should have a public key'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug('Checking podsAdd parameters', { parameters: req.body })
checkErrors(req, res, () => {
db.Pod.loadByHost(req.body.host)
.then(pod => {
// Pod with this host already exists
if (pod) {
return res.sendStatus(409)
}
return next()
})
.catch(err => {
logger.error('Cannot load pod by host.', err)
res.sendStatus(500)
})
})
}
]
// ---------------------------------------------------------------------------
export {
remotePodsAddValidator
}

View file

@ -6,11 +6,13 @@ import { logger } from '../../helpers'
import { SORTABLE_COLUMNS } from '../../initializers'
// Initialize constants here for better performances
const SORTABLE_PODS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.PODS)
const SORTABLE_USERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USERS)
const SORTABLE_VIDEO_ABUSES_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_ABUSES)
const SORTABLE_VIDEOS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEOS)
const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACKLISTS)
const podsSortValidator = checkSort(SORTABLE_PODS_COLUMNS)
const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS)
const videoAbusesSortValidator = checkSort(SORTABLE_VIDEO_ABUSES_COLUMNS)
const videosSortValidator = checkSort(SORTABLE_VIDEOS_COLUMNS)
@ -19,6 +21,7 @@ const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS)
// ---------------------------------------------------------------------------
export {
podsSortValidator,
usersSortValidator,
videoAbusesSortValidator,
videosSortValidator,

View file

@ -3,12 +3,12 @@ import * as express from 'express'
import { logger } from '../../helpers'
function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction, statusCode = 400) {
function checkErrors (req: express.Request, res: express.Response, next: express.NextFunction) {
const errors = validationResult(req)
if (!errors.isEmpty()) {
logger.warn('Incorrect request parameters', { path: req.originalUrl, err: errors.mapped() })
return res.status(statusCode).json({ errors: errors.mapped() })
return res.status(400).json({ errors: errors.mapped() })
}
return next()