]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/pods.js
3 const each
= require('async/each')
4 const express
= require('express')
5 const mongoose
= require('mongoose')
6 const waterfall
= require('async/waterfall')
8 const logger
= require('../../../helpers/logger')
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()
19 const Pod
= mongoose
.model('Pod')
20 const Video
= mongoose
.model('Video')
22 router
.get('/', listPods
)
25 podsMiddleware
.setBodyUrlPort
,
28 router
.post('/makefriends',
31 validators
.makeFriends
,
32 podsMiddleware
.setBodyUrlsPort
,
35 router
.get('/quitfriends',
40 // Post because this is a secured request
41 router
.post('/remove',
47 // ---------------------------------------------------------------------------
49 module
.exports
= router
51 // ---------------------------------------------------------------------------
53 function addPods (req
, res
, next
) {
54 const informations
= req
.body
57 function addPod (callback
) {
58 const pod
= new Pod(informations
)
59 pod
.save(function (err
, podCreated
) {
60 // Be sure about the number of parameters for the callback
61 return callback(err
, podCreated
)
65 function sendMyVideos (podCreated
, callback
) {
66 friends
.sendOwnedVideosToPod(podCreated
._id
)
71 function fetchMyCertificate (callback
) {
72 friends
.getMyCertificate(function (err
, cert
) {
74 logger
.error('Cannot read cert file.')
78 return callback(null, cert
)
81 ], function (err
, cert
) {
82 if (err
) return next(err
)
84 return res
.json({ cert: cert
})
88 function listPods (req
, res
, next
) {
89 Pod
.list(function (err
, podsUrlList
) {
90 if (err
) return next(err
)
92 res
.json(getFormatedPods(podsUrlList
))
96 function makeFriends (req
, res
, next
) {
97 const urls
= req
.body
.urls
99 friends
.makeFriends(urls
, function (err
) {
101 logger
.error('Could not make friends.', { error: err
})
105 logger
.info('Made friends!')
108 res
.type('json').status(204).end()
111 function removePods (req
, res
, next
) {
112 const url
= req
.body
.signature
.url
115 function loadPod (callback
) {
116 Pod
.loadByUrl(url
, callback
)
119 function removePod (pod
, callback
) {
120 pod
.remove(function (err
) {
121 // Be sure we only return one argument in the callback
126 function (callback
) {
127 Video
.listByUrls([ url
], function (err
, videosList
) {
129 logger
.error('Cannot list videos from url.', { error: err
})
133 return callback(null, videosList
)
137 function removeTheRemoteVideos (videosList
, callback
) {
138 each(videosList
, function (video
, callbackEach
) {
139 video
.remove(callbackEach
)
143 if (err
) return next(err
)
145 return res
.type('json').status(204).end()
149 function quitFriends (req
, res
, next
) {
150 friends
.quitFriends(function (err
) {
151 if (err
) return next(err
)
153 res
.type('json').status(204).end()
157 // ---------------------------------------------------------------------------
159 function getFormatedPods (pods
) {
160 const formatedPods
= []
162 pods
.forEach(function (pod
) {
163 formatedPods
.push(pod
.toFormatedJSON())