]>
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 | |
528a9efa | 20 | router.get('/', listPodsUrl) |
fc51fde0 | 21 | router.post('/', validators.podsAdd, addPods) |
9bd26629 C |
22 | router.get('/makefriends', |
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 | |
528a9efa | 77 | function listPodsUrl (req, res, next) { |
a3ee6fa2 | 78 | Pod.listOnlyUrls(function (err, podsUrlList) { |
9f10b292 | 79 | if (err) return next(err) |
45239549 | 80 | |
528a9efa | 81 | res.json(podsUrlList) |
9f10b292 C |
82 | }) |
83 | } | |
45239549 | 84 | |
9f10b292 C |
85 | function makeFriends (req, res, next) { |
86 | friends.makeFriends(function (err) { | |
87 | if (err) return next(err) | |
45239549 | 88 | |
dc8bc31b | 89 | res.type('json').status(204).end() |
9f10b292 C |
90 | }) |
91 | } | |
45239549 | 92 | |
9f10b292 | 93 | function removePods (req, res, next) { |
f0f5567b | 94 | const url = req.body.signature.url |
8c308c2b | 95 | |
1a42c9e2 | 96 | waterfall([ |
a3ee6fa2 C |
97 | function loadPod (callback) { |
98 | Pod.loadByUrl(url, callback) | |
99 | }, | |
100 | ||
101 | function removePod (pod, callback) { | |
102 | pod.remove(function (err) { | |
103 | // Be sure we only return one argument in the callback | |
1cad0f39 C |
104 | return callback(err) |
105 | }) | |
106 | }, | |
107 | ||
108 | function (callback) { | |
aaf61f38 | 109 | Video.listByUrls([ url ], function (err, videosList) { |
1cad0f39 C |
110 | if (err) { |
111 | logger.error('Cannot list videos from url.', { error: err }) | |
112 | return callback(err) | |
113 | } | |
114 | ||
115 | return callback(null, videosList) | |
116 | }) | |
117 | }, | |
8425cb89 | 118 | |
1cad0f39 | 119 | function removeTheRemoteVideos (videosList, callback) { |
1a42c9e2 | 120 | each(videosList, function (video, callbackEach) { |
aaf61f38 C |
121 | video.remove(callbackEach) |
122 | }, callback) | |
1cad0f39 C |
123 | } |
124 | ], function (err) { | |
125 | if (err) return next(err) | |
126 | ||
127 | return res.type('json').status(204).end() | |
9f10b292 C |
128 | }) |
129 | } | |
8c308c2b | 130 | |
9f10b292 C |
131 | function quitFriends (req, res, next) { |
132 | friends.quitFriends(function (err) { | |
133 | if (err) return next(err) | |
8c308c2b | 134 | |
dc8bc31b | 135 | res.type('json').status(204).end() |
9f10b292 C |
136 | }) |
137 | } |