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