]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/pods.js
e216714d6addf3abcc0d16996236631a5b8f303e
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
1 'use strict'
2
3 const async = require('async')
4 const express = require('express')
5
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')
16
17 const router = express.Router()
18
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)
25
26 // ---------------------------------------------------------------------------
27
28 module.exports = router
29
30 // ---------------------------------------------------------------------------
31
32 function addPods (req, res, next) {
33 const informations = req.body.data
34
35 async.waterfall([
36 function addPod (callback) {
37 Pods.add(informations, function (err) {
38 return callback(err)
39 })
40 },
41
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 })
46
47 return callback(err)
48 })
49 },
50
51 function fetchMyCertificate (callback) {
52 friends.getMyCertificate(function (err, cert) {
53 if (err) {
54 logger.error('Cannot read cert file.')
55 return callback(err)
56 }
57
58 return callback(null, cert)
59 })
60 },
61
62 function getListOfMyVideos (cert, callback) {
63 Videos.listOwned(function (err, videosList) {
64 if (err) {
65 logger.error('Cannot get the list of owned videos.')
66 return callback(err)
67 }
68
69 return callback(null, cert, videosList)
70 })
71 }
72 ], function (err, cert, videosList) {
73 if (err) return next(err)
74
75 return res.json({ cert: cert, videos: videosList })
76 })
77 }
78
79 function listPods (req, res, next) {
80 Pods.list(function (err, podsList) {
81 if (err) return next(err)
82
83 res.json(podsList)
84 })
85 }
86
87 function makeFriends (req, res, next) {
88 friends.makeFriends(function (err) {
89 if (err) return next(err)
90
91 res.type('json').status(204).end()
92 })
93 }
94
95 function removePods (req, res, next) {
96 const url = req.body.signature.url
97
98 async.waterfall([
99 function (callback) {
100 Pods.remove(url, function (err) {
101 return callback(err)
102 })
103 },
104
105 function (callback) {
106 Videos.listFromUrl(url, function (err, videosList) {
107 if (err) {
108 logger.error('Cannot list videos from url.', { error: err })
109 return callback(err)
110 }
111
112 return callback(null, videosList)
113 })
114 },
115
116 function removeTheRemoteVideos (videosList, callback) {
117 videos.removeRemoteVideos(videosList, function (err) {
118 if (err) {
119 logger.error('Cannot remove remote videos.', { error: err })
120 callback(err)
121 }
122
123 return callback(null)
124 })
125 }
126 ], function (err) {
127 if (err) return next(err)
128
129 return res.type('json').status(204).end()
130 })
131 }
132
133 function quitFriends (req, res, next) {
134 friends.quitFriends(function (err) {
135 if (err) return next(err)
136
137 res.type('json').status(204).end()
138 })
139 }