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