]>
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 validators
= middlewares
.validators
.pods
14 const signatureValidator
= middlewares
.validators
.remote
.signature
16 const router
= express
.Router()
17 const Pod
= mongoose
.model('Pod')
18 const Video
= mongoose
.model('Video')
20 router
.get('/', listPods
)
21 router
.post('/', validators
.podsAdd
, addPods
)
22 router
.post('/makefriends',
25 validators
.makeFriends
,
28 router
.get('/quitfriends',
33 // Post because this is a secured request
34 router
.post('/remove', signatureValidator
, removePods
)
36 // ---------------------------------------------------------------------------
38 module
.exports
= router
40 // ---------------------------------------------------------------------------
42 function addPods (req
, res
, next
) {
43 const informations
= req
.body
46 function addPod (callback
) {
47 const pod
= new Pod(informations
)
48 pod
.save(function (err
, podCreated
) {
49 // Be sure about the number of parameters for the callback
50 return callback(err
, podCreated
)
54 function sendMyVideos (podCreated
, callback
) {
55 friends
.sendOwnedVideosToPod(podCreated
._id
)
60 function fetchMyCertificate (callback
) {
61 friends
.getMyCertificate(function (err
, cert
) {
63 logger
.error('Cannot read cert file.')
67 return callback(null, cert
)
70 ], function (err
, cert
) {
71 if (err
) return next(err
)
73 return res
.json({ cert: cert
})
77 function listPods (req
, res
, next
) {
78 Pod
.list(function (err
, podsUrlList
) {
79 if (err
) return next(err
)
81 res
.json(getFormatedPods(podsUrlList
))
85 function makeFriends (req
, res
, next
) {
86 const urls
= req
.body
.urls
88 friends
.makeFriends(urls
, function (err
) {
90 logger
.error('Could not make friends.', { error: err
})
94 logger
.info('Made friends!')
97 res
.type('json').status(204).end()
100 function removePods (req
, res
, next
) {
101 const url
= req
.body
.signature
.url
104 function loadPod (callback
) {
105 Pod
.loadByUrl(url
, callback
)
108 function removePod (pod
, callback
) {
109 pod
.remove(function (err
) {
110 // Be sure we only return one argument in the callback
115 function (callback
) {
116 Video
.listByUrls([ url
], function (err
, videosList
) {
118 logger
.error('Cannot list videos from url.', { error: err
})
122 return callback(null, videosList
)
126 function removeTheRemoteVideos (videosList
, callback
) {
127 each(videosList
, function (video
, callbackEach
) {
128 video
.remove(callbackEach
)
132 if (err
) return next(err
)
134 return res
.type('json').status(204).end()
138 function quitFriends (req
, res
, next
) {
139 friends
.quitFriends(function (err
) {
140 if (err
) return next(err
)
142 res
.type('json').status(204).end()
146 // ---------------------------------------------------------------------------
148 function getFormatedPods (pods
) {
149 const formatedPods
= []
151 pods
.forEach(function (pod
) {
152 formatedPods
.push(pod
.toFormatedJSON())