]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/v1/pods.js
Server: udpate async to 2.0.0
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
CommitLineData
9f10b292 1'use strict'
8c308c2b 2
1a42c9e2 3const each = require('async/each')
f0f5567b 4const express = require('express')
aaf61f38 5const mongoose = require('mongoose')
1a42c9e2 6const waterfall = require('async/waterfall')
f0f5567b
C
7
8const logger = require('../../../helpers/logger')
9const friends = require('../../../lib/friends')
b3b92647 10const middlewares = require('../../../middlewares')
69b0a27c 11const oAuth = middlewares.oauth
fc51fde0
C
12const validators = middlewares.validators.pods
13const signatureValidator = middlewares.validators.remote.signature
f0f5567b
C
14
15const router = express.Router()
a3ee6fa2 16const Pod = mongoose.model('Pod')
aaf61f38 17const Video = mongoose.model('Video')
8c308c2b 18
528a9efa 19router.get('/', listPodsUrl)
fc51fde0
C
20router.post('/', validators.podsAdd, addPods)
21router.get('/makefriends', oAuth.authenticate, validators.makeFriends, makeFriends)
69b0a27c 22router.get('/quitfriends', oAuth.authenticate, quitFriends)
9f10b292 23// Post because this is a secured request
528a9efa 24router.post('/remove', signatureValidator, removePods)
c45f7f84 25
9f10b292 26// ---------------------------------------------------------------------------
c45f7f84 27
9f10b292 28module.exports = router
c45f7f84 29
9f10b292 30// ---------------------------------------------------------------------------
8c308c2b 31
9f10b292 32function addPods (req, res, next) {
528a9efa 33 const informations = req.body
8c308c2b 34
1a42c9e2 35 waterfall([
1cad0f39 36 function addPod (callback) {
a3ee6fa2
C
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 })
1cad0f39
C
42 },
43
528a9efa
C
44 function sendMyVideos (podCreated, callback) {
45 friends.sendOwnedVideosToPod(podCreated._id)
1cad0f39 46
528a9efa 47 callback(null)
1cad0f39
C
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 }
c173e565 56
1cad0f39
C
57 return callback(null, cert)
58 })
1cad0f39 59 }
528a9efa 60 ], function (err, cert) {
1cad0f39
C
61 if (err) return next(err)
62
528a9efa 63 return res.json({ cert: cert })
9f10b292
C
64 })
65}
8c308c2b 66
528a9efa 67function listPodsUrl (req, res, next) {
a3ee6fa2 68 Pod.listOnlyUrls(function (err, podsUrlList) {
9f10b292 69 if (err) return next(err)
45239549 70
528a9efa 71 res.json(podsUrlList)
9f10b292
C
72 })
73}
45239549 74
9f10b292
C
75function makeFriends (req, res, next) {
76 friends.makeFriends(function (err) {
77 if (err) return next(err)
45239549 78
dc8bc31b 79 res.type('json').status(204).end()
9f10b292
C
80 })
81}
45239549 82
9f10b292 83function removePods (req, res, next) {
f0f5567b 84 const url = req.body.signature.url
8c308c2b 85
1a42c9e2 86 waterfall([
a3ee6fa2
C
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
1cad0f39
C
94 return callback(err)
95 })
96 },
97
98 function (callback) {
aaf61f38 99 Video.listByUrls([ url ], function (err, videosList) {
1cad0f39
C
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 },
8425cb89 108
1cad0f39 109 function removeTheRemoteVideos (videosList, callback) {
1a42c9e2 110 each(videosList, function (video, callbackEach) {
aaf61f38
C
111 video.remove(callbackEach)
112 }, callback)
1cad0f39
C
113 }
114 ], function (err) {
115 if (err) return next(err)
116
117 return res.type('json').status(204).end()
9f10b292
C
118 })
119}
8c308c2b 120
9f10b292
C
121function quitFriends (req, res, next) {
122 friends.quitFriends(function (err) {
123 if (err) return next(err)
8c308c2b 124
dc8bc31b 125 res.type('json').status(204).end()
9f10b292
C
126 })
127}