]>
Commit | Line | Data |
---|---|---|
65fcc311 C |
1 | import express = require('express') |
2 | import { waterfall } from 'async' | |
3 | ||
4 | const db = require('../../initializers/database') | |
5 | import { CONFIG } from '../../initializers' | |
6 | import { | |
7 | logger, | |
8 | getMyPublicCert, | |
9 | getFormatedObjects | |
10 | } from '../../helpers' | |
11 | import { | |
12 | sendOwnedVideosToPod, | |
13 | makeFriends, | |
14 | quitFriends | |
15 | } from '../../lib' | |
16 | import { | |
17 | podsAddValidator, | |
18 | authenticate, | |
19 | ensureIsAdmin, | |
20 | makeFriendsValidator, | |
21 | setBodyHostPort, | |
22 | setBodyHostsPort | |
23 | } from '../../middlewares' | |
24 | ||
25 | const podsRouter = express.Router() | |
26 | ||
27 | podsRouter.get('/', listPods) | |
28 | podsRouter.post('/', | |
29 | setBodyHostPort, // We need to modify the host before running the validator! | |
30 | podsAddValidator, | |
31 | addPods | |
32 | ) | |
33 | podsRouter.post('/makefriends', | |
34 | authenticate, | |
35 | ensureIsAdmin, | |
36 | makeFriendsValidator, | |
37 | setBodyHostsPort, | |
38 | makeFriends | |
39 | ) | |
40 | podsRouter.get('/quitfriends', | |
41 | authenticate, | |
42 | ensureIsAdmin, | |
43 | quitFriends | |
44 | ) | |
45 | ||
46 | // --------------------------------------------------------------------------- | |
47 | ||
48 | export { | |
49 | podsRouter | |
50 | } | |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | ||
54 | function addPods (req, res, next) { | |
55 | const informations = req.body | |
56 | ||
57 | waterfall([ | |
58 | function addPod (callback) { | |
59 | const pod = db.Pod.build(informations) | |
60 | pod.save().asCallback(function (err, podCreated) { | |
61 | // Be sure about the number of parameters for the callback | |
62 | return callback(err, podCreated) | |
63 | }) | |
64 | }, | |
65 | ||
66 | function sendMyVideos (podCreated, callback) { | |
67 | sendOwnedVideosToPod(podCreated.id) | |
68 | ||
69 | callback(null) | |
70 | }, | |
71 | ||
72 | function fetchMyCertificate (callback) { | |
73 | getMyPublicCert(function (err, cert) { | |
74 | if (err) { | |
75 | logger.error('Cannot read cert file.') | |
76 | return callback(err) | |
77 | } | |
78 | ||
79 | return callback(null, cert) | |
80 | }) | |
81 | } | |
82 | ], function (err, cert) { | |
83 | if (err) return next(err) | |
84 | ||
85 | return res.json({ cert: cert, email: CONFIG.ADMIN.EMAIL }) | |
86 | }) | |
87 | } | |
88 | ||
89 | function listPods (req, res, next) { | |
90 | db.Pod.list(function (err, podsList) { | |
91 | if (err) return next(err) | |
92 | ||
93 | res.json(getFormatedObjects(podsList, podsList.length)) | |
94 | }) | |
95 | } | |
96 | ||
97 | function makeFriendsController (req, res, next) { | |
98 | const hosts = req.body.hosts | |
99 | ||
100 | makeFriends(hosts, function (err) { | |
101 | if (err) { | |
102 | logger.error('Could not make friends.', { error: err }) | |
103 | return | |
104 | } | |
105 | ||
106 | logger.info('Made friends!') | |
107 | }) | |
108 | ||
109 | res.type('json').status(204).end() | |
110 | } | |
111 | ||
112 | function quitFriendsController (req, res, next) { | |
113 | quitFriends(function (err) { | |
114 | if (err) return next(err) | |
115 | ||
116 | res.type('json').status(204).end() | |
117 | }) | |
118 | } |