]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/api/pods.js
Server: add video abuse support
[github/Chocobozzz/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 logger = require('../../helpers/logger')
8 const utils = require('../../helpers/utils')
9 const friends = require('../../lib/friends')
10 const middlewares = require('../../middlewares')
11 const admin = middlewares.admin
12 const oAuth = middlewares.oauth
13 const podsMiddleware = middlewares.pods
14 const checkSignature = middlewares.secure.checkSignature
15 const validators = middlewares.validators.pods
16 const signatureValidator = middlewares.validators.remote.signature
17
18 const router = express.Router()
19
20 router.get('/', listPods)
21 router.post('/',
22 validators.podsAdd,
23 podsMiddleware.setBodyHostPort,
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 // Post because this is a secured request
39 router.post('/remove',
40 signatureValidator.signature,
41 checkSignature,
42 removePods
43 )
44
45 // ---------------------------------------------------------------------------
46
47 module.exports = router
48
49 // ---------------------------------------------------------------------------
50
51 function addPods (req, res, next) {
52 const informations = req.body
53
54 waterfall([
55 function addPod (callback) {
56 const pod = db.Pod.build(informations)
57 pod.save().asCallback(function (err, podCreated) {
58 // Be sure about the number of parameters for the callback
59 return callback(err, podCreated)
60 })
61 },
62
63 function sendMyVideos (podCreated, callback) {
64 friends.sendOwnedVideosToPod(podCreated.id)
65
66 callback(null)
67 },
68
69 function fetchMyCertificate (callback) {
70 friends.getMyCertificate(function (err, cert) {
71 if (err) {
72 logger.error('Cannot read cert file.')
73 return callback(err)
74 }
75
76 return callback(null, cert)
77 })
78 }
79 ], function (err, cert) {
80 if (err) return next(err)
81
82 return res.json({ cert: cert })
83 })
84 }
85
86 function listPods (req, res, next) {
87 db.Pod.list(function (err, podsList) {
88 if (err) return next(err)
89
90 res.json(utils.getFormatedObjects(podsList, podsList.length))
91 })
92 }
93
94 function makeFriends (req, res, next) {
95 const hosts = req.body.hosts
96
97 friends.makeFriends(hosts, function (err) {
98 if (err) {
99 logger.error('Could not make friends.', { error: err })
100 return
101 }
102
103 logger.info('Made friends!')
104 })
105
106 res.type('json').status(204).end()
107 }
108
109 function removePods (req, res, next) {
110 const host = req.body.signature.host
111
112 waterfall([
113 function loadPod (callback) {
114 db.Pod.loadByHost(host, callback)
115 },
116
117 function deletePod (pod, callback) {
118 pod.destroy().asCallback(callback)
119 }
120 ], function (err) {
121 if (err) return next(err)
122
123 return res.type('json').status(204).end()
124 })
125 }
126
127 function quitFriends (req, res, next) {
128 friends.quitFriends(function (err) {
129 if (err) return next(err)
130
131 res.type('json').status(204).end()
132 })
133 }