]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/pods.ts
Fix test (#71)
[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
d57d6f26 21 checkErrors(req, res, function () {
65fcc311 22 hasFriends(function (err, heHasFriends) {
d57d6f26
C
23 if (err) {
24 logger.error('Cannot know if we have friends.', { error: err })
25 res.sendStatus(500)
26 }
27
65fcc311 28 if (heHasFriends === true) {
d57d6f26 29 // We need to quit our friends before make new ones
b09ce645 30 return res.sendStatus(409)
d57d6f26 31 }
b09ce645
C
32
33 return next()
d57d6f26 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
b09ce645
C
44 checkErrors(req, res, function () {
45 db.Pod.loadByHost(req.body.host, function (err, pod) {
46 if (err) {
47 logger.error('Cannot load pod by host.', { error: err })
48 res.sendStatus(500)
49 }
9f10b292 50
b09ce645
C
51 // Pod with this host already exists
52 if (pod) {
53 return res.sendStatus(409)
54 }
9f10b292 55
b09ce645
C
56 return next()
57 })
58 })
9f10b292
C
59}
60
61// ---------------------------------------------------------------------------
62
65fcc311
C
63export {
64 makeFriendsValidator,
65 podsAddValidator
66}