]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/v1/pods.js
Add radix to parseInt
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
... / ...
CommitLineData
1'use strict'
2
3const async = require('async')
4const express = require('express')
5
6const logger = require('../../../helpers/logger')
7const friends = require('../../../lib/friends')
8const middlewares = require('../../../middlewares')
9const Pods = require('../../../models/pods')
10const oAuth2 = middlewares.oauth2
11const reqValidator = middlewares.reqValidators.pods
12const secureMiddleware = middlewares.secure
13const secureRequest = middlewares.reqValidators.remote.secureRequest
14const videos = require('../../../lib/videos')
15const Videos = require('../../../models/videos')
16
17const router = express.Router()
18
19router.get('/', listPods)
20router.post('/', reqValidator.podsAdd, addPods)
21router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends)
22router.get('/quitfriends', oAuth2.authenticate, quitFriends)
23// Post because this is a secured request
24router.post('/remove', secureRequest, secureMiddleware.decryptBody, removePods)
25
26// ---------------------------------------------------------------------------
27
28module.exports = router
29
30// ---------------------------------------------------------------------------
31
32function 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
79function listPods (req, res, next) {
80 Pods.list(function (err, podsList) {
81 if (err) return next(err)
82
83 res.json(podsList)
84 })
85}
86
87function 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
95function 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
133function 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}