]>
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 |
0eb78d53 | 13 | const checkSignature = middlewares.secure.checkSignature |
fc51fde0 C |
14 | const validators = middlewares.validators.pods |
15 | const signatureValidator = middlewares.validators.remote.signature | |
f0f5567b C |
16 | |
17 | const router = express.Router() | |
a3ee6fa2 | 18 | const Pod = mongoose.model('Pod') |
aaf61f38 | 19 | const Video = mongoose.model('Video') |
8c308c2b | 20 | |
53572423 | 21 | router.get('/', listPods) |
fc51fde0 | 22 | router.post('/', validators.podsAdd, addPods) |
1e2564d3 | 23 | router.post('/makefriends', |
9bd26629 C |
24 | oAuth.authenticate, |
25 | admin.ensureIsAdmin, | |
26 | validators.makeFriends, | |
27 | makeFriends | |
28 | ) | |
29 | router.get('/quitfriends', | |
30 | oAuth.authenticate, | |
31 | admin.ensureIsAdmin, | |
32 | quitFriends | |
33 | ) | |
9f10b292 | 34 | // Post because this is a secured request |
0eb78d53 C |
35 | router.post('/remove', |
36 | signatureValidator, | |
37 | checkSignature, | |
38 | removePods | |
39 | ) | |
c45f7f84 | 40 | |
9f10b292 | 41 | // --------------------------------------------------------------------------- |
c45f7f84 | 42 | |
9f10b292 | 43 | module.exports = router |
c45f7f84 | 44 | |
9f10b292 | 45 | // --------------------------------------------------------------------------- |
8c308c2b | 46 | |
9f10b292 | 47 | function addPods (req, res, next) { |
528a9efa | 48 | const informations = req.body |
8c308c2b | 49 | |
1a42c9e2 | 50 | waterfall([ |
1cad0f39 | 51 | function addPod (callback) { |
a3ee6fa2 C |
52 | const pod = new Pod(informations) |
53 | pod.save(function (err, podCreated) { | |
54 | // Be sure about the number of parameters for the callback | |
55 | return callback(err, podCreated) | |
56 | }) | |
1cad0f39 C |
57 | }, |
58 | ||
528a9efa C |
59 | function sendMyVideos (podCreated, callback) { |
60 | friends.sendOwnedVideosToPod(podCreated._id) | |
1cad0f39 | 61 | |
528a9efa | 62 | callback(null) |
1cad0f39 C |
63 | }, |
64 | ||
65 | function fetchMyCertificate (callback) { | |
66 | friends.getMyCertificate(function (err, cert) { | |
67 | if (err) { | |
68 | logger.error('Cannot read cert file.') | |
69 | return callback(err) | |
70 | } | |
c173e565 | 71 | |
1cad0f39 C |
72 | return callback(null, cert) |
73 | }) | |
1cad0f39 | 74 | } |
528a9efa | 75 | ], function (err, cert) { |
1cad0f39 C |
76 | if (err) return next(err) |
77 | ||
528a9efa | 78 | return res.json({ cert: cert }) |
9f10b292 C |
79 | }) |
80 | } | |
8c308c2b | 81 | |
53572423 C |
82 | function listPods (req, res, next) { |
83 | Pod.list(function (err, podsUrlList) { | |
9f10b292 | 84 | if (err) return next(err) |
45239549 | 85 | |
53572423 | 86 | res.json(getFormatedPods(podsUrlList)) |
9f10b292 C |
87 | }) |
88 | } | |
45239549 | 89 | |
9f10b292 | 90 | function makeFriends (req, res, next) { |
1e2564d3 C |
91 | const urls = req.body.urls |
92 | ||
93 | friends.makeFriends(urls, function (err) { | |
9ab1071c C |
94 | if (err) { |
95 | logger.error('Could not make friends.', { error: err }) | |
96 | return | |
97 | } | |
45239549 | 98 | |
9ab1071c | 99 | logger.info('Made friends!') |
9f10b292 | 100 | }) |
9ab1071c C |
101 | |
102 | res.type('json').status(204).end() | |
9f10b292 | 103 | } |
45239549 | 104 | |
9f10b292 | 105 | function removePods (req, res, next) { |
f0f5567b | 106 | const url = req.body.signature.url |
8c308c2b | 107 | |
1a42c9e2 | 108 | waterfall([ |
a3ee6fa2 C |
109 | function loadPod (callback) { |
110 | Pod.loadByUrl(url, callback) | |
111 | }, | |
112 | ||
113 | function removePod (pod, callback) { | |
114 | pod.remove(function (err) { | |
115 | // Be sure we only return one argument in the callback | |
1cad0f39 C |
116 | return callback(err) |
117 | }) | |
118 | }, | |
119 | ||
120 | function (callback) { | |
aaf61f38 | 121 | Video.listByUrls([ url ], function (err, videosList) { |
1cad0f39 C |
122 | if (err) { |
123 | logger.error('Cannot list videos from url.', { error: err }) | |
124 | return callback(err) | |
125 | } | |
126 | ||
127 | return callback(null, videosList) | |
128 | }) | |
129 | }, | |
8425cb89 | 130 | |
1cad0f39 | 131 | function removeTheRemoteVideos (videosList, callback) { |
1a42c9e2 | 132 | each(videosList, function (video, callbackEach) { |
aaf61f38 C |
133 | video.remove(callbackEach) |
134 | }, callback) | |
1cad0f39 C |
135 | } |
136 | ], function (err) { | |
137 | if (err) return next(err) | |
138 | ||
139 | return res.type('json').status(204).end() | |
9f10b292 C |
140 | }) |
141 | } | |
8c308c2b | 142 | |
9f10b292 C |
143 | function quitFriends (req, res, next) { |
144 | friends.quitFriends(function (err) { | |
145 | if (err) return next(err) | |
8c308c2b | 146 | |
dc8bc31b | 147 | res.type('json').status(204).end() |
9f10b292 C |
148 | }) |
149 | } | |
53572423 C |
150 | |
151 | // --------------------------------------------------------------------------- | |
152 | ||
153 | function getFormatedPods (pods) { | |
154 | const formatedPods = [] | |
155 | ||
156 | pods.forEach(function (pod) { | |
157 | formatedPods.push(pod.toFormatedJSON()) | |
158 | }) | |
159 | ||
160 | return formatedPods | |
161 | } |