]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/v1/pods.js
Server: make friends urls come from the request instead of the
[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 validators = middlewares.validators.pods
14 const signatureValidator = middlewares.validators.remote.signature
15
16 const router = express.Router()
17 const Pod = mongoose.model('Pod')
18 const Video = mongoose.model('Video')
19
20 router.get('/', listPodsUrl)
21 router.post('/', validators.podsAdd, addPods)
22 router.post('/makefriends',
23 oAuth.authenticate,
24 admin.ensureIsAdmin,
25 validators.makeFriends,
26 makeFriends
27 )
28 router.get('/quitfriends',
29 oAuth.authenticate,
30 admin.ensureIsAdmin,
31 quitFriends
32 )
33 // Post because this is a secured request
34 router.post('/remove', signatureValidator, removePods)
35
36 // ---------------------------------------------------------------------------
37
38 module.exports = router
39
40 // ---------------------------------------------------------------------------
41
42 function addPods (req, res, next) {
43 const informations = req.body
44
45 waterfall([
46 function addPod (callback) {
47 const pod = new Pod(informations)
48 pod.save(function (err, podCreated) {
49 // Be sure about the number of parameters for the callback
50 return callback(err, podCreated)
51 })
52 },
53
54 function sendMyVideos (podCreated, callback) {
55 friends.sendOwnedVideosToPod(podCreated._id)
56
57 callback(null)
58 },
59
60 function fetchMyCertificate (callback) {
61 friends.getMyCertificate(function (err, cert) {
62 if (err) {
63 logger.error('Cannot read cert file.')
64 return callback(err)
65 }
66
67 return callback(null, cert)
68 })
69 }
70 ], function (err, cert) {
71 if (err) return next(err)
72
73 return res.json({ cert: cert })
74 })
75 }
76
77 function listPodsUrl (req, res, next) {
78 Pod.listOnlyUrls(function (err, podsUrlList) {
79 if (err) return next(err)
80
81 res.json(podsUrlList)
82 })
83 }
84
85 function makeFriends (req, res, next) {
86 const urls = req.body.urls
87
88 friends.makeFriends(urls, 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 waterfall([
99 function loadPod (callback) {
100 Pod.loadByUrl(url, callback)
101 },
102
103 function removePod (pod, callback) {
104 pod.remove(function (err) {
105 // Be sure we only return one argument in the callback
106 return callback(err)
107 })
108 },
109
110 function (callback) {
111 Video.listByUrls([ url ], function (err, videosList) {
112 if (err) {
113 logger.error('Cannot list videos from url.', { error: err })
114 return callback(err)
115 }
116
117 return callback(null, videosList)
118 })
119 },
120
121 function removeTheRemoteVideos (videosList, callback) {
122 each(videosList, function (video, callbackEach) {
123 video.remove(callbackEach)
124 }, callback)
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 }