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