]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/pods.ts
Upgrade server packages
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pods.ts
CommitLineData
b60e5f38 1import { body, param } from 'express-validator/check'
69818c93
C
2import * as express from 'express'
3
e02643f3 4import { database as db } from '../../initializers/database'
65fcc311 5import { checkErrors } from './utils'
53abc4c2 6import { logger, isEachUniqueHostValid, isTestInstance } from '../../helpers'
65fcc311
C
7import { CONFIG } from '../../initializers'
8import { hasFriends } from '../../lib'
9f10b292 9
b60e5f38
C
10const makeFriendsValidator = [
11 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
12
13 (req: express.Request, res: express.Response, next: express.NextFunction) => {
14 // Force https if the administrator wants to make friends
15 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
16 return res.status(400)
17 .json({
18 error: 'Cannot make friends with a non HTTPS web server.'
19 })
20 .end()
21 }
22
23 logger.debug('Checking makeFriends parameters', { parameters: req.body })
24
25 checkErrors(req, res, () => {
26 hasFriends()
27 .then(heHasFriends => {
28 if (heHasFriends === true) {
29 // We need to quit our friends before make new ones
30 return res.sendStatus(409)
31 }
32
33 return next()
34 })
35 .catch(err => {
36 logger.error('Cannot know if we have friends.', err)
37 res.sendStatus(500)
38 })
39 })
441b66f8 40 }
b60e5f38 41]
441b66f8 42
b60e5f38
C
43const podRemoveValidator = [
44 param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'),
45
46 (req: express.Request, res: express.Response, next: express.NextFunction) => {
47 logger.debug('Checking podRemoveValidator parameters', { parameters: req.params })
48
49 checkErrors(req, res, () => {
50 db.Pod.load(req.params.id)
51 .then(pod => {
52 if (!pod) {
53 logger.error('Cannot find pod %d.', req.params.id)
54 return res.sendStatus(404)
55 }
56
57 res.locals.pod = pod
58 return next()
59 })
60 .catch(err => {
61 logger.error('Cannot load pod %d.', req.params.id, err)
62 res.sendStatus(500)
63 })
64 })
65 }
66]
d5f5a670 67
9f10b292
C
68// ---------------------------------------------------------------------------
69
65fcc311
C
70export {
71 makeFriendsValidator,
d5f5a670 72 podRemoveValidator
65fcc311 73}