]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/pods.js
Server: host -> hostname (host = hostname + port)
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.js
CommitLineData
9f10b292 1'use strict'
8c308c2b 2
f0f5567b 3const express = require('express')
aaf61f38 4const mongoose = require('mongoose')
1a42c9e2 5const waterfall = require('async/waterfall')
f0f5567b 6
f253b1c1
C
7const logger = require('../../helpers/logger')
8const friends = require('../../lib/friends')
9const middlewares = require('../../middlewares')
9bd26629 10const admin = middlewares.admin
69b0a27c 11const oAuth = middlewares.oauth
1ab844d8 12const podsMiddleware = middlewares.pods
0eb78d53 13const checkSignature = middlewares.secure.checkSignature
fc51fde0
C
14const validators = middlewares.validators.pods
15const signatureValidator = middlewares.validators.remote.signature
f0f5567b
C
16
17const router = express.Router()
a3ee6fa2 18const Pod = mongoose.model('Pod')
8c308c2b 19
53572423 20router.get('/', listPods)
1ab844d8
C
21router.post('/',
22 validators.podsAdd,
23 podsMiddleware.setBodyUrlPort,
24 addPods
25)
1e2564d3 26router.post('/makefriends',
9bd26629
C
27 oAuth.authenticate,
28 admin.ensureIsAdmin,
29 validators.makeFriends,
1ab844d8 30 podsMiddleware.setBodyUrlsPort,
9bd26629
C
31 makeFriends
32)
33router.get('/quitfriends',
34 oAuth.authenticate,
35 admin.ensureIsAdmin,
36 quitFriends
37)
9f10b292 38// Post because this is a secured request
0eb78d53
C
39router.post('/remove',
40 signatureValidator,
41 checkSignature,
42 removePods
43)
c45f7f84 44
9f10b292 45// ---------------------------------------------------------------------------
c45f7f84 46
9f10b292 47module.exports = router
c45f7f84 48
9f10b292 49// ---------------------------------------------------------------------------
8c308c2b 50
9f10b292 51function addPods (req, res, next) {
528a9efa 52 const informations = req.body
8c308c2b 53
1a42c9e2 54 waterfall([
1cad0f39 55 function addPod (callback) {
a3ee6fa2
C
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 })
1cad0f39
C
61 },
62
528a9efa
C
63 function sendMyVideos (podCreated, callback) {
64 friends.sendOwnedVideosToPod(podCreated._id)
1cad0f39 65
528a9efa 66 callback(null)
1cad0f39
C
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 }
c173e565 75
1cad0f39
C
76 return callback(null, cert)
77 })
1cad0f39 78 }
528a9efa 79 ], function (err, cert) {
1cad0f39
C
80 if (err) return next(err)
81
528a9efa 82 return res.json({ cert: cert })
9f10b292
C
83 })
84}
8c308c2b 85
53572423
C
86function listPods (req, res, next) {
87 Pod.list(function (err, podsUrlList) {
9f10b292 88 if (err) return next(err)
45239549 89
53572423 90 res.json(getFormatedPods(podsUrlList))
9f10b292
C
91 })
92}
45239549 93
9f10b292 94function makeFriends (req, res, next) {
1e2564d3
C
95 const urls = req.body.urls
96
97 friends.makeFriends(urls, function (err) {
9ab1071c
C
98 if (err) {
99 logger.error('Could not make friends.', { error: err })
100 return
101 }
45239549 102
9ab1071c 103 logger.info('Made friends!')
9f10b292 104 })
9ab1071c
C
105
106 res.type('json').status(204).end()
9f10b292 107}
45239549 108
9f10b292 109function removePods (req, res, next) {
f0f5567b 110 const url = req.body.signature.url
8c308c2b 111
1a42c9e2 112 waterfall([
a3ee6fa2
C
113 function loadPod (callback) {
114 Pod.loadByUrl(url, callback)
115 },
116
117 function removePod (pod, callback) {
80a6c9e7 118 pod.remove(callback)
1cad0f39
C
119 }
120 ], function (err) {
121 if (err) return next(err)
122
123 return res.type('json').status(204).end()
9f10b292
C
124 })
125}
8c308c2b 126
9f10b292
C
127function quitFriends (req, res, next) {
128 friends.quitFriends(function (err) {
129 if (err) return next(err)
8c308c2b 130
dc8bc31b 131 res.type('json').status(204).end()
9f10b292
C
132 })
133}
53572423
C
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}