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