]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/pods.ts
Video blacklist refractoring
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pods.ts
1 import { body, param } from 'express-validator/check'
2 import * as express from 'express'
3
4 import { database as db } from '../../initializers/database'
5 import { checkErrors } from './utils'
6 import { logger, isEachUniqueHostValid, isHostValid } from '../../helpers'
7 import { CONFIG } from '../../initializers'
8 import { hasFriends } from '../../lib'
9 import { isTestInstance } from '../../helpers'
10
11 const makeFriendsValidator = [
12 body('hosts').custom(isEachUniqueHostValid).withMessage('Should have an array of unique hosts'),
13
14 (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 // Force https if the administrator wants to make friends
16 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
17 return res.status(400)
18 .json({
19 error: 'Cannot make friends with a non HTTPS web server.'
20 })
21 .end()
22 }
23
24 logger.debug('Checking makeFriends parameters', { parameters: req.body })
25
26 checkErrors(req, res, () => {
27 hasFriends()
28 .then(heHasFriends => {
29 if (heHasFriends === true) {
30 // We need to quit our friends before make new ones
31 return res.sendStatus(409)
32 }
33
34 return next()
35 })
36 .catch(err => {
37 logger.error('Cannot know if we have friends.', err)
38 res.sendStatus(500)
39 })
40 })
41 }
42 ]
43
44 const podsAddValidator = [
45 body('host').custom(isHostValid).withMessage('Should have a host'),
46 body('email').isEmail().withMessage('Should have an email'),
47 body('publicKey').not().isEmpty().withMessage('Should have a public key'),
48
49 (req: express.Request, res: express.Response, next: express.NextFunction) => {
50 logger.debug('Checking podsAdd parameters', { parameters: req.body })
51
52 checkErrors(req, res, () => {
53 db.Pod.loadByHost(req.body.host)
54 .then(pod => {
55 // Pod with this host already exists
56 if (pod) {
57 return res.sendStatus(409)
58 }
59
60 return next()
61 })
62 .catch(err => {
63 logger.error('Cannot load pod by host.', err)
64 res.sendStatus(500)
65 })
66 })
67 }
68 ]
69
70 const podRemoveValidator = [
71 param('id').isNumeric().not().isEmpty().withMessage('Should have a valid id'),
72
73 (req: express.Request, res: express.Response, next: express.NextFunction) => {
74 logger.debug('Checking podRemoveValidator parameters', { parameters: req.params })
75
76 checkErrors(req, res, () => {
77 db.Pod.load(req.params.id)
78 .then(pod => {
79 if (!pod) {
80 logger.error('Cannot find pod %d.', req.params.id)
81 return res.sendStatus(404)
82 }
83
84 res.locals.pod = pod
85 return next()
86 })
87 .catch(err => {
88 logger.error('Cannot load pod %d.', req.params.id, err)
89 res.sendStatus(500)
90 })
91 })
92 }
93 ]
94
95 // ---------------------------------------------------------------------------
96
97 export {
98 makeFriendsValidator,
99 podsAddValidator,
100 podRemoveValidator
101 }