3 const express = require('express')
4 const waterfall = require('async/waterfall')
6 const db = require('../../initializers/database')
7 const constants = require('../../initializers/constants')
8 const logger = require('../../helpers/logger')
9 const peertubeCrypto = require('../../helpers/peertube-crypto')
10 const utils = require('../../helpers/utils')
11 const friends = require('../../lib/friends')
12 const middlewares = require('../../middlewares')
13 const admin = middlewares.admin
14 const oAuth = middlewares.oauth
15 const podsMiddleware = middlewares.pods
16 const checkSignature = middlewares.secure.checkSignature
17 const validators = middlewares.validators.pods
18 const signatureValidator = middlewares.validators.remote.signature
20 const router = express.Router()
22 router.get('/', listPods)
24 podsMiddleware.setBodyHostPort, // We need to modify the host before running the validator!
28 router.post('/makefriends',
31 validators.makeFriends,
32 podsMiddleware.setBodyHostsPort,
35 router.get('/quitfriends',
40 // Post because this is a secured request
41 router.post('/remove',
42 signatureValidator.signature,
47 // ---------------------------------------------------------------------------
49 module.exports = router
51 // ---------------------------------------------------------------------------
53 function addPods (req, res, next) {
54 const informations = req.body
57 function addPod (callback) {
58 const pod = db.Pod.build(informations)
59 pod.save().asCallback(function (err, podCreated) {
60 // Be sure about the number of parameters for the callback
61 return callback(err, podCreated)
65 function sendMyVideos (podCreated, callback) {
66 friends.sendOwnedVideosToPod(podCreated.id)
71 function fetchMyCertificate (callback) {
72 peertubeCrypto.getMyPublicCert(function (err, cert) {
74 logger.error('Cannot read cert file.')
78 return callback(null, cert)
81 ], function (err, cert) {
82 if (err) return next(err)
84 return res.json({ cert: cert, email: constants.CONFIG.ADMIN.EMAIL })
88 function listPods (req, res, next) {
89 db.Pod.list(function (err, podsList) {
90 if (err) return next(err)
92 res.json(utils.getFormatedObjects(podsList, podsList.length))
96 function makeFriends (req, res, next) {
97 const hosts = req.body.hosts
99 friends.makeFriends(hosts, function (err) {
101 logger.error('Could not make friends.', { error: err })
105 logger.info('Made friends!')
108 res.type('json').status(204).end()
111 function removePods (req, res, next) {
112 const host = req.body.signature.host
115 function loadPod (callback) {
116 db.Pod.loadByHost(host, callback)
119 function deletePod (pod, callback) {
120 pod.destroy().asCallback(callback)
123 if (err) return next(err)
125 return res.type('json').status(204).end()
129 function quitFriends (req, res, next) {
130 friends.quitFriends(function (err) {
131 if (err) return next(err)
133 res.type('json').status(204).end()