]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/v1/pods.js
Try to make a better communication (between pods) module
[github/Chocobozzz/PeerTube.git] / server / controllers / api / v1 / pods.js
... / ...
CommitLineData
1'use strict'
2
3const async = require('async')
4const express = require('express')
5
6const logger = require('../../../helpers/logger')
7const friends = require('../../../lib/friends')
8const middlewares = require('../../../middlewares')
9const Pods = require('../../../models/pods')
10const oAuth2 = middlewares.oauth2
11const reqValidator = middlewares.reqValidators.pods
12const signatureValidator = middlewares.reqValidators.remote.signature
13const videos = require('../../../lib/videos')
14const Videos = require('../../../models/videos')
15
16const router = express.Router()
17
18router.get('/', listPodsUrl)
19router.post('/', reqValidator.podsAdd, addPods)
20router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends)
21router.get('/quitfriends', oAuth2.authenticate, quitFriends)
22// Post because this is a secured request
23router.post('/remove', signatureValidator, removePods)
24
25// ---------------------------------------------------------------------------
26
27module.exports = router
28
29// ---------------------------------------------------------------------------
30
31function addPods (req, res, next) {
32 const informations = req.body
33
34 async.waterfall([
35 function addPod (callback) {
36 Pods.add(informations, callback)
37 },
38
39 function sendMyVideos (podCreated, callback) {
40 friends.sendOwnedVideosToPod(podCreated._id)
41
42 callback(null)
43 },
44
45 function fetchMyCertificate (callback) {
46 friends.getMyCertificate(function (err, cert) {
47 if (err) {
48 logger.error('Cannot read cert file.')
49 return callback(err)
50 }
51
52 return callback(null, cert)
53 })
54 }
55 ], function (err, cert) {
56 if (err) return next(err)
57
58 return res.json({ cert: cert })
59 })
60}
61
62function listPodsUrl (req, res, next) {
63 Pods.listAllUrls(function (err, podsUrlList) {
64 if (err) return next(err)
65
66 res.json(podsUrlList)
67 })
68}
69
70function makeFriends (req, res, next) {
71 friends.makeFriends(function (err) {
72 if (err) return next(err)
73
74 res.type('json').status(204).end()
75 })
76}
77
78function removePods (req, res, next) {
79 const url = req.body.signature.url
80
81 async.waterfall([
82 function (callback) {
83 Pods.remove(url, function (err) {
84 return callback(err)
85 })
86 },
87
88 function (callback) {
89 Videos.listFromUrl(url, function (err, videosList) {
90 if (err) {
91 logger.error('Cannot list videos from url.', { error: err })
92 return callback(err)
93 }
94
95 return callback(null, videosList)
96 })
97 },
98
99 function removeTheRemoteVideos (videosList, callback) {
100 videos.removeRemoteVideos(videosList, function (err) {
101 if (err) {
102 logger.error('Cannot remove remote videos.', { error: err })
103 return callback(err)
104 }
105
106 return callback(null)
107 })
108 }
109 ], function (err) {
110 if (err) return next(err)
111
112 return res.type('json').status(204).end()
113 })
114}
115
116function quitFriends (req, res, next) {
117 friends.quitFriends(function (err) {
118 if (err) return next(err)
119
120 res.type('json').status(204).end()
121 })
122}