]>
Commit | Line | Data |
---|---|---|
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 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 | |
16 | ||
17 | const router = express.Router() | |
18 | ||
19 | router.get('/', listPods) | |
20 | router.post('/', | |
21 | validators.podsAdd, | |
22 | podsMiddleware.setBodyHostPort, | |
23 | addPods | |
24 | ) | |
25 | router.post('/makefriends', | |
26 | oAuth.authenticate, | |
27 | admin.ensureIsAdmin, | |
28 | validators.makeFriends, | |
29 | podsMiddleware.setBodyHostsPort, | |
30 | makeFriends | |
31 | ) | |
32 | router.get('/quitfriends', | |
33 | oAuth.authenticate, | |
34 | admin.ensureIsAdmin, | |
35 | quitFriends | |
36 | ) | |
37 | // Post because this is a secured request | |
38 | router.post('/remove', | |
39 | signatureValidator, | |
40 | checkSignature, | |
41 | removePods | |
42 | ) | |
43 | ||
44 | // --------------------------------------------------------------------------- | |
45 | ||
46 | module.exports = router | |
47 | ||
48 | // --------------------------------------------------------------------------- | |
49 | ||
50 | function addPods (req, res, next) { | |
51 | const informations = req.body | |
52 | ||
53 | waterfall([ | |
54 | function addPod (callback) { | |
55 | const pod = db.Pod.build(informations) | |
56 | pod.save().asCallback(function (err, podCreated) { | |
57 | // Be sure about the number of parameters for the callback | |
58 | return callback(err, podCreated) | |
59 | }) | |
60 | }, | |
61 | ||
62 | function sendMyVideos (podCreated, callback) { | |
63 | friends.sendOwnedVideosToPod(podCreated.id) | |
64 | ||
65 | callback(null) | |
66 | }, | |
67 | ||
68 | function fetchMyCertificate (callback) { | |
69 | friends.getMyCertificate(function (err, cert) { | |
70 | if (err) { | |
71 | logger.error('Cannot read cert file.') | |
72 | return callback(err) | |
73 | } | |
74 | ||
75 | return callback(null, cert) | |
76 | }) | |
77 | } | |
78 | ], function (err, cert) { | |
79 | if (err) return next(err) | |
80 | ||
81 | return res.json({ cert: cert }) | |
82 | }) | |
83 | } | |
84 | ||
85 | function listPods (req, res, next) { | |
86 | db.Pod.list(function (err, podsList) { | |
87 | if (err) return next(err) | |
88 | ||
89 | res.json(getFormatedPods(podsList)) | |
90 | }) | |
91 | } | |
92 | ||
93 | function makeFriends (req, res, next) { | |
94 | const hosts = req.body.hosts | |
95 | ||
96 | friends.makeFriends(hosts, function (err) { | |
97 | if (err) { | |
98 | logger.error('Could not make friends.', { error: err }) | |
99 | return | |
100 | } | |
101 | ||
102 | logger.info('Made friends!') | |
103 | }) | |
104 | ||
105 | res.type('json').status(204).end() | |
106 | } | |
107 | ||
108 | function removePods (req, res, next) { | |
109 | const host = req.body.signature.host | |
110 | ||
111 | waterfall([ | |
112 | function loadPod (callback) { | |
113 | db.Pod.loadByHost(host, callback) | |
114 | }, | |
115 | ||
116 | function deletePod (pod, callback) { | |
117 | pod.destroy().asCallback(callback) | |
118 | } | |
119 | ], function (err) { | |
120 | if (err) return next(err) | |
121 | ||
122 | return res.type('json').status(204).end() | |
123 | }) | |
124 | } | |
125 | ||
126 | function quitFriends (req, res, next) { | |
127 | friends.quitFriends(function (err) { | |
128 | if (err) return next(err) | |
129 | ||
130 | res.type('json').status(204).end() | |
131 | }) | |
132 | } | |
133 | ||
134 | // --------------------------------------------------------------------------- | |
135 | ||
136 | function getFormatedPods (pods) { | |
137 | const formatedPods = [] | |
138 | ||
139 | pods.forEach(function (pod) { | |
140 | formatedPods.push(pod.toFormatedJSON()) | |
141 | }) | |
142 | ||
143 | return formatedPods | |
144 | } |