]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/pods.ts
Remove any typing from server
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
65fcc311 2
e02643f3 3import { database as db } from '../../initializers/database'
65fcc311
C
4import { CONFIG } from '../../initializers'
5import {
6 logger,
7 getMyPublicCert,
8 getFormatedObjects
9} from '../../helpers'
10import {
11 sendOwnedVideosToPod,
12 makeFriends,
13 quitFriends
14} from '../../lib'
15import {
16 podsAddValidator,
17 authenticate,
18 ensureIsAdmin,
19 makeFriendsValidator,
20 setBodyHostPort,
21 setBodyHostsPort
22} from '../../middlewares'
69818c93
C
23import {
24 PodInstance
25} from '../../models'
e6d4b0ff 26import { Pod as FormatedPod } from '../../../shared'
65fcc311
C
27
28const podsRouter = express.Router()
29
30podsRouter.get('/', listPods)
31podsRouter.post('/',
32 setBodyHostPort, // We need to modify the host before running the validator!
33 podsAddValidator,
34 addPods
35)
36podsRouter.post('/makefriends',
37 authenticate,
38 ensureIsAdmin,
39 makeFriendsValidator,
40 setBodyHostsPort,
e02643f3 41 makeFriendsController
65fcc311
C
42)
43podsRouter.get('/quitfriends',
44 authenticate,
45 ensureIsAdmin,
e02643f3 46 quitFriendsController
65fcc311
C
47)
48
49// ---------------------------------------------------------------------------
50
51export {
52 podsRouter
53}
54
55// ---------------------------------------------------------------------------
56
69818c93 57function addPods (req: express.Request, res: express.Response, next: express.NextFunction) {
65fcc311
C
58 const informations = req.body
59
6fcd19ba
C
60 const pod = db.Pod.build(informations)
61 pod.save()
62 .then(podCreated => {
63 return sendOwnedVideosToPod(podCreated.id)
64 })
65 .then(() => {
66 return getMyPublicCert()
67 })
68 .then(cert => {
69 return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
70 })
71 .catch(err => next(err))
65fcc311
C
72}
73
69818c93 74function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
6fcd19ba 75 db.Pod.list()
e6d4b0ff 76 .then(podsList => res.json(getFormatedObjects<FormatedPod, PodInstance>(podsList, podsList.length)))
6fcd19ba 77 .catch(err => next(err))
65fcc311
C
78}
79
69818c93
C
80function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
81 const hosts = req.body.hosts as string[]
65fcc311 82
6fcd19ba
C
83 makeFriends(hosts)
84 .then(() => logger.info('Made friends!'))
ad0997ad 85 .catch(err => logger.error('Could not make friends.', err))
65fcc311 86
6fcd19ba 87 // Don't wait the process that could be long
65fcc311
C
88 res.type('json').status(204).end()
89}
90
69818c93 91function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
6fcd19ba
C
92 quitFriends()
93 .then(() => res.type('json').status(204).end())
94 .catch(err => next(err))
65fcc311 95}