]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/pods.js
Client: fix prod compilation
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.js
1 'use strict'
2
3 const express = require('express')
4 const waterfall = require('async/waterfall')
5
6 const db = require('../../initializers/database')
7 const constants = require('../../initializers/constants')
8 const logger = require('../../helpers/logger')
9 const peertubeCrypto = require('../../helpers/peertube-crypto')
10 const utils = require('../../helpers/utils')
11 const friends = require('../../lib/friends')
12 const middlewares = require('../../middlewares')
13 const admin = middlewares.admin
14 const oAuth = middlewares.oauth
15 const podsMiddleware = middlewares.pods
16 const checkSignature = middlewares.secure.checkSignature
17 const validators = middlewares.validators.pods
18 const signatureValidator = middlewares.validators.remote.signature
19
20 const router = express.Router()
21
22 router.get('/', listPods)
23 router.post('/',
24 podsMiddleware.setBodyHostPort, // We need to modify the host before running the validator!
25 validators.podsAdd,
26 addPods
27 )
28 router.post('/makefriends',
29 oAuth.authenticate,
30 admin.ensureIsAdmin,
31 validators.makeFriends,
32 podsMiddleware.setBodyHostsPort,
33 makeFriends
34 )
35 router.get('/quitfriends',
36 oAuth.authenticate,
37 admin.ensureIsAdmin,
38 quitFriends
39 )
40 // Post because this is a secured request
41 router.post('/remove',
42 signatureValidator.signature,
43 checkSignature,
44 removePods
45 )
46
47 // ---------------------------------------------------------------------------
48
49 module.exports = router
50
51 // ---------------------------------------------------------------------------
52
53 function addPods (req, res, next) {
54 const informations = req.body
55
56 waterfall([
57 function addPod (callback) {
58 const pod = db.Pod.build(informations)
59 pod.save().asCallback(function (err, podCreated) {
60 // Be sure about the number of parameters for the callback
61 return callback(err, podCreated)
62 })
63 },
64
65 function sendMyVideos (podCreated, callback) {
66 friends.sendOwnedVideosToPod(podCreated.id)
67
68 callback(null)
69 },
70
71 function fetchMyCertificate (callback) {
72 peertubeCrypto.getMyPublicCert(function (err, cert) {
73 if (err) {
74 logger.error('Cannot read cert file.')
75 return callback(err)
76 }
77
78 return callback(null, cert)
79 })
80 }
81 ], function (err, cert) {
82 if (err) return next(err)
83
84 return res.json({ cert: cert, email: constants.CONFIG.ADMIN.EMAIL })
85 })
86 }
87
88 function listPods (req, res, next) {
89 db.Pod.list(function (err, podsList) {
90 if (err) return next(err)
91
92 res.json(utils.getFormatedObjects(podsList, podsList.length))
93 })
94 }
95
96 function makeFriends (req, res, next) {
97 const hosts = req.body.hosts
98
99 friends.makeFriends(hosts, function (err) {
100 if (err) {
101 logger.error('Could not make friends.', { error: err })
102 return
103 }
104
105 logger.info('Made friends!')
106 })
107
108 res.type('json').status(204).end()
109 }
110
111 function removePods (req, res, next) {
112 const host = req.body.signature.host
113
114 waterfall([
115 function loadPod (callback) {
116 db.Pod.loadByHost(host, callback)
117 },
118
119 function deletePod (pod, callback) {
120 pod.destroy().asCallback(callback)
121 }
122 ], function (err) {
123 if (err) return next(err)
124
125 return res.type('json').status(204).end()
126 })
127 }
128
129 function quitFriends (req, res, next) {
130 friends.quitFriends(function (err) {
131 if (err) return next(err)
132
133 res.type('json').status(204).end()
134 })
135 }