]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
8c308c2b | 2 | |
f0f5567b | 3 | const express = require('express') |
1a42c9e2 | 4 | const waterfall = require('async/waterfall') |
f0f5567b | 5 | |
feb4bdfd | 6 | const db = require('../../initializers/database') |
4793c343 | 7 | const constants = require('../../initializers/constants') |
f253b1c1 | 8 | const logger = require('../../helpers/logger') |
15103f11 | 9 | const peertubeCrypto = require('../../helpers/peertube-crypto') |
55fa55a9 | 10 | const utils = require('../../helpers/utils') |
f253b1c1 C |
11 | const friends = require('../../lib/friends') |
12 | const middlewares = require('../../middlewares') | |
9bd26629 | 13 | const admin = middlewares.admin |
69b0a27c | 14 | const oAuth = middlewares.oauth |
1ab844d8 | 15 | const podsMiddleware = middlewares.pods |
0eb78d53 | 16 | const checkSignature = middlewares.secure.checkSignature |
fc51fde0 C |
17 | const validators = middlewares.validators.pods |
18 | const signatureValidator = middlewares.validators.remote.signature | |
f0f5567b C |
19 | |
20 | const router = express.Router() | |
8c308c2b | 21 | |
53572423 | 22 | router.get('/', listPods) |
1ab844d8 | 23 | router.post('/', |
b09ce645 | 24 | podsMiddleware.setBodyHostPort, // We need to modify the host before running the validator! |
1ab844d8 | 25 | validators.podsAdd, |
1ab844d8 C |
26 | addPods |
27 | ) | |
1e2564d3 | 28 | router.post('/makefriends', |
9bd26629 C |
29 | oAuth.authenticate, |
30 | admin.ensureIsAdmin, | |
31 | validators.makeFriends, | |
49abbbbe | 32 | podsMiddleware.setBodyHostsPort, |
9bd26629 C |
33 | makeFriends |
34 | ) | |
35 | router.get('/quitfriends', | |
36 | oAuth.authenticate, | |
37 | admin.ensureIsAdmin, | |
38 | quitFriends | |
39 | ) | |
9f10b292 | 40 | // Post because this is a secured request |
0eb78d53 | 41 | router.post('/remove', |
55fa55a9 | 42 | signatureValidator.signature, |
0eb78d53 C |
43 | checkSignature, |
44 | removePods | |
45 | ) | |
c45f7f84 | 46 | |
9f10b292 | 47 | // --------------------------------------------------------------------------- |
c45f7f84 | 48 | |
9f10b292 | 49 | module.exports = router |
c45f7f84 | 50 | |
9f10b292 | 51 | // --------------------------------------------------------------------------- |
8c308c2b | 52 | |
9f10b292 | 53 | function addPods (req, res, next) { |
528a9efa | 54 | const informations = req.body |
8c308c2b | 55 | |
1a42c9e2 | 56 | waterfall([ |
1cad0f39 | 57 | function addPod (callback) { |
feb4bdfd C |
58 | const pod = db.Pod.build(informations) |
59 | pod.save().asCallback(function (err, podCreated) { | |
a3ee6fa2 C |
60 | // Be sure about the number of parameters for the callback |
61 | return callback(err, podCreated) | |
62 | }) | |
1cad0f39 C |
63 | }, |
64 | ||
528a9efa | 65 | function sendMyVideos (podCreated, callback) { |
feb4bdfd | 66 | friends.sendOwnedVideosToPod(podCreated.id) |
1cad0f39 | 67 | |
528a9efa | 68 | callback(null) |
1cad0f39 C |
69 | }, |
70 | ||
71 | function fetchMyCertificate (callback) { | |
15103f11 | 72 | peertubeCrypto.getMyPublicCert(function (err, cert) { |
1cad0f39 C |
73 | if (err) { |
74 | logger.error('Cannot read cert file.') | |
75 | return callback(err) | |
76 | } | |
c173e565 | 77 | |
1cad0f39 C |
78 | return callback(null, cert) |
79 | }) | |
1cad0f39 | 80 | } |
528a9efa | 81 | ], function (err, cert) { |
1cad0f39 C |
82 | if (err) return next(err) |
83 | ||
4793c343 | 84 | return res.json({ cert: cert, email: constants.CONFIG.ADMIN.EMAIL }) |
9f10b292 C |
85 | }) |
86 | } | |
8c308c2b | 87 | |
53572423 | 88 | function listPods (req, res, next) { |
feb4bdfd | 89 | db.Pod.list(function (err, podsList) { |
9f10b292 | 90 | if (err) return next(err) |
45239549 | 91 | |
55fa55a9 | 92 | res.json(utils.getFormatedObjects(podsList, podsList.length)) |
9f10b292 C |
93 | }) |
94 | } | |
45239549 | 95 | |
9f10b292 | 96 | function makeFriends (req, res, next) { |
49abbbbe | 97 | const hosts = req.body.hosts |
1e2564d3 | 98 | |
49abbbbe | 99 | friends.makeFriends(hosts, function (err) { |
9ab1071c C |
100 | if (err) { |
101 | logger.error('Could not make friends.', { error: err }) | |
102 | return | |
103 | } | |
45239549 | 104 | |
9ab1071c | 105 | logger.info('Made friends!') |
9f10b292 | 106 | }) |
9ab1071c C |
107 | |
108 | res.type('json').status(204).end() | |
9f10b292 | 109 | } |
45239549 | 110 | |
9f10b292 | 111 | function removePods (req, res, next) { |
49abbbbe | 112 | const host = req.body.signature.host |
8c308c2b | 113 | |
1a42c9e2 | 114 | waterfall([ |
a3ee6fa2 | 115 | function loadPod (callback) { |
feb4bdfd | 116 | db.Pod.loadByHost(host, callback) |
a3ee6fa2 C |
117 | }, |
118 | ||
98ac898a | 119 | function deletePod (pod, callback) { |
feb4bdfd | 120 | pod.destroy().asCallback(callback) |
1cad0f39 C |
121 | } |
122 | ], function (err) { | |
123 | if (err) return next(err) | |
124 | ||
125 | return res.type('json').status(204).end() | |
9f10b292 C |
126 | }) |
127 | } | |
8c308c2b | 128 | |
9f10b292 C |
129 | function quitFriends (req, res, next) { |
130 | friends.quitFriends(function (err) { | |
131 | if (err) return next(err) | |
8c308c2b | 132 | |
dc8bc31b | 133 | res.type('json').status(204).end() |
9f10b292 C |
134 | }) |
135 | } |