]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/pods.js
Server: add port when making friends if it is not specified
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
1 'use strict'
2
3 const each = require('async/each')
4 const express = require('express')
5 const mongoose = require('mongoose')
6 const waterfall = require('async/waterfall')
7
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 podsMiddleware = middlewares.pods
14 const checkSignature = middlewares.secure.checkSignature
15 const validators = middlewares.validators.pods
16 const signatureValidator = middlewares.validators.remote.signature
17
18 const router = express.Router()
19 const Pod = mongoose.model('Pod')
20 const Video = mongoose.model('Video')
21
22 router.get('/', listPods)
23 router.post('/',
24 validators.podsAdd,
25 podsMiddleware.setBodyUrlPort,
26 addPods
27 )
28 router.post('/makefriends',
29 oAuth.authenticate,
30 admin.ensureIsAdmin,
31 validators.makeFriends,
32 podsMiddleware.setBodyUrlsPort,
33 makeFriends
34 )
35 router.get('/quitfriends',
36 oAuth.authenticate,
37 admin.ensureIsAdmin,
38 quitFriends
39 )
40 // Post because this is a secured request
41 router.post('/remove',
42 signatureValidator,
43 checkSignature,
44 removePods
45 )
46
47 // ---------------------------------------------------------------------------
48
49 module.exports = router
50
51 // ---------------------------------------------------------------------------
52
53 function addPods (req, res, next) {
54 const informations = req.body
55
56 waterfall([
57 function addPod (callback) {
58 const pod = new Pod(informations)
59 pod.save(function (err, podCreated) {
60 // Be sure about the number of parameters for the callback
61 return callback(err, podCreated)
62 })
63 },
64
65 function sendMyVideos (podCreated, callback) {
66 friends.sendOwnedVideosToPod(podCreated._id)
67
68 callback(null)
69 },
70
71 function fetchMyCertificate (callback) {
72 friends.getMyCertificate(function (err, cert) {
73 if (err) {
74 logger.error('Cannot read cert file.')
75 return callback(err)
76 }
77
78 return callback(null, cert)
79 })
80 }
81 ], function (err, cert) {
82 if (err) return next(err)
83
84 return res.json({ cert: cert })
85 })
86 }
87
88 function listPods (req, res, next) {
89 Pod.list(function (err, podsUrlList) {
90 if (err) return next(err)
91
92 res.json(getFormatedPods(podsUrlList))
93 })
94 }
95
96 function makeFriends (req, res, next) {
97 const urls = req.body.urls
98
99 friends.makeFriends(urls, function (err) {
100 if (err) {
101 logger.error('Could not make friends.', { error: err })
102 return
103 }
104
105 logger.info('Made friends!')
106 })
107
108 res.type('json').status(204).end()
109 }
110
111 function removePods (req, res, next) {
112 const url = req.body.signature.url
113
114 waterfall([
115 function loadPod (callback) {
116 Pod.loadByUrl(url, callback)
117 },
118
119 function removePod (pod, callback) {
120 pod.remove(function (err) {
121 // Be sure we only return one argument in the callback
122 return callback(err)
123 })
124 },
125
126 function (callback) {
127 Video.listByUrls([ url ], function (err, videosList) {
128 if (err) {
129 logger.error('Cannot list videos from url.', { error: err })
130 return callback(err)
131 }
132
133 return callback(null, videosList)
134 })
135 },
136
137 function removeTheRemoteVideos (videosList, callback) {
138 each(videosList, function (video, callbackEach) {
139 video.remove(callbackEach)
140 }, callback)
141 }
142 ], function (err) {
143 if (err) return next(err)
144
145 return res.type('json').status(204).end()
146 })
147 }
148
149 function quitFriends (req, res, next) {
150 friends.quitFriends(function (err) {
151 if (err) return next(err)
152
153 res.type('json').status(204).end()
154 })
155 }
156
157 // ---------------------------------------------------------------------------
158
159 function getFormatedPods (pods) {
160 const formatedPods = []
161
162 pods.forEach(function (pod) {
163 formatedPods.push(pod.toFormatedJSON())
164 })
165
166 return formatedPods
167 }