Add video category support
[oweals/peertube.git] / server / controllers / api / pods.js
1 'use strict'
2
3 const express = require('express')
4 const waterfall = require('async/waterfall')
5
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 validators = middlewares.validators.pods
17
18 const router = express.Router()
19
20 router.get('/', listPods)
21 router.post('/',
22   podsMiddleware.setBodyHostPort, // We need to modify the host before running the validator!
23   validators.podsAdd,
24   addPods
25 )
26 router.post('/makefriends',
27   oAuth.authenticate,
28   admin.ensureIsAdmin,
29   validators.makeFriends,
30   podsMiddleware.setBodyHostsPort,
31   makeFriends
32 )
33 router.get('/quitfriends',
34   oAuth.authenticate,
35   admin.ensureIsAdmin,
36   quitFriends
37 )
38
39 // ---------------------------------------------------------------------------
40
41 module.exports = router
42
43 // ---------------------------------------------------------------------------
44
45 function addPods (req, res, next) {
46   const informations = req.body
47
48   waterfall([
49     function addPod (callback) {
50       const pod = db.Pod.build(informations)
51       pod.save().asCallback(function (err, podCreated) {
52         // Be sure about the number of parameters for the callback
53         return callback(err, podCreated)
54       })
55     },
56
57     function sendMyVideos (podCreated, callback) {
58       friends.sendOwnedVideosToPod(podCreated.id)
59
60       callback(null)
61     },
62
63     function fetchMyCertificate (callback) {
64       peertubeCrypto.getMyPublicCert(function (err, cert) {
65         if (err) {
66           logger.error('Cannot read cert file.')
67           return callback(err)
68         }
69
70         return callback(null, cert)
71       })
72     }
73   ], function (err, cert) {
74     if (err) return next(err)
75
76     return res.json({ cert: cert, email: constants.CONFIG.ADMIN.EMAIL })
77   })
78 }
79
80 function listPods (req, res, next) {
81   db.Pod.list(function (err, podsList) {
82     if (err) return next(err)
83
84     res.json(utils.getFormatedObjects(podsList, podsList.length))
85   })
86 }
87
88 function makeFriends (req, res, next) {
89   const hosts = req.body.hosts
90
91   friends.makeFriends(hosts, function (err) {
92     if (err) {
93       logger.error('Could not make friends.', { error: err })
94       return
95     }
96
97     logger.info('Made friends!')
98   })
99
100   res.type('json').status(204).end()
101 }
102
103 function quitFriends (req, res, next) {
104   friends.quitFriends(function (err) {
105     if (err) return next(err)
106
107     res.type('json').status(204).end()
108   })
109 }