3 const express = require('express')
4 const mongoose = require('mongoose')
5 const waterfall = require('async/waterfall')
7 const logger = require('../../helpers/logger')
8 const friends = require('../../lib/friends')
9 const middlewares = require('../../middlewares')
10 const admin = middlewares.admin
11 const oAuth = middlewares.oauth
12 const podsMiddleware = middlewares.pods
13 const checkSignature = middlewares.secure.checkSignature
14 const validators = middlewares.validators.pods
15 const signatureValidator = middlewares.validators.remote.signature
17 const router = express.Router()
18 const Pod = mongoose.model('Pod')
20 router.get('/', listPods)
23 podsMiddleware.setBodyHostPort,
26 router.post('/makefriends',
29 validators.makeFriends,
30 podsMiddleware.setBodyHostsPort,
33 router.get('/quitfriends',
38 // Post because this is a secured request
39 router.post('/remove',
45 // ---------------------------------------------------------------------------
47 module.exports = router
49 // ---------------------------------------------------------------------------
51 function addPods (req, res, next) {
52 const informations = req.body
55 function addPod (callback) {
56 const pod = new Pod(informations)
57 pod.save(function (err, podCreated) {
58 // Be sure about the number of parameters for the callback
59 return callback(err, podCreated)
63 function sendMyVideos (podCreated, callback) {
64 friends.sendOwnedVideosToPod(podCreated._id)
69 function fetchMyCertificate (callback) {
70 friends.getMyCertificate(function (err, cert) {
72 logger.error('Cannot read cert file.')
76 return callback(null, cert)
79 ], function (err, cert) {
80 if (err) return next(err)
82 return res.json({ cert: cert })
86 function listPods (req, res, next) {
87 Pod.list(function (err, podsList) {
88 if (err) return next(err)
90 res.json(getFormatedPods(podsList))
94 function makeFriends (req, res, next) {
95 const hosts = req.body.hosts
97 friends.makeFriends(hosts, function (err) {
99 logger.error('Could not make friends.', { error: err })
103 logger.info('Made friends!')
106 res.type('json').status(204).end()
109 function removePods (req, res, next) {
110 const host = req.body.signature.host
113 function loadPod (callback) {
114 Pod.loadByHost(host, callback)
117 function removePod (pod, callback) {
121 if (err) return next(err)
123 return res.type('json').status(204).end()
127 function quitFriends (req, res, next) {
128 friends.quitFriends(function (err) {
129 if (err) return next(err)
131 res.type('json').status(204).end()
135 // ---------------------------------------------------------------------------
137 function getFormatedPods (pods) {
138 const formatedPods = []
140 pods.forEach(function (pod) {
141 formatedPods.push(pod.toFormatedJSON())