]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
8c308c2b | 2 | |
1a42c9e2 | 3 | const each = require('async/each') |
f0f5567b | 4 | const express = require('express') |
aaf61f38 | 5 | const mongoose = require('mongoose') |
1a42c9e2 | 6 | const waterfall = require('async/waterfall') |
f0f5567b C |
7 | |
8 | const logger = require('../../../helpers/logger') | |
9 | const friends = require('../../../lib/friends') | |
b3b92647 | 10 | const middlewares = require('../../../middlewares') |
9bd26629 | 11 | const admin = middlewares.admin |
69b0a27c | 12 | const oAuth = middlewares.oauth |
1ab844d8 | 13 | const podsMiddleware = middlewares.pods |
0eb78d53 | 14 | const checkSignature = middlewares.secure.checkSignature |
fc51fde0 C |
15 | const validators = middlewares.validators.pods |
16 | const signatureValidator = middlewares.validators.remote.signature | |
f0f5567b C |
17 | |
18 | const router = express.Router() | |
a3ee6fa2 | 19 | const Pod = mongoose.model('Pod') |
aaf61f38 | 20 | const Video = mongoose.model('Video') |
8c308c2b | 21 | |
53572423 | 22 | router.get('/', listPods) |
1ab844d8 C |
23 | router.post('/', |
24 | validators.podsAdd, | |
25 | podsMiddleware.setBodyUrlPort, | |
26 | addPods | |
27 | ) | |
1e2564d3 | 28 | router.post('/makefriends', |
9bd26629 C |
29 | oAuth.authenticate, |
30 | admin.ensureIsAdmin, | |
31 | validators.makeFriends, | |
1ab844d8 | 32 | podsMiddleware.setBodyUrlsPort, |
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 C |
41 | router.post('/remove', |
42 | signatureValidator, | |
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) { |
a3ee6fa2 C |
58 | const pod = new Pod(informations) |
59 | pod.save(function (err, podCreated) { | |
60 | // Be sure about the number of parameters for the callback | |
61 | return callback(err, podCreated) | |
62 | }) | |
1cad0f39 C |
63 | }, |
64 | ||
528a9efa C |
65 | function sendMyVideos (podCreated, callback) { |
66 | friends.sendOwnedVideosToPod(podCreated._id) | |
1cad0f39 | 67 | |
528a9efa | 68 | callback(null) |
1cad0f39 C |
69 | }, |
70 | ||
71 | function fetchMyCertificate (callback) { | |
72 | friends.getMyCertificate(function (err, cert) { | |
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 | ||
528a9efa | 84 | return res.json({ cert: cert }) |
9f10b292 C |
85 | }) |
86 | } | |
8c308c2b | 87 | |
53572423 C |
88 | function listPods (req, res, next) { |
89 | Pod.list(function (err, podsUrlList) { | |
9f10b292 | 90 | if (err) return next(err) |
45239549 | 91 | |
53572423 | 92 | res.json(getFormatedPods(podsUrlList)) |
9f10b292 C |
93 | }) |
94 | } | |
45239549 | 95 | |
9f10b292 | 96 | function makeFriends (req, res, next) { |
1e2564d3 C |
97 | const urls = req.body.urls |
98 | ||
99 | friends.makeFriends(urls, 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) { |
f0f5567b | 112 | const url = req.body.signature.url |
8c308c2b | 113 | |
1a42c9e2 | 114 | waterfall([ |
a3ee6fa2 C |
115 | function loadPod (callback) { |
116 | Pod.loadByUrl(url, callback) | |
117 | }, | |
118 | ||
119 | function removePod (pod, callback) { | |
120 | pod.remove(function (err) { | |
121 | // Be sure we only return one argument in the callback | |
1cad0f39 C |
122 | return callback(err) |
123 | }) | |
124 | }, | |
125 | ||
126 | function (callback) { | |
aaf61f38 | 127 | Video.listByUrls([ url ], function (err, videosList) { |
1cad0f39 C |
128 | if (err) { |
129 | logger.error('Cannot list videos from url.', { error: err }) | |
130 | return callback(err) | |
131 | } | |
132 | ||
133 | return callback(null, videosList) | |
134 | }) | |
135 | }, | |
8425cb89 | 136 | |
1cad0f39 | 137 | function removeTheRemoteVideos (videosList, callback) { |
1a42c9e2 | 138 | each(videosList, function (video, callbackEach) { |
aaf61f38 C |
139 | video.remove(callbackEach) |
140 | }, callback) | |
1cad0f39 C |
141 | } |
142 | ], function (err) { | |
143 | if (err) return next(err) | |
144 | ||
145 | return res.type('json').status(204).end() | |
9f10b292 C |
146 | }) |
147 | } | |
8c308c2b | 148 | |
9f10b292 C |
149 | function quitFriends (req, res, next) { |
150 | friends.quitFriends(function (err) { | |
151 | if (err) return next(err) | |
8c308c2b | 152 | |
dc8bc31b | 153 | res.type('json').status(204).end() |
9f10b292 C |
154 | }) |
155 | } | |
53572423 C |
156 | |
157 | // --------------------------------------------------------------------------- | |
158 | ||
159 | function getFormatedPods (pods) { | |
160 | const formatedPods = [] | |
161 | ||
162 | pods.forEach(function (pod) { | |
163 | formatedPods.push(pod.toFormatedJSON()) | |
164 | }) | |
165 | ||
166 | return formatedPods | |
167 | } |