]>
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 checkSignature
= middlewares
.secure
.checkSignature
14 const validators
= middlewares
.validators
.pods
15 const signatureValidator
= middlewares
.validators
.remote
.signature
17 const router
= express
.Router()
18 const Pod
= mongoose
.model('Pod')
19 const Video
= mongoose
.model('Video')
21 router
.get('/', listPods
)
22 router
.post('/', validators
.podsAdd
, addPods
)
23 router
.post('/makefriends',
26 validators
.makeFriends
,
29 router
.get('/quitfriends',
34 // Post because this is a secured request
35 router
.post('/remove',
41 // ---------------------------------------------------------------------------
43 module
.exports
= router
45 // ---------------------------------------------------------------------------
47 function addPods (req
, res
, next
) {
48 const informations
= req
.body
51 function addPod (callback
) {
52 const pod
= new Pod(informations
)
53 pod
.save(function (err
, podCreated
) {
54 // Be sure about the number of parameters for the callback
55 return callback(err
, podCreated
)
59 function sendMyVideos (podCreated
, callback
) {
60 friends
.sendOwnedVideosToPod(podCreated
._id
)
65 function fetchMyCertificate (callback
) {
66 friends
.getMyCertificate(function (err
, cert
) {
68 logger
.error('Cannot read cert file.')
72 return callback(null, cert
)
75 ], function (err
, cert
) {
76 if (err
) return next(err
)
78 return res
.json({ cert: cert
})
82 function listPods (req
, res
, next
) {
83 Pod
.list(function (err
, podsUrlList
) {
84 if (err
) return next(err
)
86 res
.json(getFormatedPods(podsUrlList
))
90 function makeFriends (req
, res
, next
) {
91 const urls
= req
.body
.urls
93 friends
.makeFriends(urls
, function (err
) {
95 logger
.error('Could not make friends.', { error: err
})
99 logger
.info('Made friends!')
102 res
.type('json').status(204).end()
105 function removePods (req
, res
, next
) {
106 const url
= req
.body
.signature
.url
109 function loadPod (callback
) {
110 Pod
.loadByUrl(url
, callback
)
113 function removePod (pod
, callback
) {
114 pod
.remove(function (err
) {
115 // Be sure we only return one argument in the callback
120 function (callback
) {
121 Video
.listByUrls([ url
], function (err
, videosList
) {
123 logger
.error('Cannot list videos from url.', { error: err
})
127 return callback(null, videosList
)
131 function removeTheRemoteVideos (videosList
, callback
) {
132 each(videosList
, function (video
, callbackEach
) {
133 video
.remove(callbackEach
)
137 if (err
) return next(err
)
139 return res
.type('json').status(204).end()
143 function quitFriends (req
, res
, next
) {
144 friends
.quitFriends(function (err
) {
145 if (err
) return next(err
)
147 res
.type('json').status(204).end()
151 // ---------------------------------------------------------------------------
153 function getFormatedPods (pods
) {
154 const formatedPods
= []
156 pods
.forEach(function (pod
) {
157 formatedPods
.push(pod
.toFormatedJSON())