]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/pods.js
feb6bd958be480a3cdd66aff3e607f000283508c
3 const async
= require('async')
4 const express
= require('express')
5 const mongoose
= require('mongoose')
7 const logger
= require('../../../helpers/logger')
8 const friends
= require('../../../lib/friends')
9 const middlewares
= require('../../../middlewares')
10 const oAuth2
= middlewares
.oauth2
11 const reqValidator
= middlewares
.reqValidators
.pods
12 const signatureValidator
= middlewares
.reqValidators
.remote
.signature
14 const router
= express
.Router()
15 const Pod
= mongoose
.model('Pod')
16 const Video
= mongoose
.model('Video')
18 router
.get('/', listPodsUrl
)
19 router
.post('/', reqValidator
.podsAdd
, addPods
)
20 router
.get('/makefriends', oAuth2
.authenticate
, reqValidator
.makeFriends
, makeFriends
)
21 router
.get('/quitfriends', oAuth2
.authenticate
, quitFriends
)
22 // Post because this is a secured request
23 router
.post('/remove', signatureValidator
, removePods
)
25 // ---------------------------------------------------------------------------
27 module
.exports
= router
29 // ---------------------------------------------------------------------------
31 function addPods (req
, res
, next
) {
32 const informations
= req
.body
35 function addPod (callback
) {
36 const pod
= new Pod(informations
)
37 pod
.save(function (err
, podCreated
) {
38 // Be sure about the number of parameters for the callback
39 return callback(err
, podCreated
)
43 function sendMyVideos (podCreated
, callback
) {
44 friends
.sendOwnedVideosToPod(podCreated
._id
)
49 function fetchMyCertificate (callback
) {
50 friends
.getMyCertificate(function (err
, cert
) {
52 logger
.error('Cannot read cert file.')
56 return callback(null, cert
)
59 ], function (err
, cert
) {
60 if (err
) return next(err
)
62 return res
.json({ cert: cert
})
66 function listPodsUrl (req
, res
, next
) {
67 Pod
.listOnlyUrls(function (err
, podsUrlList
) {
68 if (err
) return next(err
)
74 function makeFriends (req
, res
, next
) {
75 friends
.makeFriends(function (err
) {
76 if (err
) return next(err
)
78 res
.type('json').status(204).end()
82 function removePods (req
, res
, next
) {
83 const url
= req
.body
.signature
.url
86 function loadPod (callback
) {
87 Pod
.loadByUrl(url
, callback
)
90 function removePod (pod
, callback
) {
91 pod
.remove(function (err
) {
92 // Be sure we only return one argument in the callback
98 Video
.listByUrls([ url
], function (err
, videosList
) {
100 logger
.error('Cannot list videos from url.', { error: err
})
104 return callback(null, videosList
)
108 function removeTheRemoteVideos (videosList
, callback
) {
109 async
.each(videosList
, function (video
, callbackEach
) {
110 video
.remove(callbackEach
)
114 if (err
) return next(err
)
116 return res
.type('json').status(204).end()
120 function quitFriends (req
, res
, next
) {
121 friends
.quitFriends(function (err
) {
122 if (err
) return next(err
)
124 res
.type('json').status(204).end()