]>
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 utils
= require('../../helpers/utils')
9 const friends
= require('../../lib/friends')
10 const middlewares
= require('../../middlewares')
11 const admin
= middlewares
.admin
12 const oAuth
= middlewares
.oauth
13 const podsMiddleware
= middlewares
.pods
14 const checkSignature
= middlewares
.secure
.checkSignature
15 const validators
= middlewares
.validators
.pods
16 const signatureValidator
= middlewares
.validators
.remote
.signature
18 const router
= express
.Router()
20 router
.get('/', listPods
)
23 podsMiddleware
.setBodyHostPort
,
26 router
.post('/makefriends',
29 validators
.makeFriends
,
30 podsMiddleware
.setBodyHostsPort
,
33 router
.get('/quitfriends',
38 // Post because this is a secured request
39 router
.post('/remove',
40 signatureValidator
.signature
,
45 // ---------------------------------------------------------------------------
47 module
.exports
= router
49 // ---------------------------------------------------------------------------
51 function addPods (req
, res
, next
) {
52 const informations
= req
.body
55 function addPod (callback
) {
56 const pod
= db
.Pod
.build(informations
)
57 pod
.save().asCallback(function (err
, podCreated
) {
58 // Be sure about the number of parameters for the callback
59 return callback(err
, podCreated
)
63 function sendMyVideos (podCreated
, callback
) {
64 friends
.sendOwnedVideosToPod(podCreated
.id
)
69 function fetchMyCertificate (callback
) {
70 friends
.getMyCertificate(function (err
, cert
) {
72 logger
.error('Cannot read cert file.')
76 return callback(null, cert
)
79 ], function (err
, cert
) {
80 if (err
) return next(err
)
82 return res
.json({ cert: cert
})
86 function listPods (req
, res
, next
) {
87 db
.Pod
.list(function (err
, podsList
) {
88 if (err
) return next(err
)
90 res
.json(utils
.getFormatedObjects(podsList
, podsList
.length
))
94 function makeFriends (req
, res
, next
) {
95 const hosts
= req
.body
.hosts
97 friends
.makeFriends(hosts
, function (err
) {
99 logger
.error('Could not make friends.', { error: err
})
103 logger
.info('Made friends!')
106 res
.type('json').status(204).end()
109 function removePods (req
, res
, next
) {
110 const host
= req
.body
.signature
.host
113 function loadPod (callback
) {
114 db
.Pod
.loadByHost(host
, callback
)
117 function deletePod (pod
, callback
) {
118 pod
.destroy().asCallback(callback
)
121 if (err
) return next(err
)
123 return res
.type('json').status(204).end()
127 function quitFriends (req
, res
, next
) {
128 friends
.quitFriends(function (err
) {
129 if (err
) return next(err
)
131 res
.type('json').status(204).end()