]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1'use strict'
2
3const express = require('express')
4const waterfall = require('async/waterfall')
5
6const db = require('../../initializers/database')
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()
18
19router.get('/', listPods)
20router.post('/',
21 validators.podsAdd,
22 podsMiddleware.setBodyHostPort,
23 addPods
24)
25router.post('/makefriends',
26 oAuth.authenticate,
27 admin.ensureIsAdmin,
28 validators.makeFriends,
29 podsMiddleware.setBodyHostsPort,
30 makeFriends
31)
32router.get('/quitfriends',
33 oAuth.authenticate,
34 admin.ensureIsAdmin,
35 quitFriends
36)
37// Post because this is a secured request
38router.post('/remove',
39 signatureValidator,
40 checkSignature,
41 removePods
42)
43
44// ---------------------------------------------------------------------------
45
46module.exports = router
47
48// ---------------------------------------------------------------------------
49
50function addPods (req, res, next) {
51 const informations = req.body
52
53 waterfall([
54 function addPod (callback) {
55 const pod = db.Pod.build(informations)
56 pod.save().asCallback(function (err, podCreated) {
57 // Be sure about the number of parameters for the callback
58 return callback(err, podCreated)
59 })
60 },
61
62 function sendMyVideos (podCreated, callback) {
63 friends.sendOwnedVideosToPod(podCreated.id)
64
65 callback(null)
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 }
74
75 return callback(null, cert)
76 })
77 }
78 ], function (err, cert) {
79 if (err) return next(err)
80
81 return res.json({ cert: cert })
82 })
83}
84
85function listPods (req, res, next) {
86 db.Pod.list(function (err, podsList) {
87 if (err) return next(err)
88
89 res.json(getFormatedPods(podsList))
90 })
91}
92
93function makeFriends (req, res, next) {
94 const hosts = req.body.hosts
95
96 friends.makeFriends(hosts, function (err) {
97 if (err) {
98 logger.error('Could not make friends.', { error: err })
99 return
100 }
101
102 logger.info('Made friends!')
103 })
104
105 res.type('json').status(204).end()
106}
107
108function removePods (req, res, next) {
109 const host = req.body.signature.host
110
111 waterfall([
112 function loadPod (callback) {
113 db.Pod.loadByHost(host, callback)
114 },
115
116 function deletePod (pod, callback) {
117 pod.destroy().asCallback(callback)
118 }
119 ], function (err) {
120 if (err) return next(err)
121
122 return res.type('json').status(204).end()
123 })
124}
125
126function quitFriends (req, res, next) {
127 friends.quitFriends(function (err) {
128 if (err) return next(err)
129
130 res.type('json').status(204).end()
131 })
132}
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}