]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/api/pods.js
Format video blacklist
[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')
4793c343 7const constants = require('../../initializers/constants')
f253b1c1 8const logger = require('../../helpers/logger')
15103f11 9const peertubeCrypto = require('../../helpers/peertube-crypto')
55fa55a9 10const utils = require('../../helpers/utils')
f253b1c1
C
11const friends = require('../../lib/friends')
12const middlewares = require('../../middlewares')
9bd26629 13const admin = middlewares.admin
69b0a27c 14const oAuth = middlewares.oauth
1ab844d8 15const podsMiddleware = middlewares.pods
fc51fde0 16const validators = middlewares.validators.pods
f0f5567b
C
17
18const router = express.Router()
8c308c2b 19
53572423 20router.get('/', listPods)
1ab844d8 21router.post('/',
b09ce645 22 podsMiddleware.setBodyHostPort, // We need to modify the host before running the validator!
1ab844d8 23 validators.podsAdd,
1ab844d8
C
24 addPods
25)
1e2564d3 26router.post('/makefriends',
9bd26629
C
27 oAuth.authenticate,
28 admin.ensureIsAdmin,
29 validators.makeFriends,
49abbbbe 30 podsMiddleware.setBodyHostsPort,
9bd26629
C
31 makeFriends
32)
33router.get('/quitfriends',
34 oAuth.authenticate,
35 admin.ensureIsAdmin,
36 quitFriends
37)
c45f7f84 38
9f10b292 39// ---------------------------------------------------------------------------
c45f7f84 40
9f10b292 41module.exports = router
c45f7f84 42
9f10b292 43// ---------------------------------------------------------------------------
8c308c2b 44
9f10b292 45function addPods (req, res, next) {
528a9efa 46 const informations = req.body
8c308c2b 47
1a42c9e2 48 waterfall([
1cad0f39 49 function addPod (callback) {
feb4bdfd
C
50 const pod = db.Pod.build(informations)
51 pod.save().asCallback(function (err, podCreated) {
a3ee6fa2
C
52 // Be sure about the number of parameters for the callback
53 return callback(err, podCreated)
54 })
1cad0f39
C
55 },
56
528a9efa 57 function sendMyVideos (podCreated, callback) {
feb4bdfd 58 friends.sendOwnedVideosToPod(podCreated.id)
1cad0f39 59
528a9efa 60 callback(null)
1cad0f39
C
61 },
62
63 function fetchMyCertificate (callback) {
15103f11 64 peertubeCrypto.getMyPublicCert(function (err, cert) {
1cad0f39
C
65 if (err) {
66 logger.error('Cannot read cert file.')
67 return callback(err)
68 }
c173e565 69
1cad0f39
C
70 return callback(null, cert)
71 })
1cad0f39 72 }
528a9efa 73 ], function (err, cert) {
1cad0f39
C
74 if (err) return next(err)
75
4793c343 76 return res.json({ cert: cert, email: constants.CONFIG.ADMIN.EMAIL })
9f10b292
C
77 })
78}
8c308c2b 79
53572423 80function listPods (req, res, next) {
feb4bdfd 81 db.Pod.list(function (err, podsList) {
9f10b292 82 if (err) return next(err)
45239549 83
55fa55a9 84 res.json(utils.getFormatedObjects(podsList, podsList.length))
9f10b292
C
85 })
86}
45239549 87
9f10b292 88function makeFriends (req, res, next) {
49abbbbe 89 const hosts = req.body.hosts
1e2564d3 90
49abbbbe 91 friends.makeFriends(hosts, function (err) {
9ab1071c
C
92 if (err) {
93 logger.error('Could not make friends.', { error: err })
94 return
95 }
45239549 96
9ab1071c 97 logger.info('Made friends!')
9f10b292 98 })
9ab1071c
C
99
100 res.type('json').status(204).end()
9f10b292 101}
45239549 102
9f10b292
C
103function quitFriends (req, res, next) {
104 friends.quitFriends(function (err) {
105 if (err) return next(err)
8c308c2b 106
dc8bc31b 107 res.type('json').status(204).end()
9f10b292
C
108 })
109}