]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/pods.ts
Type functions
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
65fcc311
C
2import { waterfall } from 'async'
3
e02643f3 4import { database as db } from '../../initializers/database'
65fcc311
C
5import { CONFIG } from '../../initializers'
6import {
7 logger,
8 getMyPublicCert,
9 getFormatedObjects
10} from '../../helpers'
11import {
12 sendOwnedVideosToPod,
13 makeFriends,
14 quitFriends
15} from '../../lib'
16import {
17 podsAddValidator,
18 authenticate,
19 ensureIsAdmin,
20 makeFriendsValidator,
21 setBodyHostPort,
22 setBodyHostsPort
23} from '../../middlewares'
69818c93
C
24import {
25 PodInstance
26} from '../../models'
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
69818c93 60 waterfall<string, Error>([
65fcc311
C
61 function addPod (callback) {
62 const pod = db.Pod.build(informations)
63 pod.save().asCallback(function (err, podCreated) {
64 // Be sure about the number of parameters for the callback
65 return callback(err, podCreated)
66 })
67 },
68
69818c93 69 function sendMyVideos (podCreated: PodInstance, callback) {
65fcc311
C
70 sendOwnedVideosToPod(podCreated.id)
71
72 callback(null)
73 },
74
75 function fetchMyCertificate (callback) {
76 getMyPublicCert(function (err, cert) {
77 if (err) {
78 logger.error('Cannot read cert file.')
79 return callback(err)
80 }
81
82 return callback(null, cert)
83 })
84 }
85 ], function (err, cert) {
86 if (err) return next(err)
87
88 return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
89 })
90}
91
69818c93 92function listPods (req: express.Request, res: express.Response, next: express.NextFunction) {
65fcc311
C
93 db.Pod.list(function (err, podsList) {
94 if (err) return next(err)
95
96 res.json(getFormatedObjects(podsList, podsList.length))
97 })
98}
99
69818c93
C
100function makeFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
101 const hosts = req.body.hosts as string[]
65fcc311
C
102
103 makeFriends(hosts, function (err) {
104 if (err) {
105 logger.error('Could not make friends.', { error: err })
106 return
107 }
108
109 logger.info('Made friends!')
110 })
111
112 res.type('json').status(204).end()
113}
114
69818c93 115function quitFriendsController (req: express.Request, res: express.Response, next: express.NextFunction) {
65fcc311
C
116 quitFriends(function (err) {
117 if (err) return next(err)
118
119 res.type('json').status(204).end()
120 })
121}