]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/pods.js
Server: use video hook to send information to other pods when a video is
[github/Chocobozzz/PeerTube.git] / server / controllers / api / pods.js
CommitLineData
9f10b292 1'use strict'
8c308c2b 2
f0f5567b 3const express = require('express')
1a42c9e2 4const waterfall = require('async/waterfall')
f0f5567b 5
feb4bdfd 6const db = require('../../initializers/database')
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()
8c308c2b 18
53572423 19router.get('/', listPods)
1ab844d8
C
20router.post('/',
21 validators.podsAdd,
49abbbbe 22 podsMiddleware.setBodyHostPort,
1ab844d8
C
23 addPods
24)
1e2564d3 25router.post('/makefriends',
9bd26629
C
26 oAuth.authenticate,
27 admin.ensureIsAdmin,
28 validators.makeFriends,
49abbbbe 29 podsMiddleware.setBodyHostsPort,
9bd26629
C
30 makeFriends
31)
32router.get('/quitfriends',
33 oAuth.authenticate,
34 admin.ensureIsAdmin,
35 quitFriends
36)
9f10b292 37// Post because this is a secured request
0eb78d53
C
38router.post('/remove',
39 signatureValidator,
40 checkSignature,
41 removePods
42)
c45f7f84 43
9f10b292 44// ---------------------------------------------------------------------------
c45f7f84 45
9f10b292 46module.exports = router
c45f7f84 47
9f10b292 48// ---------------------------------------------------------------------------
8c308c2b 49
9f10b292 50function addPods (req, res, next) {
528a9efa 51 const informations = req.body
8c308c2b 52
1a42c9e2 53 waterfall([
1cad0f39 54 function addPod (callback) {
feb4bdfd
C
55 const pod = db.Pod.build(informations)
56 pod.save().asCallback(function (err, podCreated) {
a3ee6fa2
C
57 // Be sure about the number of parameters for the callback
58 return callback(err, podCreated)
59 })
1cad0f39
C
60 },
61
528a9efa 62 function sendMyVideos (podCreated, callback) {
feb4bdfd 63 friends.sendOwnedVideosToPod(podCreated.id)
1cad0f39 64
528a9efa 65 callback(null)
1cad0f39
C
66 },
67
68 function fetchMyCertificate (callback) {
69 friends.getMyCertificate(function (err, cert) {
70 if (err) {
71 logger.error('Cannot read cert file.')
72 return callback(err)
73 }
c173e565 74
1cad0f39
C
75 return callback(null, cert)
76 })
1cad0f39 77 }
528a9efa 78 ], function (err, cert) {
1cad0f39
C
79 if (err) return next(err)
80
528a9efa 81 return res.json({ cert: cert })
9f10b292
C
82 })
83}
8c308c2b 84
53572423 85function listPods (req, res, next) {
feb4bdfd 86 db.Pod.list(function (err, podsList) {
9f10b292 87 if (err) return next(err)
45239549 88
49abbbbe 89 res.json(getFormatedPods(podsList))
9f10b292
C
90 })
91}
45239549 92
9f10b292 93function makeFriends (req, res, next) {
49abbbbe 94 const hosts = req.body.hosts
1e2564d3 95
49abbbbe 96 friends.makeFriends(hosts, function (err) {
9ab1071c
C
97 if (err) {
98 logger.error('Could not make friends.', { error: err })
99 return
100 }
45239549 101
9ab1071c 102 logger.info('Made friends!')
9f10b292 103 })
9ab1071c
C
104
105 res.type('json').status(204).end()
9f10b292 106}
45239549 107
9f10b292 108function removePods (req, res, next) {
49abbbbe 109 const host = req.body.signature.host
8c308c2b 110
1a42c9e2 111 waterfall([
a3ee6fa2 112 function loadPod (callback) {
feb4bdfd 113 db.Pod.loadByHost(host, callback)
a3ee6fa2
C
114 },
115
98ac898a 116 function deletePod (pod, callback) {
feb4bdfd 117 pod.destroy().asCallback(callback)
1cad0f39
C
118 }
119 ], function (err) {
120 if (err) return next(err)
121
122 return res.type('json').status(204).end()
9f10b292
C
123 })
124}
8c308c2b 125
9f10b292
C
126function quitFriends (req, res, next) {
127 friends.quitFriends(function (err) {
128 if (err) return next(err)
8c308c2b 129
dc8bc31b 130 res.type('json').status(204).end()
9f10b292
C
131 })
132}
53572423
C
133
134// ---------------------------------------------------------------------------
135
136function getFormatedPods (pods) {
137 const formatedPods = []
138
139 pods.forEach(function (pod) {
140 formatedPods.push(pod.toFormatedJSON())
141 })
142
143 return formatedPods
144}