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