]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/pods.ts
First typescript iteration
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.ts
CommitLineData
65fcc311
C
1import express = require('express')
2import { waterfall } from 'async'
3
4const db = require('../../initializers/database')
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'
24
25const podsRouter = express.Router()
26
27podsRouter.get('/', listPods)
28podsRouter.post('/',
29 setBodyHostPort, // We need to modify the host before running the validator!
30 podsAddValidator,
31 addPods
32)
33podsRouter.post('/makefriends',
34 authenticate,
35 ensureIsAdmin,
36 makeFriendsValidator,
37 setBodyHostsPort,
38 makeFriends
39)
40podsRouter.get('/quitfriends',
41 authenticate,
42 ensureIsAdmin,
43 quitFriends
44)
45
46// ---------------------------------------------------------------------------
47
48export {
49 podsRouter
50}
51
52// ---------------------------------------------------------------------------
53
54function addPods (req, res, next) {
55 const informations = req.body
56
57 waterfall([
58 function addPod (callback) {
59 const pod = db.Pod.build(informations)
60 pod.save().asCallback(function (err, podCreated) {
61 // Be sure about the number of parameters for the callback
62 return callback(err, podCreated)
63 })
64 },
65
66 function sendMyVideos (podCreated, callback) {
67 sendOwnedVideosToPod(podCreated.id)
68
69 callback(null)
70 },
71
72 function fetchMyCertificate (callback) {
73 getMyPublicCert(function (err, cert) {
74 if (err) {
75 logger.error('Cannot read cert file.')
76 return callback(err)
77 }
78
79 return callback(null, cert)
80 })
81 }
82 ], function (err, cert) {
83 if (err) return next(err)
84
85 return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL })
86 })
87}
88
89function listPods (req, res, next) {
90 db.Pod.list(function (err, podsList) {
91 if (err) return next(err)
92
93 res.json(getFormatedObjects(podsList, podsList.length))
94 })
95}
96
97function makeFriendsController (req, res, next) {
98 const hosts = req.body.hosts
99
100 makeFriends(hosts, function (err) {
101 if (err) {
102 logger.error('Could not make friends.', { error: err })
103 return
104 }
105
106 logger.info('Made friends!')
107 })
108
109 res.type('json').status(204).end()
110}
111
112function quitFriendsController (req, res, next) {
113 quitFriends(function (err) {
114 if (err) return next(err)
115
116 res.type('json').status(204).end()
117 })
118}