]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/pods.js
Client: fix prod compilation
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.js
... / ...
CommitLineData
1'use strict'
2
3const express = require('express')
4const waterfall = require('async/waterfall')
5
6const db = require('../../initializers/database')
7const constants = require('../../initializers/constants')
8const logger = require('../../helpers/logger')
9const peertubeCrypto = require('../../helpers/peertube-crypto')
10const utils = require('../../helpers/utils')
11const friends = require('../../lib/friends')
12const middlewares = require('../../middlewares')
13const admin = middlewares.admin
14const oAuth = middlewares.oauth
15const podsMiddleware = middlewares.pods
16const checkSignature = middlewares.secure.checkSignature
17const validators = middlewares.validators.pods
18const signatureValidator = middlewares.validators.remote.signature
19
20const router = express.Router()
21
22router.get('/', listPods)
23router.post('/',
24 podsMiddleware.setBodyHostPort, // We need to modify the host before running the validator!
25 validators.podsAdd,
26 addPods
27)
28router.post('/makefriends',
29 oAuth.authenticate,
30 admin.ensureIsAdmin,
31 validators.makeFriends,
32 podsMiddleware.setBodyHostsPort,
33 makeFriends
34)
35router.get('/quitfriends',
36 oAuth.authenticate,
37 admin.ensureIsAdmin,
38 quitFriends
39)
40// Post because this is a secured request
41router.post('/remove',
42 signatureValidator.signature,
43 checkSignature,
44 removePods
45)
46
47// ---------------------------------------------------------------------------
48
49module.exports = router
50
51// ---------------------------------------------------------------------------
52
53function 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
88function 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
96function 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
111function 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
129function 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}