]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/pods.js
e216714d6addf3abcc0d16996236631a5b8f303e
3 const async
= require('async')
4 const express
= require('express')
6 const logger
= require('../../../helpers/logger')
7 const friends
= require('../../../lib/friends')
8 const middlewares
= require('../../../middlewares')
9 const Pods
= require('../../../models/pods')
10 const oAuth2
= middlewares
.oauth2
11 const reqValidator
= middlewares
.reqValidators
.pods
12 const secureMiddleware
= middlewares
.secure
13 const secureRequest
= middlewares
.reqValidators
.remote
.secureRequest
14 const videos
= require('../../../lib/videos')
15 const Videos
= require('../../../models/videos')
17 const router
= express
.Router()
19 router
.get('/', listPods
)
20 router
.post('/', reqValidator
.podsAdd
, addPods
)
21 router
.get('/makefriends', oAuth2
.authenticate
, reqValidator
.makeFriends
, makeFriends
)
22 router
.get('/quitfriends', oAuth2
.authenticate
, quitFriends
)
23 // Post because this is a secured request
24 router
.post('/remove', secureRequest
, secureMiddleware
.decryptBody
, removePods
)
26 // ---------------------------------------------------------------------------
28 module
.exports
= router
30 // ---------------------------------------------------------------------------
32 function addPods (req
, res
, next
) {
33 const informations
= req
.body
.data
36 function addPod (callback
) {
37 Pods
.add(informations
, function (err
) {
42 function createVideosOfThisPod (callback
) {
43 // Create the remote videos from the new pod
44 videos
.createRemoteVideos(informations
.videos
, function (err
) {
45 if (err
) logger
.error('Cannot create remote videos.', { error: err
})
51 function fetchMyCertificate (callback
) {
52 friends
.getMyCertificate(function (err
, cert
) {
54 logger
.error('Cannot read cert file.')
58 return callback(null, cert
)
62 function getListOfMyVideos (cert
, callback
) {
63 Videos
.listOwned(function (err
, videosList
) {
65 logger
.error('Cannot get the list of owned videos.')
69 return callback(null, cert
, videosList
)
72 ], function (err
, cert
, videosList
) {
73 if (err
) return next(err
)
75 return res
.json({ cert: cert
, videos: videosList
})
79 function listPods (req
, res
, next
) {
80 Pods
.list(function (err
, podsList
) {
81 if (err
) return next(err
)
87 function makeFriends (req
, res
, next
) {
88 friends
.makeFriends(function (err
) {
89 if (err
) return next(err
)
91 res
.type('json').status(204).end()
95 function removePods (req
, res
, next
) {
96 const url
= req
.body
.signature
.url
100 Pods
.remove(url
, function (err
) {
105 function (callback
) {
106 Videos
.listFromUrl(url
, function (err
, videosList
) {
108 logger
.error('Cannot list videos from url.', { error: err
})
112 return callback(null, videosList
)
116 function removeTheRemoteVideos (videosList
, callback
) {
117 videos
.removeRemoteVideos(videosList
, function (err
) {
119 logger
.error('Cannot remove remote videos.', { error: err
})
123 return callback(null)
127 if (err
) return next(err
)
129 return res
.type('json').status(204).end()
133 function quitFriends (req
, res
, next
) {
134 friends
.quitFriends(function (err
) {
135 if (err
) return next(err
)
137 res
.type('json').status(204).end()