]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/pods.ts
Remove one pod (#76)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / pods.ts
CommitLineData
69818c93
C
1import 'express-validator'
2import * as express from 'express'
3
e02643f3 4import { database as db } from '../../initializers/database'
65fcc311
C
5import { checkErrors } from './utils'
6import { logger } from '../../helpers'
7import { CONFIG } from '../../initializers'
8import { hasFriends } from '../../lib'
9import { isTestInstance } from '../../helpers'
9f10b292 10
69818c93 11function makeFriendsValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
441b66f8 12 // Force https if the administrator wants to make friends
65fcc311 13 if (isTestInstance() === false && CONFIG.WEBSERVER.SCHEME === 'http') {
441b66f8
C
14 return res.status(400).send('Cannot make friends with a non HTTPS webserver.')
15 }
16
49abbbbe 17 req.checkBody('hosts', 'Should have an array of unique hosts').isEachUniqueHostValid()
1e2564d3
C
18
19 logger.debug('Checking makeFriends parameters', { parameters: req.body })
20
075f16ca 21 checkErrors(req, res, () => {
6fcd19ba
C
22 hasFriends()
23 .then(heHasFriends => {
24 if (heHasFriends === true) {
25 // We need to quit our friends before make new ones
26 return res.sendStatus(409)
27 }
28
29 return next()
30 })
31 .catch(err => {
ad0997ad 32 logger.error('Cannot know if we have friends.', err)
d57d6f26 33 res.sendStatus(500)
6fcd19ba 34 })
9f10b292
C
35 })
36}
37
69818c93 38function podsAddValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
4793c343
C
39 req.checkBody('host', 'Should have a host').isHostValid()
40 req.checkBody('email', 'Should have an email').isEmail()
528a9efa 41 req.checkBody('publicKey', 'Should have a public key').notEmpty()
b09ce645 42 logger.debug('Checking podsAdd parameters', { parameters: req.body })
528a9efa 43
075f16ca 44 checkErrors(req, res, () => {
6fcd19ba
C
45 db.Pod.loadByHost(req.body.host)
46 .then(pod => {
47 // Pod with this host already exists
48 if (pod) {
49 return res.sendStatus(409)
50 }
51
52 return next()
53 })
54 .catch(err => {
ad0997ad 55 logger.error('Cannot load pod by host.', err)
b09ce645 56 res.sendStatus(500)
6fcd19ba 57 })
b09ce645 58 })
9f10b292
C
59}
60
d5f5a670
GS
61function podRemoveValidator (req: express.Request, res: express.Response, next: express.NextFunction) {
62 req.checkParams('id', 'Should have a valid id').notEmpty().isNumeric()
63
64 logger.debug('Checking podRemoveValidator parameters', { parameters: req.params })
65
66 checkErrors(req, res, function () {
67 db.Pod.load(req.params.id)
68 .then(pod => {
69 if (!pod) {
70 logger.error('Cannot find pod %d.', req.params.id)
71 return res.sendStatus(404)
72 }
73
74 res.locals.pod = pod
75 return next()
76 })
77 .catch(err => {
78 logger.error('Cannot load pod %d.', req.params.id, err)
79 res.sendStatus(500)
80 })
81 })
82}
83
9f10b292
C
84// ---------------------------------------------------------------------------
85
65fcc311
C
86export {
87 makeFriendsValidator,
d5f5a670
GS
88 podsAddValidator,
89 podRemoveValidator
65fcc311 90}