]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/pods.js
3 const express
= require('express')
4 const waterfall
= require('async/waterfall')
6 const db
= require('../../initializers/database')
7 const logger
= require('../../helpers/logger')
8 const friends
= require('../../lib/friends')
9 const middlewares
= require('../../middlewares')
10 const admin
= middlewares
.admin
11 const oAuth
= middlewares
.oauth
12 const podsMiddleware
= middlewares
.pods
13 const checkSignature
= middlewares
.secure
.checkSignature
14 const validators
= middlewares
.validators
.pods
15 const signatureValidator
= middlewares
.validators
.remote
.signature
17 const router
= express
.Router()
19 router
.get('/', listPods
)
22 podsMiddleware
.setBodyHostPort
,
25 router
.post('/makefriends',
28 validators
.makeFriends
,
29 podsMiddleware
.setBodyHostsPort
,
32 router
.get('/quitfriends',
37 // Post because this is a secured request
38 router
.post('/remove',
44 // ---------------------------------------------------------------------------
46 module
.exports
= router
48 // ---------------------------------------------------------------------------
50 function addPods (req
, res
, next
) {
51 const informations
= req
.body
54 function addPod (callback
) {
55 const pod
= db
.Pod
.build(informations
)
56 pod
.save().asCallback(function (err
, podCreated
) {
57 // Be sure about the number of parameters for the callback
58 return callback(err
, podCreated
)
62 function sendMyVideos (podCreated
, callback
) {
63 friends
.sendOwnedVideosToPod(podCreated
.id
)
68 function fetchMyCertificate (callback
) {
69 friends
.getMyCertificate(function (err
, cert
) {
71 logger
.error('Cannot read cert file.')
75 return callback(null, cert
)
78 ], function (err
, cert
) {
79 if (err
) return next(err
)
81 return res
.json({ cert: cert
})
85 function listPods (req
, res
, next
) {
86 db
.Pod
.list(function (err
, podsList
) {
87 if (err
) return next(err
)
89 res
.json(getFormatedPods(podsList
))
93 function makeFriends (req
, res
, next
) {
94 const hosts
= req
.body
.hosts
96 friends
.makeFriends(hosts
, function (err
) {
98 logger
.error('Could not make friends.', { error: err
})
102 logger
.info('Made friends!')
105 res
.type('json').status(204).end()
108 function removePods (req
, res
, next
) {
109 const host
= req
.body
.signature
.host
112 function loadPod (callback
) {
113 db
.Pod
.loadByHost(host
, callback
)
116 function deletePod (pod
, callback
) {
117 pod
.destroy().asCallback(callback
)
120 if (err
) return next(err
)
122 return res
.type('json').status(204).end()
126 function quitFriends (req
, res
, next
) {
127 friends
.quitFriends(function (err
) {
128 if (err
) return next(err
)
130 res
.type('json').status(204).end()
134 // ---------------------------------------------------------------------------
136 function getFormatedPods (pods
) {
137 const formatedPods
= []
139 pods
.forEach(function (pod
) {
140 formatedPods
.push(pod
.toFormatedJSON())