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