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