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