diff options
Diffstat (limited to 'server/controllers/api/v1/pods.js')
-rw-r--r-- | server/controllers/api/v1/pods.js | 62 |
1 files changed, 51 insertions, 11 deletions
diff --git a/server/controllers/api/v1/pods.js b/server/controllers/api/v1/pods.js index 2bc761fef..8ffade578 100644 --- a/server/controllers/api/v1/pods.js +++ b/server/controllers/api/v1/pods.js | |||
@@ -8,7 +8,10 @@ const waterfall = require('async/waterfall') | |||
8 | const logger = require('../../../helpers/logger') | 8 | const logger = require('../../../helpers/logger') |
9 | const friends = require('../../../lib/friends') | 9 | const friends = require('../../../lib/friends') |
10 | const middlewares = require('../../../middlewares') | 10 | const middlewares = require('../../../middlewares') |
11 | const admin = middlewares.admin | ||
11 | const oAuth = middlewares.oauth | 12 | const oAuth = middlewares.oauth |
13 | const podsMiddleware = middlewares.pods | ||
14 | const checkSignature = middlewares.secure.checkSignature | ||
12 | const validators = middlewares.validators.pods | 15 | const validators = middlewares.validators.pods |
13 | const signatureValidator = middlewares.validators.remote.signature | 16 | const signatureValidator = middlewares.validators.remote.signature |
14 | 17 | ||
@@ -16,12 +19,30 @@ const router = express.Router() | |||
16 | const Pod = mongoose.model('Pod') | 19 | const Pod = mongoose.model('Pod') |
17 | const Video = mongoose.model('Video') | 20 | const Video = mongoose.model('Video') |
18 | 21 | ||
19 | router.get('/', listPodsUrl) | 22 | router.get('/', listPods) |
20 | router.post('/', validators.podsAdd, addPods) | 23 | router.post('/', |
21 | router.get('/makefriends', oAuth.authenticate, validators.makeFriends, makeFriends) | 24 | validators.podsAdd, |
22 | router.get('/quitfriends', oAuth.authenticate, quitFriends) | 25 | podsMiddleware.setBodyUrlPort, |
26 | addPods | ||
27 | ) | ||
28 | router.post('/makefriends', | ||
29 | oAuth.authenticate, | ||
30 | admin.ensureIsAdmin, | ||
31 | validators.makeFriends, | ||
32 | podsMiddleware.setBodyUrlsPort, | ||
33 | makeFriends | ||
34 | ) | ||
35 | router.get('/quitfriends', | ||
36 | oAuth.authenticate, | ||
37 | admin.ensureIsAdmin, | ||
38 | quitFriends | ||
39 | ) | ||
23 | // Post because this is a secured request | 40 | // Post because this is a secured request |
24 | router.post('/remove', signatureValidator, removePods) | 41 | router.post('/remove', |
42 | signatureValidator, | ||
43 | checkSignature, | ||
44 | removePods | ||
45 | ) | ||
25 | 46 | ||
26 | // --------------------------------------------------------------------------- | 47 | // --------------------------------------------------------------------------- |
27 | 48 | ||
@@ -64,20 +85,27 @@ function addPods (req, res, next) { | |||
64 | }) | 85 | }) |
65 | } | 86 | } |
66 | 87 | ||
67 | function listPodsUrl (req, res, next) { | 88 | function listPods (req, res, next) { |
68 | Pod.listOnlyUrls(function (err, podsUrlList) { | 89 | Pod.list(function (err, podsUrlList) { |
69 | if (err) return next(err) | 90 | if (err) return next(err) |
70 | 91 | ||
71 | res.json(podsUrlList) | 92 | res.json(getFormatedPods(podsUrlList)) |
72 | }) | 93 | }) |
73 | } | 94 | } |
74 | 95 | ||
75 | function makeFriends (req, res, next) { | 96 | function makeFriends (req, res, next) { |
76 | friends.makeFriends(function (err) { | 97 | const urls = req.body.urls |
77 | if (err) return next(err) | ||
78 | 98 | ||
79 | res.type('json').status(204).end() | 99 | friends.makeFriends(urls, function (err) { |
100 | if (err) { | ||
101 | logger.error('Could not make friends.', { error: err }) | ||
102 | return | ||
103 | } | ||
104 | |||
105 | logger.info('Made friends!') | ||
80 | }) | 106 | }) |
107 | |||
108 | res.type('json').status(204).end() | ||
81 | } | 109 | } |
82 | 110 | ||
83 | function removePods (req, res, next) { | 111 | function removePods (req, res, next) { |
@@ -125,3 +153,15 @@ function quitFriends (req, res, next) { | |||
125 | res.type('json').status(204).end() | 153 | res.type('json').status(204).end() |
126 | }) | 154 | }) |
127 | } | 155 | } |
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 | } | ||