]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/pods.js
Server: host -> hostname (host = hostname + port)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.js
... / ...
CommitLineData
1'use strict'
2
3const express = require('express')
4const mongoose = require('mongoose')
5const waterfall = require('async/waterfall')
6
7const logger = require('../../helpers/logger')
8const friends = require('../../lib/friends')
9const middlewares = require('../../middlewares')
10const admin = middlewares.admin
11const oAuth = middlewares.oauth
12const podsMiddleware = middlewares.pods
13const checkSignature = middlewares.secure.checkSignature
14const validators = middlewares.validators.pods
15const signatureValidator = middlewares.validators.remote.signature
16
17const router = express.Router()
18const Pod = mongoose.model('Pod')
19
20router.get('/', listPods)
21router.post('/',
22 validators.podsAdd,
23 podsMiddleware.setBodyUrlPort,
24 addPods
25)
26router.post('/makefriends',
27 oAuth.authenticate,
28 admin.ensureIsAdmin,
29 validators.makeFriends,
30 podsMiddleware.setBodyUrlsPort,
31 makeFriends
32)
33router.get('/quitfriends',
34 oAuth.authenticate,
35 admin.ensureIsAdmin,
36 quitFriends
37)
38// Post because this is a secured request
39router.post('/remove',
40 signatureValidator,
41 checkSignature,
42 removePods
43)
44
45// ---------------------------------------------------------------------------
46
47module.exports = router
48
49// ---------------------------------------------------------------------------
50
51function addPods (req, res, next) {
52 const informations = req.body
53
54 waterfall([
55 function addPod (callback) {
56 const pod = new Pod(informations)
57 pod.save(function (err, podCreated) {
58 // Be sure about the number of parameters for the callback
59 return callback(err, podCreated)
60 })
61 },
62
63 function sendMyVideos (podCreated, callback) {
64 friends.sendOwnedVideosToPod(podCreated._id)
65
66 callback(null)
67 },
68
69 function fetchMyCertificate (callback) {
70 friends.getMyCertificate(function (err, cert) {
71 if (err) {
72 logger.error('Cannot read cert file.')
73 return callback(err)
74 }
75
76 return callback(null, cert)
77 })
78 }
79 ], function (err, cert) {
80 if (err) return next(err)
81
82 return res.json({ cert: cert })
83 })
84}
85
86function listPods (req, res, next) {
87 Pod.list(function (err, podsUrlList) {
88 if (err) return next(err)
89
90 res.json(getFormatedPods(podsUrlList))
91 })
92}
93
94function makeFriends (req, res, next) {
95 const urls = req.body.urls
96
97 friends.makeFriends(urls, function (err) {
98 if (err) {
99 logger.error('Could not make friends.', { error: err })
100 return
101 }
102
103 logger.info('Made friends!')
104 })
105
106 res.type('json').status(204).end()
107}
108
109function removePods (req, res, next) {
110 const url = req.body.signature.url
111
112 waterfall([
113 function loadPod (callback) {
114 Pod.loadByUrl(url, callback)
115 },
116
117 function removePod (pod, callback) {
118 pod.remove(callback)
119 }
120 ], function (err) {
121 if (err) return next(err)
122
123 return res.type('json').status(204).end()
124 })
125}
126
127function quitFriends (req, res, next) {
128 friends.quitFriends(function (err) {
129 if (err) return next(err)
130
131 res.type('json').status(204).end()
132 })
133}
134
135// ---------------------------------------------------------------------------
136
137function getFormatedPods (pods) {
138 const formatedPods = []
139
140 pods.forEach(function (pod) {
141 formatedPods.push(pod.toFormatedJSON())
142 })
143
144 return formatedPods
145}