]>
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 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
19 const router
= express
.Router()
21 router
.get('/', listPods
)
24 podsMiddleware
.setBodyHostPort
,
27 router
.post('/makefriends',
30 validators
.makeFriends
,
31 podsMiddleware
.setBodyHostsPort
,
34 router
.get('/quitfriends',
39 // Post because this is a secured request
40 router
.post('/remove',
41 signatureValidator
.signature
,
46 // ---------------------------------------------------------------------------
48 module
.exports
= router
50 // ---------------------------------------------------------------------------
52 function addPods (req
, res
, next
) {
53 const informations
= req
.body
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
)
64 function sendMyVideos (podCreated
, callback
) {
65 friends
.sendOwnedVideosToPod(podCreated
.id
)
70 function fetchMyCertificate (callback
) {
71 peertubeCrypto
.getMyPublicCert(function (err
, cert
) {
73 logger
.error('Cannot read cert file.')
77 return callback(null, cert
)
80 ], function (err
, cert
) {
81 if (err
) return next(err
)
83 return res
.json({ cert: cert
})
87 function listPods (req
, res
, next
) {
88 db
.Pod
.list(function (err
, podsList
) {
89 if (err
) return next(err
)
91 res
.json(utils
.getFormatedObjects(podsList
, podsList
.length
))
95 function makeFriends (req
, res
, next
) {
96 const hosts
= req
.body
.hosts
98 friends
.makeFriends(hosts
, function (err
) {
100 logger
.error('Could not make friends.', { error: err
})
104 logger
.info('Made friends!')
107 res
.type('json').status(204).end()
110 function removePods (req
, res
, next
) {
111 const host
= req
.body
.signature
.host
114 function loadPod (callback
) {
115 db
.Pod
.loadByHost(host
, callback
)
118 function deletePod (pod
, callback
) {
119 pod
.destroy().asCallback(callback
)
122 if (err
) return next(err
)
124 return res
.type('json').status(204).end()
128 function quitFriends (req
, res
, next
) {
129 friends
.quitFriends(function (err
) {
130 if (err
) return next(err
)
132 res
.type('json').status(204).end()