]>
Commit | Line | Data |
---|---|---|
1 | 'use strict' | |
2 | ||
3 | const express = require('express') | |
4 | const mongoose = require('mongoose') | |
5 | const waterfall = require('async/waterfall') | |
6 | ||
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 | const Pod = mongoose.model('Pod') | |
19 | ||
20 | router.get('/', listPods) | |
21 | router.post('/', | |
22 | validators.podsAdd, | |
23 | podsMiddleware.setBodyUrlPort, | |
24 | addPods | |
25 | ) | |
26 | router.post('/makefriends', | |
27 | oAuth.authenticate, | |
28 | admin.ensureIsAdmin, | |
29 | validators.makeFriends, | |
30 | podsMiddleware.setBodyUrlsPort, | |
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, | |
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 = new Pod(informations) | |
57 | pod.save(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 | Pod.list(function (err, podsUrlList) { | |
88 | if (err) return next(err) | |
89 | ||
90 | res.json(getFormatedPods(podsUrlList)) | |
91 | }) | |
92 | } | |
93 | ||
94 | function makeFriends (req, res, next) { | |
95 | const urls = req.body.urls | |
96 | ||
97 | friends.makeFriends(urls, 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 url = req.body.signature.url | |
111 | ||
112 | waterfall([ | |
113 | function loadPod (callback) { | |
114 | Pod.loadByUrl(url, callback) | |
115 | }, | |
116 | ||
117 | function removePod (pod, callback) { | |
118 | pod.remove(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 | } | |
134 | ||
135 | // --------------------------------------------------------------------------- | |
136 | ||
137 | function getFormatedPods (pods) { | |
138 | const formatedPods = [] | |
139 | ||
140 | pods.forEach(function (pod) { | |
141 | formatedPods.push(pod.toFormatedJSON()) | |
142 | }) | |
143 | ||
144 | return formatedPods | |
145 | } |