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