'use strict'
-const async = require('async')
+const each = require('async/each')
const express = require('express')
const mongoose = require('mongoose')
+const waterfall = require('async/waterfall')
const logger = require('../../../helpers/logger')
const friends = require('../../../lib/friends')
const middlewares = require('../../../middlewares')
+const admin = middlewares.admin
const oAuth = middlewares.oauth
-const reqValidator = middlewares.reqValidators.pods
-const signatureValidator = middlewares.reqValidators.remote.signature
+const validators = middlewares.validators.pods
+const signatureValidator = middlewares.validators.remote.signature
const router = express.Router()
const Pod = mongoose.model('Pod')
const Video = mongoose.model('Video')
-router.get('/', listPodsUrl)
-router.post('/', reqValidator.podsAdd, addPods)
-router.get('/makefriends', oAuth.authenticate, reqValidator.makeFriends, makeFriends)
-router.get('/quitfriends', oAuth.authenticate, quitFriends)
+router.get('/', listPods)
+router.post('/', validators.podsAdd, addPods)
+router.post('/makefriends',
+ oAuth.authenticate,
+ admin.ensureIsAdmin,
+ validators.makeFriends,
+ makeFriends
+)
+router.get('/quitfriends',
+ oAuth.authenticate,
+ admin.ensureIsAdmin,
+ quitFriends
+)
// Post because this is a secured request
router.post('/remove', signatureValidator, removePods)
function addPods (req, res, next) {
const informations = req.body
- async.waterfall([
+ waterfall([
function addPod (callback) {
const pod = new Pod(informations)
pod.save(function (err, podCreated) {
})
}
-function listPodsUrl (req, res, next) {
- Pod.listOnlyUrls(function (err, podsUrlList) {
+function listPods (req, res, next) {
+ Pod.list(function (err, podsUrlList) {
if (err) return next(err)
- res.json(podsUrlList)
+ res.json(getFormatedPods(podsUrlList))
})
}
function makeFriends (req, res, next) {
- friends.makeFriends(function (err) {
- if (err) return next(err)
+ const urls = req.body.urls
- res.type('json').status(204).end()
+ friends.makeFriends(urls, function (err) {
+ if (err) {
+ logger.error('Could not make friends.', { error: err })
+ return
+ }
+
+ logger.info('Made friends!')
})
+
+ res.type('json').status(204).end()
}
function removePods (req, res, next) {
const url = req.body.signature.url
- async.waterfall([
+ waterfall([
function loadPod (callback) {
Pod.loadByUrl(url, callback)
},
},
function removeTheRemoteVideos (videosList, callback) {
- async.each(videosList, function (video, callbackEach) {
+ each(videosList, function (video, callbackEach) {
video.remove(callbackEach)
}, callback)
}
res.type('json').status(204).end()
})
}
+
+// ---------------------------------------------------------------------------
+
+function getFormatedPods (pods) {
+ const formatedPods = []
+
+ pods.forEach(function (pod) {
+ formatedPods.push(pod.toFormatedJSON())
+ })
+
+ return formatedPods
+}