]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
9f10b292 1'use strict'
8c308c2b 2
1cad0f39 3const async = require('async')
f0f5567b 4const express = require('express')
f0f5567b
C
5
6const logger = require('../../../helpers/logger')
7const friends = require('../../../lib/friends')
b3b92647 8const middlewares = require('../../../middlewares')
f0f5567b 9const Pods = require('../../../models/pods')
b3b92647
C
10const oAuth2 = middlewares.oauth2
11const reqValidator = middlewares.reqValidators.pods
528a9efa 12const signatureValidator = middlewares.reqValidators.remote.signature
cbe2f7c3 13const videos = require('../../../lib/videos')
f0f5567b
C
14const Videos = require('../../../models/videos')
15
16const router = express.Router()
8c308c2b 17
528a9efa 18router.get('/', listPodsUrl)
5dda52c9 19router.post('/', reqValidator.podsAdd, addPods)
b3b92647
C
20router.get('/makefriends', oAuth2.authenticate, reqValidator.makeFriends, makeFriends)
21router.get('/quitfriends', oAuth2.authenticate, quitFriends)
9f10b292 22// Post because this is a secured request
528a9efa 23router.post('/remove', signatureValidator, removePods)
c45f7f84 24
9f10b292 25// ---------------------------------------------------------------------------
c45f7f84 26
9f10b292 27module.exports = router
c45f7f84 28
9f10b292 29// ---------------------------------------------------------------------------
8c308c2b 30
9f10b292 31function addPods (req, res, next) {
528a9efa 32 const informations = req.body
8c308c2b 33
1cad0f39
C
34 async.waterfall([
35 function addPod (callback) {
528a9efa 36 Pods.add(informations, callback)
1cad0f39
C
37 },
38
528a9efa
C
39 function sendMyVideos (podCreated, callback) {
40 friends.sendOwnedVideosToPod(podCreated._id)
1cad0f39 41
528a9efa 42 callback(null)
1cad0f39
C
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 }
c173e565 51
1cad0f39
C
52 return callback(null, cert)
53 })
1cad0f39 54 }
528a9efa 55 ], function (err, cert) {
1cad0f39
C
56 if (err) return next(err)
57
528a9efa 58 return res.json({ cert: cert })
9f10b292
C
59 })
60}
8c308c2b 61
528a9efa
C
62function listPodsUrl (req, res, next) {
63 Pods.listAllUrls(function (err, podsUrlList) {
9f10b292 64 if (err) return next(err)
45239549 65
528a9efa 66 res.json(podsUrlList)
9f10b292
C
67 })
68}
45239549 69
9f10b292
C
70function makeFriends (req, res, next) {
71 friends.makeFriends(function (err) {
72 if (err) return next(err)
45239549 73
dc8bc31b 74 res.type('json').status(204).end()
9f10b292
C
75 })
76}
45239549 77
9f10b292 78function removePods (req, res, next) {
f0f5567b 79 const url = req.body.signature.url
8c308c2b 80
1cad0f39
C
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 },
8425cb89 98
1cad0f39 99 function removeTheRemoteVideos (videosList, callback) {
bc503c2a 100 videos.removeRemoteVideos(videosList, function (err) {
cbe2f7c3
C
101 if (err) {
102 logger.error('Cannot remove remote videos.', { error: err })
6e56eb63 103 return callback(err)
cbe2f7c3
C
104 }
105
1cad0f39 106 return callback(null)
cbe2f7c3 107 })
1cad0f39
C
108 }
109 ], function (err) {
110 if (err) return next(err)
111
112 return res.type('json').status(204).end()
9f10b292
C
113 })
114}
8c308c2b 115
9f10b292
C
116function quitFriends (req, res, next) {
117 friends.quitFriends(function (err) {
118 if (err) return next(err)
8c308c2b 119
dc8bc31b 120 res.type('json').status(204).end()
9f10b292
C
121 })
122}